Commit 4f0a2b98 authored by Jimmy Lai's avatar Jimmy Lai

foo

parent a7bccf3a
This diff is collapsed.
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Home() {
return (
<main>
<h1>AsyncLocalStorage Context Loss Test</h1>
<p>
Click a link to trigger runtime prefetch and test context preservation:
</p>
<ul>
<li>
<Link href="/test-page/123">Test with params</Link>
</li>
<li>
<Link href="/test-cookies">Test with cookies()</Link>
</li>
<li>
<Link href="/test-headers">Test with headers()</Link>
</li>
<li>
<Link href="/test-generic-promise">Test with generic promise</Link>
</li>
</ul>
</main>
)
}
import { Suspense } from 'react'
import { cookies } from 'next/headers'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
export const unstable_prefetch = {
mode: 'runtime',
samples: [{ cookies: [] }],
}
export default async function Page() {
return (
<main>
<h1>Cookies Context Loss Test</h1>
<Suspense fallback={<div>Loading...</div>}>
<TestComponent />
</Suspense>
</main>
)
}
async function TestComponent() {
const contextBefore = workUnitAsyncStorage.getStore()
console.log(
'[COOKIES] CONTEXT_BEFORE_AWAIT:',
contextBefore?.type ?? 'undefined'
)
// Await cookies - triggers runtime stage delay
const cookieStore = await cookies()
const contextAfter = workUnitAsyncStorage.getStore()
console.log(
'[COOKIES] CONTEXT_AFTER_AWAIT:',
contextAfter?.type ?? 'undefined'
)
if (contextAfter === undefined) {
console.log('[COOKIES] CONTEXT_STATUS: LOST')
} else {
console.log('[COOKIES] CONTEXT_STATUS: PRESERVED')
}
const timestamp = Date.now()
return (
<div>
<p id="result">Cookies count: {cookieStore.getAll().length}</p>
<p>Timestamp: {timestamp}</p>
<p>Context Before: {contextBefore?.type ?? 'undefined'}</p>
<p>Context After: {contextAfter?.type ?? 'undefined'}</p>
</div>
)
}
import { Suspense } from 'react'
import { cookies } from 'next/headers'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
// A simple promise that resolves after a microtask
// This demonstrates the issue is with React's scheduler, not specific to Next.js APIs
function createDelayedPromise<T>(value: T): Promise<T> {
return new Promise((resolve) => {
queueMicrotask(() => resolve(value))
})
}
export const unstable_prefetch = {
mode: 'runtime',
samples: [{ cookies: [] }],
}
export default async function Page() {
return (
<main>
<h1>Generic Promise Context Loss Test</h1>
<Suspense fallback={<div>Loading...</div>}>
<TestComponent />
</Suspense>
</main>
)
}
async function TestComponent() {
// First access cookies to make this a valid dynamic page
const cookieStore = await cookies()
const contextBefore = workUnitAsyncStorage.getStore()
console.log(
'[GENERIC] CONTEXT_BEFORE_AWAIT:',
contextBefore?.type ?? 'undefined'
)
// Await a generic promise AFTER the runtime API
const data = await createDelayedPromise({ message: 'Hello from promise' })
const contextAfter = workUnitAsyncStorage.getStore()
console.log(
'[GENERIC] CONTEXT_AFTER_AWAIT:',
contextAfter?.type ?? 'undefined'
)
if (contextAfter === undefined) {
console.log('[GENERIC] CONTEXT_STATUS: LOST')
} else {
console.log('[GENERIC] CONTEXT_STATUS: PRESERVED')
}
// Now Date.now() is valid because we already accessed cookies
const timestamp = Date.now()
return (
<div>
<p id="result">Message: {data.message}</p>
<p>Cookies: {cookieStore.getAll().length}</p>
<p>Timestamp: {timestamp}</p>
<p>Context Before: {contextBefore?.type ?? 'undefined'}</p>
<p>Context After: {contextAfter?.type ?? 'undefined'}</p>
</div>
)
}
import { Suspense } from 'react'
import { headers } from 'next/headers'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
export const unstable_prefetch = {
mode: 'runtime',
samples: [{ cookies: [] }],
}
export default async function Page() {
return (
<main>
<h1>Headers Context Loss Test</h1>
<Suspense fallback={<div>Loading...</div>}>
<TestComponent />
</Suspense>
</main>
)
}
async function TestComponent() {
const contextBefore = workUnitAsyncStorage.getStore()
console.log(
'[HEADERS] CONTEXT_BEFORE_AWAIT:',
contextBefore?.type ?? 'undefined'
)
// Await headers - triggers runtime stage delay
const headerStore = await headers()
const contextAfter = workUnitAsyncStorage.getStore()
console.log(
'[HEADERS] CONTEXT_AFTER_AWAIT:',
contextAfter?.type ?? 'undefined'
)
if (contextAfter === undefined) {
console.log('[HEADERS] CONTEXT_STATUS: LOST')
} else {
console.log('[HEADERS] CONTEXT_STATUS: PRESERVED')
}
const timestamp = Date.now()
return (
<div>
<p id="result">
User-Agent: {headerStore.get('user-agent')?.slice(0, 20)}...
</p>
<p>Timestamp: {timestamp}</p>
<p>Context Before: {contextBefore?.type ?? 'undefined'}</p>
<p>Context After: {contextAfter?.type ?? 'undefined'}</p>
</div>
)
}
import { Suspense } from 'react'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
type Params = { id: string }
export const unstable_prefetch = {
mode: 'runtime',
samples: [{ cookies: [] }],
}
export default async function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<h1>Params Context Loss Test</h1>
<Suspense fallback={<div>Loading...</div>}>
<TestComponent params={params} />
</Suspense>
</main>
)
}
async function TestComponent({ params }: { params: Promise<Params> }) {
const contextBefore = workUnitAsyncStorage.getStore()
console.log(
'[PARAMS] CONTEXT_BEFORE_AWAIT:',
contextBefore?.type ?? 'undefined'
)
// Await params - this triggers delayUntilRuntimeStage
// With the macrotask boundary fix, this will force context loss
const resolvedParams = await params
const contextAfter = workUnitAsyncStorage.getStore()
console.log(
'[PARAMS] CONTEXT_AFTER_AWAIT:',
contextAfter?.type ?? 'undefined'
)
if (contextAfter === undefined) {
console.log('[PARAMS] CONTEXT_STATUS: LOST')
} else {
console.log('[PARAMS] CONTEXT_STATUS: PRESERVED')
}
const timestamp = Date.now()
return (
<div>
<p id="result">Param ID: {resolvedParams.id}</p>
<p>Timestamp: {timestamp}</p>
<p>Context Before: {contextBefore?.type ?? 'undefined'}</p>
<p>Context After: {contextAfter?.type ?? 'undefined'}</p>
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
import { createRouterAct } from 'router-act'
/**
* This test demonstrates the AsyncLocalStorage context loss issue
* when React's scheduler continues a suspended component.
*
* The test uses the same infrastructure as the flaky prefetch-runtime tests
* to ensure we're hitting the runtime prefetch code path.
*/
describe('AsyncLocalStorage context loss in React scheduler', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (isNextDev || skipped) {
test('skipped', () => {})
return
}
it('loses context after awaiting params in runtime prefetch', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
// Navigate to the params test page via prefetch
// This triggers runtime prefetch on the server
await act(
async () => {
// Navigate to the dynamic params page
await browser.elementByCss('a[href="/test-page/123"]').click()
},
{
// We expect the page to render with the result
includes: 'Param ID: 123',
}
)
// Check server logs for context status
const logs = next.cliOutput
// Look for the context logs during runtime prefetch
const hasContextLost = logs.includes('[PARAMS] CONTEXT_STATUS: LOST')
const hasContextPreserved = logs.includes(
'[PARAMS] CONTEXT_STATUS: PRESERVED'
)
console.log('Server logs contain CONTEXT_STATUS: LOST:', hasContextLost)
console.log(
'Server logs contain CONTEXT_STATUS: PRESERVED:',
hasContextPreserved
)
// If context is lost, this test fails - demonstrating the bug
if (hasContextLost) {
console.log('\n=== BUG CONFIRMED ===')
console.log('AsyncLocalStorage context is LOST after awaiting params')
console.log('This causes io() to fail to detect sync IO operations')
console.log('=====================\n')
}
expect(hasContextLost).toBe(false)
expect(hasContextPreserved).toBe(true)
})
it('loses context after awaiting cookies() in runtime prefetch', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
await act(
async () => {
await browser.elementByCss('a[href="/test-cookies"]').click()
},
{
includes: 'Cookies count:',
}
)
const logs = next.cliOutput
const hasContextLost = logs.includes('[COOKIES] CONTEXT_STATUS: LOST')
const hasContextPreserved = logs.includes(
'[COOKIES] CONTEXT_STATUS: PRESERVED'
)
console.log(
'COOKIES - Context Lost:',
hasContextLost,
'Preserved:',
hasContextPreserved
)
expect(hasContextLost).toBe(false)
expect(hasContextPreserved).toBe(true)
})
it('loses context after awaiting headers() in runtime prefetch', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
await act(
async () => {
await browser.elementByCss('a[href="/test-headers"]').click()
},
{
includes: 'User-Agent:',
}
)
const logs = next.cliOutput
const hasContextLost = logs.includes('[HEADERS] CONTEXT_STATUS: LOST')
const hasContextPreserved = logs.includes(
'[HEADERS] CONTEXT_STATUS: PRESERVED'
)
console.log(
'HEADERS - Context Lost:',
hasContextLost,
'Preserved:',
hasContextPreserved
)
expect(hasContextLost).toBe(false)
expect(hasContextPreserved).toBe(true)
})
})
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
dynamicIO: true,
clientSegmentCache: true,
cacheComponents: true,
},
}
import { Suspense } from 'react' import { Suspense } from 'react'
import { DebugRenderKind } from '../../../../../shared' import { DebugRenderKind } from '../../../../../shared'
import { workUnitAsyncStorage } from 'next/dist/server/app-render/work-unit-async-storage.external'
type Params = { id: string } type Params = { id: string }
...@@ -25,11 +24,6 @@ export default async function Page({ params }: { params: Promise<Params> }) { ...@@ -25,11 +24,6 @@ export default async function Page({ params }: { params: Promise<Params> }) {
} }
async function RuntimePrefetchable({ params }: { params: Promise<Params> }) { async function RuntimePrefetchable({ params }: { params: Promise<Params> }) {
const res = await params await params
console.log(
'RuntimePrefetchable :: awaited params',
res,
workUnitAsyncStorage.getStore()
)
return <div id="timestamp">Timestamp: {Date.now()}</div> return <div id="timestamp">Timestamp: {Date.now()}</div>
} }
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