-
Notifications
You must be signed in to change notification settings - Fork 64
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
Question: is it possible to use with getStaticProps()? #92
Comments
Hey Lydia - yeah that's correct. You'll have to copy one of the solutions in that thread. Take a look at this boilerplate. Might look into updating this package to abstract some of that boilerplate. |
Hey Adam, Thanks so much for replying and for the suggestion. I'm looking forward to how everything will work with React's new server components. Maybe it won't make any difference though! Have a nice new years! |
@adamsoffer What are you looking for with the abstraction? Would it be sufficient to basically lift getStaticApolloProps({
client: new ApolloClient({
uri: 'https://48p1r2roz4.sse.codesandbox.io',
cache: new InMemoryCache(),
}),
})(MyComponent); I wouldn't mind copying the code over and testing it if that's all you're looking for. |
@chrbala - yeah pretty much! That would be awesome |
Added a PR - let me know what you think! |
I've discovered that globalApolloClient has an issue with getStaticProps: it uses the cache of the initial page, requiring a refetch on page transitions. From what I can tell, there are a couple possible solutions:
My inclination is to do option 3 for now, and it can remain the default option if and when additional configuration is added later on. Thoughts? |
Hey @chrbala - options 3 sounds good and the PR looks good! I'll try and cut a release this weekend. Thanks so much for this. |
I updated the branch in the PR with option 3. I didn't add it to my testing repo, but I did test it to make sure it works in my application. |
Cool thanks. Question I just thought of. Consider this scenario using option 3:
In this scenario, will apollo have to refetch the list data since option 3 would have cleared the cache? |
Yes, they'll have to refetch the data in that case. We could replace If we go that second route, we'd have to either make a breaking change to the library API, or handle the ambiguous case where someone uses withApollo(client) together with getStaticApolloProps. Do we throw to ensure consistent experiences and to avoid having to handle the weird case? We could handle that in way I initially proposed, but it seems like we'd be optimizing for a case that shouldn't really be supported. So, what I'm proposing with all of that in mind is that (I'll also say that I don't exactly have a horse in this race for now, aside from shipping |
Gotcha. Thanks for the explaination! I think it's actually fairly common for apps to use
I think I'm slightly confused about what the issue is. Would you mind explaining how and why the above would cause a refetch when it's not supposed to? |
Yeah, basically the static props are the initial state on the first page load. You only get one first page load until you do a full HTML page refresh, so that's the only time you pull in an initial state. If you navigate from a page that already has an apollo client, you have to (1) merge the initial state from the page props into the current Apollo cache, (2) download it from the server (which Apollo merges into the current cache behind the scenes), or (3) reset the cache and set the page's apollo client with the new page's initial state. Each of those options has their own downsides.
|
hm cache seems to work fine on page transitions on next-apollo-ts. Is this necessary? |
Yeah, it is. I added a repro that intentionally breaks fetches to illustrate the issue, so that all data must come through the SSG props. Data doesn't load correctly when the page transitions between |
Gotcha. Sorry for dropping off. So I'm using next-apollo-ts on one of my projects and for some reason, the cache works as expected when navigating between a page rendered using SSG and another rendered client-side. Notice when you click the first row on the table of this statically generated page you'll see a spinner. When you go back to the "orchestrators" page and then click the first row again and it gets served from apollo's cache and there's no spinner. I can't explain why but it seems to be working as it should. 🤷 |
There are enough requests going on with your network history there that I can't really trace it down easily, but I think I know what's going on there. Actually, the experience you're describing indicates that the library isn't actually using the SSG data on route transitions. When you load the statically generated page, the data doesn't exist in the cache, so Apollo fills it in with a network request. That's why you see the spinner. If that page used the SSG data, there wouldn't be a spinner because the data from the SSG function populates synchronously. The second time you load the page, the server data has populated the cache, so it loads synchronously in your case as well. After the cache has been populated, both versions are indistinguishable in your case: it's just the first load that changes. In your case, the app is functional, but takes an extra network request the first time those pages load. If an app only uses SSG and has no live endpoint, they can't use links from 'next/link' for fast routing, and would have to use |
Dear Adam,
Thank you for this wonderful package. Please feel free to ignore this question if it is silly. I am struggling to follow what is going on...
We are using your package and would like to fetch data in
getStaticProps()
(in conjunction with the next-mdx-remote package.)I found this thread and noticed you had commented on it.
Am I right in thinking that in order to fetch data in
getStaticProps()
, we cannot use your package, and will instead have to try to copy one of the solutions in that thread?Thank you so much for any pointers and have a lovely Christmas,
Lydia
The text was updated successfully, but these errors were encountered: