Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 4c0da9a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
Contributor
|
See #1455 (comment) Let's wait for the feedback on the workerd issue fisrt. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1455
Problem
When Next.js prerenders an App Router page it builds three
ReadableStreams that it never closes:createUnclosingStreamin the app-page runtime, andcreateUnclosingPrefetchStreamplus the inlinepageDataStream(16.4+) incollect-segment-data. Next.js relies on garbage collection to reclaim them. On workerd the data they carried stays reachable, so every page prerendered at runtime (on-demand ISR and revalidation) retains about its RSC payload: ~1.4 MB per render for a 600 KB page, until the isolate reaches the memory limit. Details and measurements in #1455.Fix
A new
close-rsc-streamspatch, registered with the other Cloudflare patches increateServerBundle:app-page*.runtime.prod.js:createUnclosingStream'si<chunks.length&&c.enqueue(chunks[i++])becomesi<chunks.length?c.enqueue(chunks[i++]):c.close(), the shape ofcreateClosingStreamthat Next.js ships right next to it. All runtime variants are patched, including-experimental: React's experimental build is also used fortaint/viewTransitionwithout PPR..next/server/chunks/*.js: closescreateUnclosingPrefetchStreambefore its barereturn, and thepageDataStreamafter its lastenqueue.The rules match on the shape of the streams (minified webpack and Turbopack output, and unminified output), and each one skips a stream that already closes, so apps that patched Next.js themselves are not closed twice. The whole patch is skipped when
cacheComponents,experimental.pprorexperimental.dynamicIOis enabled, where Next.js needs the streams open.Version boundary:
createUnclosingStreamexists since 15.0.0,createUnclosingPrefetchStreamsince 15.x andpageDataStreamsince 16.4. Checked against 15.5.28, 16.4.1 and 16.5.0-canary.14.Verification
cacheComponentsskip.examples/playground16/e2e/close-rsc-streams.cloudflare.spec.tsasserts on the builthandler.mjsthat no unclosed stream is left. It passes with webpack and Turbopack, and fails on an unpatched bundle.examples/e2e/app-router(webpack): the builthandler.mjshas no unclosed form left and all three streams close.CI=true pnpm --filter app-router e2e: 70 passed, 3 skipped.examples/e2e/experimental(cacheComponents: true):handler.mjsis unchanged.pnpm code:checksandpnpm testpass.Not run: the Turbopack e2e suite (
pnpm e2e-turbopack).