next.js: next/prefetch doesn't prefetch data
It is my understanding that replacing an import 'next/link' with import next/prefetch' should “just work”, however I don’t see this behaviour in my app. It doesn’t use dynamic routes, and just has three links in its header which should be prefetched.
Locally, I see three pages in the cache with an “OK” response, which seems to be the desired behaviour:
When deployed with Now, I see the three pages in the cache, but there’s no OK response (even when waiting several minutes):
But in both cases (local and deployed), I still see a “Loading…” indicator when I then click on the Discover link (and in general “feel slow”, compared to when I return to the page a second time).
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 36 (30 by maintainers)
@sedubois If we decide to do it, it should be explicit. For example:
Otherwise it won’t scale
I think we should write more about how Next.js works internally 😃
@arunoda I still think prefetch should call getInitialProps. Even if the data changes meanwhile, it’s a great “best guess” to instantly-render the page. And as soon as we navigate to the page, the data will be refreshed anyway because getInitialProps will be called again! Wouldn’t that be the best of both worlds?
Thanks @arunoda! We just published this to npm: data-prefetch-link.
It would be nice to bring this concept into core support now that it has been battle tested and matured for a while. I worry many new and existing Next.js users don’t know that data-prefetch-link even exists. Having the “withData” option in the documentation would make it much more likely that the community would benefit from this brilliant concept.
FYI, Gatsby 1.0 prefetches both code and data (ref). IMHO Next.js should too. As a user, I’d very much want that. I don’t agree that Next.js “knows nothing about the data in your app”, it actually explicitly created an API for it (
getInitialProps). It can prefetch data, then refresh it when navigating to the page. (It could even in the longer term refresh its cache periodically, but that’s getting more fancy.)Data Prefetching
I think Next.js is about the app infrastructure, we always want user to handle the data. Relay, Apollo and others should take care of the data logic. We know nothing about the data in your app, so we can’t prefetch.
You might say, we could just call
getInitialProps()and cache it.That’s wrong. Data could change time to time. So, doing that is wrong since we don’t know nothing about data.
What you can do is, rather using
next/prefetchas a<Link/>, use our imperative API to prefetch JS code. And then use Apollo or other data layer to prefetch data.I think having an example would be nice too.
@rauchg @nkzawa @impronunciable could you guys add something here. May be as @sedubois suggested, we should add a note about prefetching section, that’s this is not for data.
Going to close this issue. Core support is not needed thanks to @gmaliandi’s brilliant work 👍
@ads1018 your app works fine. It just doing prefetching well. (As I said earlier, we only prefetch JS code related the page, not any data)
If data prefetching needed, you need to do it yourself. We won’t touch anything related to data.
Yes I think it would make sense to prefetch whatever kind of page there is. And if that’s not possible for some reason the doc should make it clear.
I think this prefetch is a very important feature which would also open the way to offer some kind of offline mode and low-bandwidth friendly experience; it’s also essential to the experience of progressive web apps I guess. So I’m very much looking forward to adding prefetching to my app 😃 especially as im working on a mindfulness app, where people will for sure want to be able to use airplane mode to reduce their distractions (and also to download the audio/video contents in advance for a smoother and more mobile experience).
Ah ok good to know. This would be a good disclaimer to add to the docs. It would be a very useful/cool/powerful feature to enable optional prefetching of data as well.
@ads1018 prefetch only does JSON page prefetching.(Basically the JS code) Not data. So, in this case loading indicator is usual I guess.
I’ll check that app too.
I can confirm this issue in my next apollo example as well. If you load the
aboutpage in my demo first and then navigate to the home page you’ll notice a loading indicator which means that the whole page isn’t getting prefetched. @sedubois perhaps it’s related to using a HOC in our pages to fetch our data? Does prefetch not like that?@sedubois are you using
as?@gmaliandi
That’s a pretty impressive way of using
Router.prefetch.Hi! We just blogged about the approach I mentioned above. Hope this helps!
Fortunately since next.js no longer uses
evalfor fetching pages, it also no longer returns the source code as a string when prefetching programmatically, but rather returns the component constructor, so prefetching the initial props of a page no longer requiresevaleither!Maybe this code can help for Apollo case:
https://github.com/estrattonbailey/apollo-prefetch
Indeed, it would be a great feature to explore. I created an issue for this specific feature - see #1175
@gmaliandi using eval doesn’t seem so fine, see https://github.com/zeit/next.js/issues/1301
I agree that prefetching the page then pre-rendering its HTML would be a great feature to explore.