Skip to content

Commit

Permalink
[ppr] Improve experimental debugging (#76016)
Browse files Browse the repository at this point in the history
This fixes a bug with the experimental PPR debugging where it previously
always failed in production because the hydration script was always
invoked even when the stream was terminated early due to the debugging
state.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
  • Loading branch information
wyattjoh authored Feb 13, 2025
1 parent 9f89c65 commit 9cfc797
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 44 deletions.
4 changes: 4 additions & 0 deletions packages/next/src/build/webpack/plugins/define-env-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export function getDefineEnv({
'process.env.__NEXT_DYNAMIC_IO': isDynamicIOEnabled,
'process.env.__NEXT_USE_CACHE': isUseCacheEnabled,
'process.env.NEXT_DEPLOYMENT_ID': config.deploymentId || false,
// Propagates the `__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING` environment
// variable to the client.
'process.env.__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING':
process.env.__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING || false,
'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX': fetchCacheKeyPrefix ?? '',
...(isTurbopack
? {}
Expand Down
21 changes: 19 additions & 2 deletions packages/next/src/client/app-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,25 @@ function loadScriptsInSequence(
})
}

export function appBootstrap(callback: () => void) {
export function appBootstrap(hydrate: () => void) {
loadScriptsInSequence((self as any).__next_s, () => {
callback()
// If the static shell is being debugged, skip hydration if the
// `__nextppronly` query is present. This is only enabled when the
// environment variable `__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING` is
// set to `1`. Otherwise the following is optimized out.
if (process.env.__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING === '1') {
const search = new URLSearchParams(window.location.search)
if (
search.get('__nextppronly') === 'fallback' ||
search.get('__nextppronly') === '1'
) {
console.warn(
`Skipping hydration due to __nextppronly=${search.get('__nextppronly')}`
)
return
}
}

hydrate()
})
}
42 changes: 7 additions & 35 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1932,11 +1932,7 @@ async function renderToStream(
})
},
maxHeadersLength: renderOpts.reactMaxHeadersLength,
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
formState,
}
)
Expand Down Expand Up @@ -2825,11 +2821,7 @@ async function prerenderToStream(
)
}
},
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
),
() => {
Expand Down Expand Up @@ -2992,11 +2984,7 @@ async function prerenderToStream(
})
},
maxHeadersLength: renderOpts.reactMaxHeadersLength,
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
),
() => {
Expand Down Expand Up @@ -3299,11 +3287,7 @@ async function prerenderToStream(
)
}
},
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
)
pendingInitialClientResult.catch((err: unknown) => {
Expand Down Expand Up @@ -3457,11 +3441,7 @@ async function prerenderToStream(

return htmlRendererErrorHandler(err, errorInfo)
},
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
),
() => {
Expand Down Expand Up @@ -3620,11 +3600,7 @@ async function prerenderToStream(
})
},
maxHeadersLength: renderOpts.reactMaxHeadersLength,
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
)
const getServerInsertedHTML = makeGetServerInsertedHTML({
Expand Down Expand Up @@ -3829,11 +3805,7 @@ async function prerenderToStream(
{
onError: htmlRendererErrorHandler,
nonce: ctx.nonce,
// When debugging the static shell, client-side rendering should be
// disabled to prevent blanking out the page.
bootstrapScripts: renderOpts.isDebugStaticShell
? []
: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
)

Expand Down
6 changes: 0 additions & 6 deletions packages/next/src/server/app-render/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,6 @@ export interface RenderOptsPartial {
*/
devRenderResumeDataCache?: RenderResumeDataCache

/**
* When true, only the static shell of the page will be rendered. This will
* also enable other debugging features such as logging in development.
*/
isDebugStaticShell?: boolean

/**
* When true, the page will be rendered using the static rendering to detect
* any dynamic API's that would have stopped the page from being fully
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,6 @@ export default abstract class Server<
renderOpts.supportsDynamicResponse = false
renderOpts.isStaticGeneration = true
renderOpts.isRevalidate = true
renderOpts.isDebugStaticShell = isDebugStaticShell
renderOpts.isDebugDynamicAccesses = isDebugDynamicAccesses
}

Expand Down

0 comments on commit 9cfc797

Please sign in to comment.