-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Add prefetch to new router #39866
Merged
Merged
Add prefetch to new router #39866
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adds the initial event for prefetching. Implementation is WIP.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
huozhi
reviewed
Sep 6, 2022
Co-authored-by: Jiachi Liu <[email protected]>
ijjk
approved these changes
Sep 6, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Follow-up to #37551
Implements prefetching for the new router.
There are multiple behaviors related to prefetching so I've split them out for each case. The list below each case is what's prefetched:
Reference:
loading.js
static case → Will be handled in a follow-up PR.prefetch={true}
(default, same as current router, links in viewport are prefetched)loading.js
loading.js
(This case can be static files to make sure it’s fast)prefetch={false}
In the first implementation there is a distinction between
hard
andsoft
navigation. With the addition of prefetching you no longer have to add asoft
prop tonext/link
in order to leverage thesoft
case.A heuristic has been added that automatically prefers
soft
navigation except when navigating between mismatching dynamic parameters.An example:
app/[userOrTeam]/dashboard/page.js
andapp/[userOrTeam]/dashboard/settings/page.js
/tim/dashboard
→/tim/dashboard/settings
= Soft navigation/tim/dashboard
→/vercel/dashboard
= Hard navigation/vercel/dashboard
→/vercel/dashboard/settings
= Soft navigation/vercel/dashboard/settings
->/tim/dashboard
= Hard navigationWhile adding these new heuristics some of the tests started failing and I found some state bugs in
router.reload()
which have been fixed. An example being when you push to/dashboard
while on/
in the same transition it would navigate to/
, it also wouldn't push a new history entry. Both of these cases are now fixed:While debugging the various changes I ended up debugging and manually diffing the cache and router state quite often and was looking at a way to automate this.
useReducer
is quite similar to Redux so I was wondering if Redux Devtools could be used in order to debug the various actions as it has diffing built-in. It took a bit of time to figure out the connection mechanism but in the end I figured out how to connectuseReducer
, a new hookuseReducerWithReduxDevtools
has been added, we'll probably want to put this behind a compile-time flag when the new router is marked stable but until then it's useful to have it enabled by default (only when you have Redux Devtools installed ofcourse).Example of the integration: