Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api call optimize #128

Closed
frozenfung opened this issue Sep 30, 2017 · 2 comments
Closed

api call optimize #128

frozenfung opened this issue Sep 30, 2017 · 2 comments

Comments

@frozenfung
Copy link

Hey teams, I am confused where to make api calls in the universal.
Let's say I have to route A and route B

route A need data from C api and D api.
route B need data from C api and E api.

Now I use put C api call in middleware in action.

{
  path: 'subRoot',
  action() {
    // call C api
    // combine data from C api and child
    // return child
  },
  children: [{
    path: 'A',
    action() {
      // call D api
      // return 
    }
  }, {
    path: 'B',
    action() {
      // call D api
      // return 
    }
  }

But I found that actually I do not need data from C api everytime route/url change. I only need it in server-side rendering and first time I switch to subRoot in client. Am I put these apis in the wrong places?

thanks

@frenzzy
Copy link
Member

frenzzy commented Oct 3, 2017

If you need to do something server-side only it is not isomorphic/universal code. You can add condition for this case

if (process.env.SERVER) { // or similar
  // server-side only stuff here
}

Another way is to share the result of such request between client and server side.

const route = {
  path: '/news',
  async action({ store, fetch }) {
    if (!store.news) {
      store.news = await fetch('/api/news');
    }
    return { title: 'News Page', component: <News news={store.news} /> };
  },
};

and transfer store form server to client like here

@frozenfung
Copy link
Author

Currently it works with a solution like the store thing, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants