Skip to content

Commit

Permalink
[dynamicIO]: dev navigations should show disallowed dynamic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Oct 22, 2024
1 parent 236e001 commit 096745c
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,26 @@ async function generateDynamicFlightRenderResult(
onFlightDataRenderError
)

const rscPayload = await generateDynamicRSCPayload(ctx, options)
const RSCPayload = await generateDynamicRSCPayload(ctx, options)

if (process.env.NODE_ENV === 'development') {
const [resolveValidation, validationOutlet] = createValidationOutlet()
;(RSCPayload as any)._validation = validationOutlet

spawnDynamicValidationInDev(
resolveValidation,
ctx.componentMod.tree,
ctx,
false,
ctx.clientReferenceManifest,
ctx.workStore.route
)
}

// For app dir, use the bundled version of Flight server renderer (renderToReadableStream)
// which contains the subset React.
const flightReadableStream = ctx.componentMod.renderToReadableStream(
rscPayload,
RSCPayload,
ctx.clientReferenceManifest.clientModules,
{
onError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default async function Page() {
const random = Math.random()
return <div id="another-random">{random}</div>
}
9 changes: 9 additions & 0 deletions test/development/app-dir/dynamic-io-dev-errors/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Link from 'next/link'

export default async function Page() {
return <Link href="/error">To /error</Link>
}
5 changes: 5 additions & 0 deletions test/development/app-dir/dynamic-io-dev-errors/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Link from 'next/link'

export default async function Page() {
return <Link href="/no-error">To /no-error</Link>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { nextTestSetup } from 'e2e-utils'
import {
getRedboxDescription,
hasErrorToast,
retry,
waitForAndOpenRuntimeError,
} from 'next-test-utils'

describe('Dynamic IO Dev Errors', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should show a red box error on the SSR render', async () => {
const browser = await next.browser('/error')

await retry(async () => {
expect(await hasErrorToast(browser)).toBe(true)

await waitForAndOpenRuntimeError(browser)

expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(
`"[ Server ] Error: Route "/error" used \`Math.random()\` outside of \`"use cache"\` and without explicitly calling \`await connection()\` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random"`
)
})
})

it('should show a red box error on client navigations', async () => {
const browser = await next.browser('/no-error')

expect(await hasErrorToast(browser)).toBe(false)

await browser.elementByCss("[href='/error']").click()

await retry(async () => {
expect(await hasErrorToast(browser)).toBe(true)

await waitForAndOpenRuntimeError(browser)

expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(
`"[ Server ] Error: Route "/error" used \`Math.random()\` outside of \`"use cache"\` and without explicitly calling \`await connection()\` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random"`
)
})
})
})
10 changes: 10 additions & 0 deletions test/development/app-dir/dynamic-io-dev-errors/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}

module.exports = nextConfig

0 comments on commit 096745c

Please # to comment.