Skip to content
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

disable static prefetching behavior for dynamic segments #58609

Merged

Conversation

ztanner
Copy link
Member

@ztanner ztanner commented Nov 17, 2023

What?

When a layout segment forces dynamic rendering (such as with force-dynamic or revalidate: 0), navigating to sub-pages of that layout will attempt to re-render the layout, also resulting in side effects re-running. This means if your layout relies on a data fetch and you render the result of that data in the layout, it will unexpectedly change when navigating between sub-paths, as described in #57326.

As a separate issue (but caused by the same underlying mechanism), when using searchParams on a dynamic page, changes to those search params will be erroneously ignored when navigating, as described in #57075

Why?

As a performance optimization we generate static prefetch files for dynamic segments (original PR). This makes it so that when prefetching is turned on, the prefetch can be served quickly from the edge without needing to invoke unnecessarily. We're able to eagerly serve things that can be safely prefetched. This is nice for cases where a path has a loading.js that we can eagerly render while waiting for the dynamic data to be loaded.

This causes a problem with layouts that opt into dynamic rendering: when the page loads and a prefetch is kicked off for the sub-page, it'll load the static prefetch, which won't be generated with the same router state as the dynamically rendered page. This causes a mismatch between the two trees, and when navigating within the same segment, a refetch will be added to the router because it thinks that it's navigating to a new layout.

This also causes issues for dynamic pages that use searchParams. The static prefetch will be generated without any knowledge of search params, and when the prefetch occurs, we still match to the prefetch generated without search params. This will make the router think that no change occurs, and the UI will not update to reflect the change.

How?

There's ongoing work by @acdlite to refactor the client router. Hopefully it will be easier to re-land this once that work is finished. For now, I've reverted the behavior as it doesn't seem to be worth the bugs it currently causes. I've also added tests so that when we do re-land this behavior, we can catch these subtleties.

Fixes #57326
Fixes #57075

@ztanner
Copy link
Member Author

ztanner commented Nov 17, 2023

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@ijjk
Copy link
Member

ijjk commented Nov 17, 2023

Stats from current PR

Default Build
General Overall increase ⚠️
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
buildDuration 10.3s 10.3s N/A
buildDurationCached 5.7s 6s ⚠️ +209ms
nodeModulesSize 199 MB 199 MB ⚠️ +2.22 kB
nextStartRea..uration (ms) 422ms 423ms N/A
Client Bundles (main, webpack)
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
199-HASH.js gzip 28.7 kB 28.7 kB N/A
3f784ff6-HASH.js gzip 53.3 kB 53.3 kB
494.HASH.js gzip 180 B 181 B N/A
framework-HASH.js gzip 45.2 kB 45.2 kB
main-app-HASH.js gzip 241 B 238 B N/A
main-HASH.js gzip 31.7 kB 31.7 kB N/A
webpack-HASH.js gzip 1.7 kB 1.7 kB
Overall change 100 kB 100 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
_app-HASH.js gzip 194 B 195 B N/A
_error-HASH.js gzip 182 B 181 B N/A
amp-HASH.js gzip 501 B 503 B N/A
css-HASH.js gzip 322 B 323 B N/A
dynamic-HASH.js gzip 2.5 kB 2.5 kB
edge-ssr-HASH.js gzip 253 B 255 B N/A
head-HASH.js gzip 348 B 347 B N/A
hooks-HASH.js gzip 369 B 368 B N/A
image-HASH.js gzip 4.28 kB 4.27 kB N/A
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.61 kB 2.6 kB N/A
routerDirect..HASH.js gzip 311 B 311 B
script-HASH.js gzip 384 B 383 B N/A
withRouter-HASH.js gzip 307 B 308 B N/A
1afbb74e6ecf..834.css gzip 106 B 106 B
Overall change 3.17 kB 3.17 kB
Client Build Manifests
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
_buildManifest.js gzip 484 B 483 B N/A
Overall change 0 B 0 B
Rendered Page Sizes
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
index.html gzip 527 B 528 B N/A
link.html gzip 538 B 542 B N/A
withRouter.html gzip 524 B 523 B N/A
Overall change 0 B 0 B
Edge SSR bundle Size
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
edge-ssr.js gzip 92.4 kB 92.4 kB N/A
page.js gzip 145 kB 145 kB N/A
Overall change 0 B 0 B
Middleware size
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
middleware-b..fest.js gzip 625 B 625 B
middleware-r..fest.js gzip 150 B 151 B N/A
middleware.js gzip 24.8 kB 24.8 kB N/A
edge-runtime..pack.js gzip 1.92 kB 1.92 kB
Overall change 2.55 kB 2.55 kB
Next Runtimes
vercel/next.js canary vercel/next.js 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic Change
app-page-exp...dev.js gzip 167 kB 167 kB
app-page-exp..prod.js gzip 93.2 kB 93.2 kB
app-page-tur..prod.js gzip 94 kB 94 kB
app-page-tur..prod.js gzip 88.5 kB 88.5 kB
app-page.run...dev.js gzip 137 kB 137 kB
app-page.run..prod.js gzip 87.9 kB 87.9 kB
app-route-ex...dev.js gzip 23.8 kB 23.8 kB
app-route-ex..prod.js gzip 16.4 kB 16.4 kB
app-route-tu..prod.js gzip 16.5 kB 16.5 kB
app-route-tu..prod.js gzip 16 kB 16 kB
app-route.ru...dev.js gzip 23.2 kB 23.2 kB
app-route.ru..prod.js gzip 16 kB 16 kB
pages-api-tu..prod.js gzip 9.37 kB 9.37 kB
pages-api.ru...dev.js gzip 9.64 kB 9.64 kB
pages-api.ru..prod.js gzip 9.37 kB 9.37 kB
pages-turbo...prod.js gzip 21.8 kB 21.8 kB
pages.runtim...dev.js gzip 22.5 kB 22.5 kB
pages.runtim..prod.js gzip 21.8 kB 21.8 kB
server.runti..prod.js gzip 49.1 kB 49.1 kB N/A
Overall change 874 kB 874 kB
Diff details
Diff for page.js

Diff too large to display

Diff for edge-ssr.js

Diff too large to display

Diff for server.runtime.prod.js

Diff too large to display

Commit: 37b1dce

@ijjk
Copy link
Member

ijjk commented Nov 17, 2023

Tests Passed

@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch from d98fcf7 to 15c0219 Compare November 17, 2023 22:14
@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch from 15c0219 to 154da49 Compare November 18, 2023 17:35
@ztanner ztanner changed the title fix: don't create static prefetch when using force-dynamic disable static prefetching behavior Nov 18, 2023
@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch 4 times, most recently from dbf936d to e0bbe3d Compare November 18, 2023 17:47
@ztanner ztanner marked this pull request as ready for review November 18, 2023 17:58
@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch from e0bbe3d to 381830a Compare November 18, 2023 18:14
@ztanner ztanner changed the title disable static prefetching behavior disable dynamic static prefetching behavior Nov 18, 2023
@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch from 381830a to cd4e580 Compare November 18, 2023 18:19
@ztanner ztanner changed the title disable dynamic static prefetching behavior disable static prefetching behavior for dynamic segments Nov 18, 2023
@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch from cd4e580 to 84199f8 Compare November 19, 2023 15:31
@ztanner ztanner force-pushed the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch from 84199f8 to 7c140e8 Compare November 19, 2023 15:32
@ztanner ztanner merged commit 3043fee into canary Nov 22, 2023
57 of 59 checks passed
@ztanner ztanner deleted the 11-17-fix_don_t_create_static_prefetch_when_using_force-dynamic branch November 22, 2023 00:55
@github-actions github-actions bot added the locked label Dec 7, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants