-
Notifications
You must be signed in to change notification settings - Fork 30k
Description
Describe the feature you'd like to request
In the documentation it is said that the conditions for hard navigation are :
- when navigating between dynamic segments
- When navigating between two different group layouts (ex: from
(groupA)/layoutto(groupB)/layout)
I'd like to suggest also adding hard navigation for segments marked with dynamic='force-dynamic' or when using dynamic functions and even when using fetch with cache: 'no-store'.
In the docs you said that using these configurations is like using getServerSideProps() in the pages directory, but it does not behave the same between navigations which is quite confusing.
Use cases for this feature could be these :
- this app that only store pokemon data in cookies (Live here and source here)
- this simple case for an app that generate a random number each time : ckblitz.com/edit/nextjs-sxijav?file=package.json,app%2Fnested%2Fpage.tsx
Describe the solution you'd like
The solution i propose is to consider hard navigation for these cases :
-
When navigating to a page marked with
dynamic='force-dynamic', next should always do a hard navigation -
When navigating to a page using dynamic functions
headers()andcookies(), next should always do a hard navigation -
When navigating to a page using
fetchwithcache: 'no-store', next should always do a hard navigation, or at least next should always refetch the data -
When navigating to a page using either
fetchwithnext: { revalidate: n_seconds }orexport const revalidate = n_seconds, next should only do hard navigation when then_secondshas elapsed.
The last two could be tricky and if it is not ideal, at least add a paragraph in the doc explaining why it is not possible and maybe recommending the first two approaches.
Describe alternatives you've considered
Updates
-
In
[email protected], using a<Link prefetch={false} />seems to fix the problem : https://stackblitz.com/edit/vercel-next-js-n1tqpr?file=app%2Flayout.tsx
. But it disables link prefetching and result in slower navigations. -
As of
[email protected]the client side cache has been reworked, dynamic pages are now cached in the client with a timer of30 seconds, so every 30 seconds your server is recalled, with one catch : this only apply if you navigates to a different page after that time, if you do very fast back & forth to the same page, the timer will reset and only wait for another 30 seconds. Disabling prefetch does not solve the problem anymore, it disables prefetching, but once the page has been navigated to, the rules of 30 seconds still applies. If you want the old behaviour, you'd have to downgrade to[email protected]for now, You will loose support for Server Actions & revalidate primitives though, Or you can also not use the Link component and use regular a tags if you still want the new features of Next.
However there are things in the work for both allowing prefetching on hover (with <Link>) and allowing for setting the stale time on the client side cache. You can read in the PR about the client side rework :
Follow ups
- we may add another API to control the cache TTL at the page level
- a way to opt-in for prefetch on hover even with prefetch={false}
This would allow for setting a pageTTL = 0 for calling the server on each & every navigation to always have fresh data.