Unverified Commit 0d5f6a7e authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[PPF] Log Sync IO errors in runtime prerenders (#99688)

Closes NAR-959

Sync IO can deopt the result of a runtime prerender, both in a runtime
request and in the embedded runtime prefetch stream.

Long ago, we decided to not log these errors, with the motivation that:
1. The user didn't opt into the runtime prefetch stream
2. They are not reproducible in dev

However:
- 1 is now no longer invalid, because the runtime prefetch stream is
only enabled in PPF, which _is_ an opt in.
- 2 is no longer valid, because opening the page in dev reliably
reproduces sync IO errors in cacheable content (see
[tests](https://github.com/vercel/next.js/blob/41215d77762375b4b5154b12cfbb4372a835f8a0/test/e2e/app-dir/instant-validation/sync-io-and-blocking.util.ts#L21))

...so we can now start logging the errors again. This helps the user
notice that the deopt is occurring, which they can then go reproduce in
dev.
parent 5dc4b954
...@@ -1883,6 +1883,17 @@ async function finalRuntimeServerPrerender( ...@@ -1883,6 +1883,17 @@ async function finalRuntimeServerPrerender(
const onUnexpectedAbort = () => { const onUnexpectedAbort = () => {
resultIsPartial = true resultIsPartial = true
const syncInterruptReason = serverDynamicTracking.syncDynamicErrorWithStack
if (syncInterruptReason) {
console.error(syncInterruptReason)
} else {
console.error(
new InvariantError(
'A runtime prerender was aborted synchronously, but Next.js could not determine the reason.'
)
)
}
// FIXME(NAR-810): If we're already aborted due to Sync IO, there should be no need to // FIXME(NAR-810): If we're already aborted due to Sync IO, there should be no need to
// finish the accumulators. However, it seems like in `--debug-prerender` // finish the accumulators. However, it seems like in `--debug-prerender`
// the stream will stay open if we don't close the iterable here. // the stream will stay open if we don't close the iterable here.
......
...@@ -27,6 +27,7 @@ export function io(expression: string, type: SyncIOApiType) { ...@@ -27,6 +27,7 @@ export function io(expression: string, type: SyncIOApiType) {
if (prerenderSignal.aborted === false) { if (prerenderSignal.aborted === false) {
// If the prerender signal is already aborted we don't need to construct // If the prerender signal is already aborted we don't need to construct
// any stacks because something else actually terminated the prerender. // any stacks because something else actually terminated the prerender.
// TODO: synchronize this with `stageController`
abortOnSynchronousPlatformIOAccess( abortOnSynchronousPlatformIOAccess(
workStore.route, workStore.route,
expression, expression,
......
...@@ -16,7 +16,7 @@ export default function Page() { ...@@ -16,7 +16,7 @@ export default function Page() {
<LinkAccordion href="/uncached-time" prefetch={false}> <LinkAccordion href="/uncached-time" prefetch={false}>
Target Target
</LinkAccordion> </LinkAccordion>
<LinkAccordion href="/uncached-time#full-prefetch" prefetch={true}> <LinkAccordion href="/uncached-time#prefetch-true" prefetch={true}>
Full prefetch Full prefetch
</LinkAccordion> </LinkAccordion>
<LinkAccordion href="/delayed-sync-io" prefetch={false}> <LinkAccordion href="/delayed-sync-io" prefetch={false}>
......
process.env.__NEXT_PARTIAL_PREFETCHING = 'true'
require('./cached-navigations-sync-io.test')
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright' import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act' import { createRouterAct } from 'router-act'
import { gate } from '../../../lib/next-test-utils'
// @force-gate prod // @force-gate prod
describe('cached-navigations-sync-io', () => { describe('cached-navigations-sync-io', () => {
...@@ -50,6 +51,7 @@ describe('cached-navigations-sync-io', () => { ...@@ -50,6 +51,7 @@ describe('cached-navigations-sync-io', () => {
]) { ]) {
for (const source of ['dynamic RSC', 'initial HTML']) { for (const source of ['dynamic RSC', 'initial HTML']) {
it(`${description} from ${source}`, async () => { it(`${description} from ${source}`, async () => {
const getCliOutput = next.getCliOutputFromHere()
const { browser, page, act, startDate, navigate } = await startBrowser() const { browser, page, act, startDate, navigate } = await startBrowser()
if (source === 'dynamic RSC') { if (source === 'dynamic RSC') {
...@@ -62,6 +64,19 @@ describe('cached-navigations-sync-io', () => { ...@@ -62,6 +64,19 @@ describe('cached-navigations-sync-io', () => {
expect(await response?.text()).toContain('Uncached time:') expect(await response?.text()).toContain('Uncached time:')
} }
// - In PPF, we should log a sync IO error.
// - Before PPF, we should not log anything, because Cached Navigations
// are an extra thing that the user did not opt into
const syncIOErrorText = `Route "${pathname}": Next.js encountered the unstable value \`Date.now()\` while prerendering`
// No CLI logs in deploy
if (!(await gate('deploy'))) {
if (await gate('partialPrefetchingGlobal')) {
expect(getCliOutput()).toInclude(syncIOErrorText)
} else {
expect(getCliOutput()).not.toInclude(syncIOErrorText)
}
}
const timestamp = await browser.elementById('timestamp').text() const timestamp = await browser.elementById('timestamp').text()
expect(timestamp).toMatch(/^Uncached time: \d+$/) expect(timestamp).toMatch(/^Uncached time: \d+$/)
await browser.eval('window.navigationMarker = "same document"') await browser.eval('window.navigationMarker = "same document"')
...@@ -100,6 +115,8 @@ describe('cached-navigations-sync-io', () => { ...@@ -100,6 +115,8 @@ describe('cached-navigations-sync-io', () => {
} }
} }
// In Partial Prefetching, `prefetch={true}` is no longer a full prefetch that includes uncached data.
// @gate !partialPrefetchingGlobal
it('finishes a full prefetch after synchronous IO interrupts its static stage', async () => { it('finishes a full prefetch after synchronous IO interrupts its static stage', async () => {
const { browser, act, navigate } = await startBrowser() const { browser, act, navigate } = await startBrowser()
await act(() => navigate('/uncached-time'), { includes: 'Dynamic content' }) await act(() => navigate('/uncached-time'), { includes: 'Dynamic content' })
...@@ -117,7 +134,7 @@ describe('cached-navigations-sync-io', () => { ...@@ -117,7 +134,7 @@ describe('cached-navigations-sync-io', () => {
// Refresh clears segment data and BFCache but retains the route tree. The // Refresh clears segment data and BFCache but retains the route tree. The
// full prefetch must fetch new data without a cold static tree prerender. // full prefetch must fetch new data without a cold static tree prerender.
const href = '/uncached-time#full-prefetch' const href = '/uncached-time#prefetch-true'
await act( await act(
async () => { async () => {
await browser await browser
......
...@@ -2,7 +2,8 @@ import type { NextConfig } from 'next' ...@@ -2,7 +2,8 @@ import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
cacheComponents: true, cacheComponents: true,
partialPrefetching: false, // PPF is controlled by the test variant
// partialPrefetching: ...,
experimental: { experimental: {
cachedNavigations: true, cachedNavigations: true,
}, },
......
...@@ -1193,31 +1193,38 @@ describe('runtime prefetching', () => { ...@@ -1193,31 +1193,38 @@ describe('runtime prefetching', () => {
{ {
description: 'when sync IO is used after awaiting cookies()', description: 'when sync IO is used after awaiting cookies()',
path: '/errors/sync-io-after-runtime-api/cookies', path: '/errors/sync-io-after-runtime-api/cookies',
route: '/errors/sync-io-after-runtime-api/cookies',
}, },
{ {
description: 'when sync IO is used after awaiting headers()', description: 'when sync IO is used after awaiting headers()',
path: '/errors/sync-io-after-runtime-api/headers', path: '/errors/sync-io-after-runtime-api/headers',
route: '/errors/sync-io-after-runtime-api/headers',
}, },
{ {
description: 'when sync IO is used after awaiting dynamic params', description: 'when sync IO is used after awaiting dynamic params',
path: '/errors/sync-io-after-runtime-api/dynamic-params/123', path: '/errors/sync-io-after-runtime-api/dynamic-params/123',
route: '/errors/sync-io-after-runtime-api/dynamic-params/[id]',
}, },
{ {
description: 'when sync IO is used after awaiting searchParams', description: 'when sync IO is used after awaiting searchParams',
path: '/errors/sync-io-after-runtime-api/search-params?foo=bar', path: '/errors/sync-io-after-runtime-api/search-params?foo=bar',
route: '/errors/sync-io-after-runtime-api/search-params',
}, },
{ {
description: 'when sync IO is used after awaiting a private cache', description: 'when sync IO is used after awaiting a private cache',
path: '/errors/sync-io-after-runtime-api/private-cache', path: '/errors/sync-io-after-runtime-api/private-cache',
route: '/errors/sync-io-after-runtime-api/private-cache',
}, },
{ {
description: description:
'when sync IO is used after awaiting a quickly-expiring public cache', 'when sync IO is used after awaiting a quickly-expiring public cache',
path: '/errors/sync-io-after-runtime-api/quickly-expiring-public-cache', path: '/errors/sync-io-after-runtime-api/quickly-expiring-public-cache',
route:
'/errors/sync-io-after-runtime-api/quickly-expiring-public-cache',
}, },
])( ])(
'aborts the prerender without logging an error $description', 'aborts the prerender without logging an error $description',
async ({ path }) => { async ({ path, route }) => {
// In a runtime prefetch, we might encounter sync IO usages that weren't caught during build, // In a runtime prefetch, we might encounter sync IO usages that weren't caught during build,
// because they were hidden behind e.g. a cookies() call. // because they were hidden behind e.g. a cookies() call.
// We currently have no way to catch these statically. // We currently have no way to catch these statically.
...@@ -1260,8 +1267,9 @@ describe('runtime prefetching', () => { ...@@ -1260,8 +1267,9 @@ describe('runtime prefetching', () => {
}, },
]) ])
const syncIOErrorText = `Route "${route}": Next.js encountered the unstable value \`Date.now()\` while prerendering`
if (!isNextDeploy) { if (!isNextDeploy) {
expect(getCliOutput()).not.toMatch(`Date.now()`) expect(getCliOutput()).toInclude(syncIOErrorText)
} }
// Navigate to the page // Navigate to the page
......
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