next-react-router icon indicating copy to clipboard operation
next-react-router copied to clipboard

Async data prefetch for pages

Open KonstantinBabichev opened this issue 5 years ago • 5 comments

I wonder if there is any way to prefetch data during SSR like NextJS provides convenient SomePage.getInitialProps functionality following the approach? Btw, Great job! I’ve been dreaming about NextJS+RR for couple months.

KonstantinBabichev avatar Aug 31 '19 22:08 KonstantinBabichev

Hi! I expect any NextJS feature not relying on the native routing system (like static pages and partial local dev builds) to work as expected.

toomuchdesign avatar Sep 01 '19 08:09 toomuchdesign

Got it. At the same time getInitialProps being triggered in process of NextJS routing lifecycle, so it's not expected to be working. Right? For example, i've added:

function About() {
  return <h2>About</h2>;
}

About.getInitialProps = () => {
  console.log('About says: Gimme some data, please.');
};

to example https://github.com/toomuchdesign/next-react-router/issues/1 and About.getInitialProps not being called. Do you see any chance to bring it back to fulfill SSR with async data?

KonstantinBabichev avatar Sep 01 '19 10:09 KonstantinBabichev

I'm sure there is room for improvement but, given the feedback I got from nextJS maintainers, I won't spend any time on this subject. I stay open to PR's, btw.

toomuchdesign avatar Sep 01 '19 12:09 toomuchdesign

Just out of curiosity, what was some of the feedback you got from NextJS maintainers?

atav32 avatar Dec 23 '19 21:12 atav32

Hello guys, I was in the same situation and found a possible alternative. In case you can use apollo and fetch your data with Graphql, they have a very good solution in place where they query getDataFromTree and look for all the queries inside the components, resolve them and then return the rendered components with all the data inside! This method is hooking inside the original getInitialProps, you can find the code here --> https://github.com/zeit/next.js/blob/canary/examples/with-apollo/lib/apollo.js#L106

Besides, you are still not being able to use getInitialProps directly, you can query data on the server-side 😄

The full example is here --> https://github.com/zeit/next.js/tree/canary/examples/with-apollo Hope it helps other 👍

EmaSuriano avatar Feb 17 '20 17:02 EmaSuriano