Unverified Commit 31774433 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Make Instant Navigation Testing full-page loads work when deployed (#95222)

This makes full-page loads work under the Instant Navigation Testing API
on a deployment, not just under `next dev` and `next start`. With the
API active, a full-page load (a reload, an MPA navigation, or direct URL
entry) on a deployment failed with a minified React hydration error, and
releasing the lock then hard-reloaded the page instead of resolving
client-side. Client-side navigations under the lock already worked; the
gap was full-page loads.

The API works by injecting an inline script that sets
`self.__next_instant_test`, which the client reads at `app-index` init
as its hydration source. That read only happens on a full-page load;
client-side navigations go through the segment cache and the lock and
never read it. The script was previously injected at request time
through a transform stream, which runs only when the function renders
the document. On a deployed full-page load the browser instead parses
the prebuilt static prelude served from the edge cache, which was
prerendered without the cookie and so carried no script, leaving
`self.__next_instant_test` undefined.

When the testing API is enabled (in development, or in a production
build via `experimental.exposeTestingApiInProductionBuild`), we embed
the cookie-guarded bootstrap into the prerendered prelude through
React's `bootstrapScriptContent`, so it lands in the cached static shell
and runs before the client bootstrap module. That production-build flag
is meant for protected preview environments, so a normal production
build never embeds the script, and even where it is embedded it stays
inert on requests without the instant-navigation cookie (the prelude is
shared across all requests). The same content is folded into the dynamic
render path in `renderToStream`, and the now-redundant request-time
transform is removed.
parent f15aa4e6
......@@ -80,7 +80,6 @@ import {
} from '../../lib/constants' with { 'turbopack-transition': 'next-server-utility' }
import type { CacheControl } from '../../server/lib/cache-control'
import { ENCODED_TAGS } from '../../server/stream-utils/encoded-tags' with { 'turbopack-transition': 'next-server-utility' }
import { createInstantTestScriptInsertionTransformStream } from '../../server/stream-utils/node-web-streams-helper' with { 'turbopack-transition': 'next-server-utility' }
import { sendRenderResult } from '../../server/send-payload' with { 'turbopack-transition': 'next-server-utility' }
import { NoFallbackError } from '../../shared/lib/no-fallback-error.external' with { 'turbopack-transition': 'next-server-utility' }
import { parseMaxPostponedStateSize } from '../../shared/lib/size-limit' with { 'turbopack-transition': 'next-server-utility' }
......@@ -934,6 +933,7 @@ export async function handler(
maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize
),
exposeTestingApi,
},
waitUntil: ctx.waitUntil,
......@@ -1875,12 +1875,13 @@ export async function handler(
// This is a request for HTML data.
const body = cachedData.html
// Instant Navigation Testing API: serve the static shell with an
// injected script that sets self.__next_instant_test and kicks off a
// static RSC fetch for hydration. The transform stream also appends
// closing </body></html> tags so the browser can parse the full document.
// In dev mode, also inject self.__next_r so the HMR WebSocket and
// debug channel can initialize.
// Instant Navigation Testing API: serve the static shell without resuming
// the dynamic render. The cookie-guarded bootstrap that sets
// self.__next_instant_test is embedded in the prerendered shell via
// `bootstrapScriptContent`, so it is already present (in the served shell
// for a fresh render, or in the cached prelude on deploy). Since the
// render is not resumed, append the closing tags so the browser can parse
// a complete document.
if (isInstantNavigationTest && isDebugStaticShell) {
// If the static shell came back empty, the page reads a dynamic value
// (e.g. `await cookies()`) at the root with no Suspense boundary above
......@@ -1901,12 +1902,13 @@ export async function handler(
)
}
const instantTestRequestId =
routeModule.isDev === true ? crypto.randomUUID() : null
body.pipeThrough(
await createInstantTestScriptInsertionTransformStream(
instantTestRequestId
)
body.push(
new ReadableStream({
start(controller) {
controller.enqueue(ENCODED_TAGS.CLOSED.BODY_AND_HTML)
controller.close()
},
})
)
return sendRenderResult({
req,
......
......@@ -184,6 +184,9 @@ async function requestHandler(
maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize
),
exposeTestingApi:
pageRouteModule.isDev === true ||
nextConfig.experimental.exposeTestingApiInProductionBuild === true,
},
incrementalCache: await pageRouteModule.getIncrementalCache(
......
......@@ -523,6 +523,8 @@ async function exportAppImpl(
maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize
),
exposeTestingApi:
nextConfig.experimental.exposeTestingApiInProductionBuild === true,
},
reactMaxHeadersLength: nextConfig.reactMaxHeadersLength,
}
......
......@@ -65,6 +65,7 @@ import {
createNodeInlinedDataStream,
} from './stream-ops'
import type { AnyStream } from './stream-ops'
import { getInstantTestBootstrapScriptContent } from './instant-test-bootstrap'
import { stripInternalQueries } from '../internal-utils'
import {
NEXT_HMR_REFRESH_HEADER,
......@@ -3307,10 +3308,20 @@ async function renderToStream(
// bootstrap script is executed, which depends on it during hydration.
// For MPA navigations (page reload, direct URL entry), the request ID
// header is not present, so we generate a random one.
const bootstrapScriptContent = process.env.__NEXT_DEV_SERVER
let bootstrapScriptContent = process.env.__NEXT_DEV_SERVER
? `self.__next_r=${JSON.stringify(requestId ?? crypto.randomUUID())}`
: undefined
// Instant Navigation Testing API: embed the cookie-guarded bootstrap so it
// runs before the client bootstrap module reads self.__next_instant_test as
// its hydration source. This mirrors the prerender path so a dynamically
// rendered document carries the same script as the cached static prelude.
if (ctx.renderOpts.experimental.exposeTestingApi) {
bootstrapScriptContent =
(bootstrapScriptContent ? `${bootstrapScriptContent};` : '') +
(await getInstantTestBootstrapScriptContent())
}
// Create the "render route (app)" span manually so we can keep it open during streaming.
// This is necessary because errors inside Suspense boundaries are reported asynchronously
// during stream consumption, after a typical wrapped function would have ended the span.
......@@ -7617,6 +7628,25 @@ async function prerenderToStream(
page
)
// Instant Navigation Testing API: when exposed, embed the cookie-guarded
// bootstrap into the prerendered prelude so the cached static shell carries
// it and it runs before the client bootstrap module reads
// self.__next_instant_test.
let bootstrapScriptContent = renderOpts.experimental.exposeTestingApi
? await getInstantTestBootstrapScriptContent()
: undefined
// In development the static shell is served without a dynamic resume, so it
// must carry the debug-channel request id (self.__next_r) itself for
// app-index to initialize the HMR/debug channel. renderToStream provides this
// for dynamic renders; prepend it here so it runs before the bootstrap
// module.
if (process.env.__NEXT_DEV_SERVER && bootstrapScriptContent) {
bootstrapScriptContent =
`self.__next_r=${JSON.stringify(ctx.requestId ?? crypto.randomUUID())};` +
bootstrapScriptContent
}
const { reactServerErrorsByDigest } = workStore
// We don't report errors during prerendering through our instrumentation hooks
const reportErrors = !experimental.isRoutePPREnabled
......@@ -8415,6 +8445,7 @@ async function prerenderToStream(
},
onHeaders: finalClientOnHeaders,
maxHeadersLength: reactMaxHeadersLength,
bootstrapScriptContent,
bootstrapScripts: [bootstrapScript],
}
)
......@@ -8635,6 +8666,7 @@ async function prerenderToStream(
onError: htmlRendererErrorHandler,
onHeaders: pprOnHeaders,
maxHeadersLength: reactMaxHeadersLength,
bootstrapScriptContent,
bootstrapScripts: [bootstrapScript],
}
)
......@@ -8857,6 +8889,7 @@ async function prerenderToStream(
{
onError: htmlRendererErrorHandler,
nonce,
bootstrapScriptContent,
bootstrapScripts: [bootstrapScript],
},
{ waitForAllReady: true }
......
import {
RSC_HEADER,
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
NEXT_RSC_UNION_QUERY,
NEXT_INSTANT_TEST_COOKIE,
} from '../../client/components/app-router-headers'
import { computeCacheBustingSearchParam } from '../../shared/lib/router/utils/cache-busting-search-param'
/**
* Builds the inline bootstrap script content for the Instant Navigation Testing
* API. It is embedded via React's `bootstrapScriptContent` into the prerendered
* shell (and dynamic renders) so that it ends up in the cached static prelude
* and runs before the client bootstrap module (app-index) reads
* self.__next_instant_test as its hydration source.
*
* The script is cookie-guarded so it is inert unless the instant test cookie is
* present (the prelude is shared across all requests). The fetch mirrors the
* __NEXT_CLIENT_RESUME prefetch used for fallback routes; the instant test
* cookie rides along on the same-origin request, so the server returns
* static-only data, which doubles as the feature flag (truthy = instant mode).
*/
export async function getInstantTestBootstrapScriptContent(): Promise<string> {
const segmentPath = '/_full'
const cacheBustingHeader = await computeCacheBustingSearchParam(
'1',
segmentPath,
undefined,
undefined
)
const searchStr = `${NEXT_RSC_UNION_QUERY}=${cacheBustingHeader}`
return `if(document.cookie.indexOf('${NEXT_INSTANT_TEST_COOKIE}=')>-1){self.__next_instant_test=fetch(location.pathname+'?${searchStr}',{credentials:'same-origin',headers:{'${RSC_HEADER}':'1','${NEXT_ROUTER_PREFETCH_HEADER}':'1','${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}':'${segmentPath}'}})}`
}
......@@ -178,6 +178,14 @@ export interface RenderOptsPartial {
* requests. Used to calculate decompression limits (5x this value).
*/
maxPostponedStateSizeBytes: number | undefined
/**
* Whether the Instant Navigation Testing API is exposed (dev mode or the
* `exposeTestingApiInProductionBuild` flag). When true, the prerendered
* shell and dynamic renders embed a cookie-guarded bootstrap script that
* drives instant navigation tests.
*/
exposeTestingApi: boolean
}
postponed?: string
......
......@@ -592,6 +592,10 @@ export default abstract class Server<
maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
this.nextConfig.experimental.maxPostponedStateSize
),
exposeTestingApi:
this.dev === true ||
this.nextConfig.experimental.exposeTestingApiInProductionBuild ===
true,
},
onInstrumentationRequestError:
this.instrumentationOnRequestError.bind(this),
......
......@@ -585,92 +585,6 @@ async function createClientResumeScriptInsertionTransformStream(): Promise<
})
}
/**
* Creates a transform stream that injects an inline script as the first
* element inside <head>. Used during instant navigation testing to set
* self.__next_instant_test before any async bootstrap scripts execute.
*/
export async function createInstantTestScriptInsertionTransformStream(
requestId: string | null
): Promise<TransformStream<Uint8Array, Uint8Array>> {
// Kick off a fetch for the static RSC payload. This is the hydration
// source for the locked static shell — same as the __NEXT_CLIENT_RESUME
// fetch used for fallback routes. The instant test cookie is sent
// automatically with the same-origin fetch, so the server returns
// static-only data.
//
// The fetch promise is stored as self.__next_instant_test, which doubles
// as the feature flag (truthy = instant test mode). The client processes
// this as a fallback prerender payload for hydration.
const segmentPath = '/_full'
const cacheBustingHeader = await computeCacheBustingSearchParam(
'1',
segmentPath,
undefined,
undefined
)
const searchStr = `${NEXT_RSC_UNION_QUERY}=${cacheBustingHeader}`
// In dev mode, inject self.__next_r (request ID) so that HMR WebSocket
// and debug channel initialization don't crash. The static shell
// bypasses renderToFizzStream which normally injects this via
// bootstrapScriptContent.
const requestIdScript =
requestId !== null ? `self.__next_r=${JSON.stringify(requestId)};` : ''
const INSTANT_TEST_SCRIPT = `<script>${requestIdScript}self.__next_instant_test=fetch(location.pathname+'?${searchStr}',{credentials:'same-origin',headers:{'${RSC_HEADER}':'1','${NEXT_ROUTER_PREFETCH_HEADER}':'1','${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}':'${segmentPath}'}})</script>`
let didAlreadyInsert = false
return new TransformStream({
transform(chunk, controller) {
if (didAlreadyInsert) {
// Already inserted the script into the head. Pass through.
controller.enqueue(chunk)
return
}
// Find the opening <head tag (may have attributes like <head class="...">)
const headOpenIndex = indexOfUint8Array(chunk, ENCODED_TAGS.OPENING.HEAD)
if (headOpenIndex === -1) {
controller.enqueue(chunk)
return
}
// Find the closing > of the <head ...> tag
const headCloseAngle = chunk.indexOf(
62, // '>'
headOpenIndex + ENCODED_TAGS.OPENING.HEAD.length
)
if (headCloseAngle === -1) {
controller.enqueue(chunk)
return
}
const encodedInsertion = encoder.encode(INSTANT_TEST_SCRIPT)
const insertionPoint = headCloseAngle + 1
// e.g.
// chunk = <!DOCTYPE html><html><head><meta charset="utf-8">...
// insertion = <script>self.__next_instant_test=fetch(...)</script>
// output = <!DOCTYPE html><html><head> [ <script>...</script> ] <meta charset="utf-8">...
const insertedHeadContent = new Uint8Array(
chunk.length + encodedInsertion.length
)
insertedHeadContent.set(chunk.slice(0, insertionPoint))
insertedHeadContent.set(encodedInsertion, insertionPoint)
insertedHeadContent.set(
chunk.slice(insertionPoint),
insertionPoint + encodedInsertion.length
)
controller.enqueue(insertedHeadContent)
didAlreadyInsert = true
},
flush(controller) {
// Append closing tags so the browser can parse the full document.
controller.enqueue(ENCODED_TAGS.CLOSED.BODY_AND_HTML)
},
})
}
// Suffix after main body content - scripts before </body>,
// but wait for the major chunks to be enqueued.
export function createDeferredSuffixStream(
......
......@@ -95,23 +95,12 @@ afterEach(async () => {
// surfaced). Development legitimately logs warnings here (such as a
// blocking-route prerender insight), so only assert in production.
if (!isNextDev) {
// TODO: Hydration is currently broken on deploy, so skip this check until
// it is fixed.
if (!isNextDeploy) {
await assertNoConsoleErrors(activeBrowser)
}
await assertNoConsoleErrors(activeBrowser)
}
activeBrowser = undefined
}
})
// The Instant Navigation lock re-engages on a freshly loaded document via the
// prerendered-shell bootstrap (`self.__next_instant_test`). That bootstrap is
// embedded into the prelude in #95222; until then it is missing from the
// deploy-served document, so any test that drives a full-page load under the
// lock (a plain-anchor MPA navigation or a reload) engages the lock unreliably
// on deploy. We skip those in deploy mode and un-skip them in #95222 by turning
// this back into a plain `it`.
const itSkipDeploy = isNextDeploy ? it.skip : it
describe('instant-navigation-testing-api', () => {
......@@ -351,8 +340,7 @@ describe('instant-navigation-testing-api', () => {
}
})
// prettier-ignore
itSkipDeploy('renders shell on page reload', async () => {
it('renders shell on page reload', async () => {
const page = await openPage(next, '/target-page')
// Wait for the page to fully load with dynamic content
......@@ -394,8 +382,7 @@ describe('instant-navigation-testing-api', () => {
expect(navigationRequests.length).toBe(navigationsAtUnlock)
})
// prettier-ignore
itSkipDeploy('renders shell on MPA navigation via plain anchor', async () => {
it('renders shell on MPA navigation via plain anchor', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -422,8 +409,7 @@ describe('instant-navigation-testing-api', () => {
)
})
// prettier-ignore
itSkipDeploy('reload followed by MPA navigation, both block dynamic data', async () => {
it('reload followed by MPA navigation, both block dynamic data', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -454,7 +440,7 @@ describe('instant-navigation-testing-api', () => {
)
})
itSkipDeploy('successive MPA navigations within instant scope', async () => {
it('successive MPA navigations within instant scope', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -565,8 +551,7 @@ describe('instant-navigation-testing-api', () => {
expect(await cookieValue.textContent()).toContain('testCookie: hello')
})
// prettier-ignore
itSkipDeploy('does not include cookie values in instant shell during page load', async () => {
it('does not include cookie values in instant shell during page load', async () => {
const page = await openPage(next, '/', {
cookies: [{ name: 'testCookie', value: 'hello' }],
})
......@@ -631,8 +616,7 @@ describe('instant-navigation-testing-api', () => {
expect(await searchParamContent.textContent()).toContain('foo: bar')
})
// prettier-ignore
itSkipDeploy('does not include search param values in instant shell during page load', async () => {
it('does not include search param values in instant shell during page load', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -684,8 +668,7 @@ describe('instant-navigation-testing-api', () => {
})
})
// prettier-ignore
itSkipDeploy('includes statically generated param values in instant shell during page load', async () => {
it('includes statically generated param values in instant shell during page load', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -741,7 +724,7 @@ describe('instant-navigation-testing-api', () => {
expect(await paramValue.textContent()).toContain('slug: anything')
})
itSkipDeploy('during page load', async () => {
it('during page load', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -923,8 +906,7 @@ describe('instant-navigation-testing-api', () => {
).toContain('slug: anything')
})
// prettier-ignore
itSkipDeploy('subsequent navigations after instant scope are not locked', async () => {
it('subsequent navigations after instant scope are not locked', async () => {
const page = await openPage(next, '/')
// First, do an MPA navigation within an instant scope
......@@ -1054,7 +1036,7 @@ describe('instant-navigation-testing-api', () => {
}
})
itSkipDeploy('clears cookie after instant scope exits', async () => {
it('clears cookie after instant scope exits', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -1096,8 +1078,7 @@ describe('instant-navigation-testing-api', () => {
expect(await fetchedData.textContent()).toContain('api response')
})
// prettier-ignore
itSkipDeploy('blocks out-of-band client fetch during instant scope (MPA)', async () => {
it('blocks out-of-band client fetch during instant scope (MPA)', async () => {
const page = await openPage(next, '/')
await instant(page, async () => {
......@@ -1180,7 +1161,7 @@ describe('instant-navigation-testing-api - root params', () => {
files: join(__dirname, 'fixtures', 'root-params'),
})
itSkipDeploy('includes root param in instant shell', async () => {
it('includes root param in instant shell', async () => {
const page = await openPage(next, '/en')
const langValue = page.locator('[data-testid="lang-value"]')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment