Unverified Commit 4a1b510f authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Add more test cases to the `dynamicIO` sync IO access test suite (#81045)

With this PR, we're adding test fixtures for `headers()`, `draftMode()`,
and (fallback) `params`, by more or less copy&pasting the fixture for
`cookies()`.

We're also adding assertions for environment prefixes of `console.log()`
calls that happen after the sync IO access.
parent b8eb7314
......@@ -42,11 +42,7 @@ function SearchParamsReadingComponent({
const fooParams = (
searchParams as unknown as UnsafeUnwrappedSearchParams<typeof searchParams>
).foo
return (
<div>
this component read the accessed the `foo` search param: {fooParams}
</div>
)
return <div>this component read the `foo` search param: {fooParams}</div>
}
function LongRunningComponent() {
......
......@@ -40,11 +40,7 @@ function SearchParamsReadingComponent({
const fooParams = (
searchParams as unknown as UnsafeUnwrappedSearchParams<typeof searchParams>
).foo
return (
<div>
this component read the accessed the `foo` search param: {fooParams}
</div>
)
return <div>this component read the `foo` search param: {fooParams}</div>
}
function LongRunningComponent() {
......
......@@ -7,10 +7,9 @@ export default async function Page() {
return (
<>
<p>
This page access cookies synchronously and it triggers dynamic before
another component is finished which doesn't define a fallback UI with
Suspense. This is considered a build error and the message should
clearly indicate that it was caused by a synchronous dynamic API usage.
This page accesses cookies synchronously but it does so late enough in
the render that all unfinished sub-trees have a defined Suspense
boundary. This is fine and doesn't need to error the build.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
......@@ -31,8 +30,17 @@ export default async function Page() {
async function CookiesReadingComponent() {
await new Promise((r) => process.nextTick(r))
const _token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
return <div>this component read the `token` cookie synchronously</div>
const token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return (
<div>
this component read the `token` cookie synchronously: {token?.value}
</div>
)
}
async function LongRunningComponent() {
......
......@@ -7,7 +7,7 @@ export default async function Page() {
return (
<>
<p>
This page access cookies synchronously and it triggers dynamic before
This page accesses cookies synchronously and it triggers dynamic before
another component is finished which doesn't define a fallback UI with
Suspense. This is considered a build error and the message should
clearly indicate that it was caused by a synchronous dynamic API usage.
......@@ -30,6 +30,11 @@ export default async function Page() {
async function CookiesReadingComponent() {
await new Promise((r) => process.nextTick(r))
const _token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return <div>this component read the `token` cookie synchronously</div>
}
......
'use client'
export function IndirectionOne({ children }) {
return children
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { Suspense } from 'react'
import { draftMode, UnsafeUnwrappedDraftMode } from 'next/headers'
import { IndirectionOne } from './indirection'
export default async function Page() {
return (
<>
<p>
This page accesses draftMode.isEnabled synchronously. This does not
trigger dynamic, and the build should succeed. In dev mode, we do log an
error for the sync access though.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
<DraftModeReadingComponent />
</IndirectionOne>
</Suspense>
</>
)
}
async function DraftModeReadingComponent() {
await new Promise((r) => process.nextTick(r))
const isEnabled = (draftMode() as unknown as UnsafeUnwrappedDraftMode)
.isEnabled
console.log(
'This log should be prefixed with the "Prerender" environment, because the sync access above does not lead to an abort.'
)
return (
<div>
this component read the draftMode isEnabled status synchronously:{' '}
<span id="draft-mode">{isEnabled.toString()}</span>
</div>
)
}
function Fallback() {
return <div data-fallback="">loading...</div>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
'use client'
export function IndirectionOne({ children }) {
return children
}
export function IndirectionTwo({ children }) {
return children
}
export function IndirectionThree({ children }) {
return children
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { Suspense } from 'react'
import { headers, UnsafeUnwrappedHeaders } from 'next/headers'
import { IndirectionOne, IndirectionTwo, IndirectionThree } from './indirection'
export default async function Page() {
return (
<>
<p>
This page accesses headers synchronously but it does so late enough in
the render that all unfinished sub-trees have a defined Suspense
boundary. This is fine and doesn't need to error the build.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
<HeadersReadingComponent />
</IndirectionOne>
</Suspense>
<Suspense fallback={<Fallback />}>
<IndirectionTwo>
<LongRunningComponent />
</IndirectionTwo>
</Suspense>
<IndirectionThree>
<ShortRunningComponent />
</IndirectionThree>
</>
)
}
async function HeadersReadingComponent() {
await new Promise((r) => process.nextTick(r))
const userAgent = (headers() as unknown as UnsafeUnwrappedHeaders).get(
'user-agent'
)
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return (
<div>
this component read the `user-agent` header synchronously: {userAgent}
</div>
)
}
async function LongRunningComponent() {
await new Promise((r) =>
process.nextTick(async () => {
await 1
process.nextTick(r)
})
)
return (
<div>
this component took a long time to resolve (but still before the dynamicIO
cutoff). It might not be done before the sync headers call happens.
</div>
)
}
async function ShortRunningComponent() {
return (
<div>
This component runs quickly (in a microtask). It should be finished before
the sync headers call is triggered.
</div>
)
}
function Fallback() {
return <div data-fallback="">loading...</div>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
'use client'
export function IndirectionOne({ children }) {
return children
}
export function IndirectionTwo({ children }) {
return children
}
export function IndirectionThree({ children }) {
return children
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { Suspense } from 'react'
import { headers, type UnsafeUnwrappedHeaders } from 'next/headers'
import { IndirectionOne, IndirectionTwo, IndirectionThree } from './indirection'
export default async function Page() {
return (
<>
<p>
This page accesses headers synchronously and it triggers dynamic before
another component is finished which doesn't define a fallback UI with
Suspense. This is considered a build error and the message should
clearly indicate that it was caused by a synchronous dynamic API usage.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
<HeadersReadingComponent />
</IndirectionOne>
</Suspense>
<IndirectionTwo>
<LongRunningComponent />
</IndirectionTwo>
<IndirectionThree>
<ShortRunningComponent />
</IndirectionThree>
</>
)
}
async function HeadersReadingComponent() {
await new Promise((r) => process.nextTick(r))
const userAgent = (headers() as unknown as UnsafeUnwrappedHeaders).get(
'user-agent'
)
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return (
<div>
this component read the `user-agent` header synchronously: {userAgent}
</div>
)
}
async function LongRunningComponent() {
await new Promise((r) =>
process.nextTick(async () => {
await 1
process.nextTick(r)
})
)
return (
<div>
this component took a long time to resolve (but still before the dynamicIO
cutoff). It might not be done before the sync headers call happens.
</div>
)
}
async function ShortRunningComponent() {
return (
<div>
This component runs quickly (in a microtask). It should be finished before
the sync headers call is triggered.
</div>
)
}
function Fallback() {
return <div data-fallback="">loading...</div>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
'use client'
export function IndirectionOne({ children }) {
return children
}
export function IndirectionTwo({ children }) {
return children
}
export function IndirectionThree({ children }) {
return children
}
import { Suspense } from 'react'
import { IndirectionOne, IndirectionTwo, IndirectionThree } from './indirection'
export default async function Page({ params }) {
return (
<>
<p>
This page accesses params synchronously but it does so late enough in
the render that all unfinished sub-trees have a defined Suspense
boundary. This is fine and doesn't need to error the build.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
<ParamsReadingComponent params={params} />
</IndirectionOne>
</Suspense>
<Suspense fallback={<Fallback />}>
<IndirectionTwo>
<LongRunningComponent />
</IndirectionTwo>
</Suspense>
<IndirectionThree>
<ShortRunningComponent />
</IndirectionThree>
</>
)
}
async function ParamsReadingComponent({ params }) {
await new Promise((r) => process.nextTick(r))
const slug = params.slug
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return <div>this component read the `slug` param synchronously: {slug}</div>
}
async function LongRunningComponent() {
await new Promise((r) =>
process.nextTick(async () => {
await 1
process.nextTick(r)
})
)
return (
<div>
this component took a long time to resolve (but still before the dynamicIO
cutoff). It might not be done before the sync params call happens.
</div>
)
}
async function ShortRunningComponent() {
return (
<div>
This component runs quickly (in a microtask). It should be finished before
the sync params call is triggered.
</div>
)
}
function Fallback() {
return <div data-fallback="">loading...</div>
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
'use client'
export function IndirectionOne({ children }) {
return children
}
export function IndirectionTwo({ children }) {
return children
}
export function IndirectionThree({ children }) {
return children
}
import { Suspense } from 'react'
import { IndirectionOne, IndirectionTwo, IndirectionThree } from './indirection'
export default async function Page({ params }) {
return (
<>
<p>
This page accesses params synchronously and it triggers dynamic before
another component is finished which doesn't define a fallback UI with
Suspense. This is considered a build error and the message should
clearly indicate that it was caused by a synchronous dynamic API usage.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
<ParamsReadingComponent params={params} />
</IndirectionOne>
</Suspense>
<IndirectionTwo>
<LongRunningComponent />
</IndirectionTwo>
<IndirectionThree>
<ShortRunningComponent />
</IndirectionThree>
</>
)
}
async function ParamsReadingComponent({ params }) {
await new Promise((r) => process.nextTick(r))
const slug = params.slug
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return <div>this component read the `slug` param synchronously: {slug}</div>
}
async function LongRunningComponent() {
await new Promise((r) =>
process.nextTick(async () => {
await 1
process.nextTick(r)
})
)
return (
<div>
this component took a long time to resolve (but still before the dynamicIO
cutoff). It might not be done before the sync params call happens.
</div>
)
}
async function ShortRunningComponent() {
return (
<div>
This component runs quickly (in a microtask). It should be finished before
the sync params call is triggered.
</div>
)
}
function Fallback() {
return <div data-fallback="">loading...</div>
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
module.exports = nextConfig
......@@ -40,6 +40,11 @@ async function SearchParamsReadingComponent({
const fooParams = (
searchParams as unknown as UnsafeUnwrappedSearchParams<typeof searchParams>
).foo
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return (
<div>
this component read the accessed the `foo` search param: {fooParams}
......
......@@ -10,9 +10,10 @@ export default async function Page(props: {
return (
<>
<p>
This page accesses searchParams synchronously but it does so late enough
in the render that all unfinished sub-trees have a defined Suspense
boundary. This is fine and doesn't need to error the build.
This page accesses searchParams synchronously and it triggers dynamic
before another component is finished which doesn't define a fallback UI
with Suspense. This is considered a build error and the message should
clearly indicate that it was caused by a synchronous dynamic API usage.
</p>
<Suspense fallback={<Fallback />}>
<IndirectionOne>
......@@ -38,6 +39,11 @@ async function SearchParamsReadingComponent({
const fooParams = (
searchParams as unknown as UnsafeUnwrappedSearchParams<typeof searchParams>
).foo
console.log(
'This log should be prefixed with the "Server" environment, because the sync IO access above advanced the rendering out of the "Prerender" environment.'
)
return (
<div>
this component read the accessed the `foo` search param: {fooParams}
......
// Used to deterministically stub out minified local names in stack traces.
const abc = 'abcdefghijklmnopqrstuvwxyz'
const hostElementsUsedInFixtures = ['html', 'body', 'main', 'div']
import escapeStringRegexp from 'escape-string-regexp'
export function getPrerenderOutput(
cliOutput: string,
......@@ -47,3 +48,19 @@ export function getPrerenderOutput(
return lines.join('\n').trim()
}
export function assertLog(
logs: Array<{ source: string; message: string }>,
environment: 'Server' | 'Prerender' | 'Cache',
message: string
) {
expect(logs.map((l) => l.message)).toEqual(
expect.arrayContaining([
expect.stringMatching(
new RegExp(
`^.*${escapeStringRegexp(message)}.* \\b${environment}\\b $`
)
),
])
)
}
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