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

[test] Fix deploy test of `cache-components.server-action.test.ts` (#88854)

When deployed, the sentinels where showing `at runtime` instead of `at
buildtime`. There are two reasons for this:

1. The page sentinel was accidentally rendered in a client component,
which causes a hydration mismatch, and the value toggling from `at
buildtime` to `at runtime` when the page is hydrated before the
assertion is done.
2. The middleware was setting sentinel cookies that are intended for the
`/cookies/*` pages, but not for the `/server-action-inline` page. This
caused the server action (which is triggered in the second test) to
consider the page as revalidated, which then led the client router to
refetch the page. With `next start` this is not a problem because the
RSC response is prerendered at build time, and returned as a static
file, which contains the correct sentinel values. However, when deployed
on Vercel, the RSC request results in an ISR cache miss for some reason,
which causes the page to be revalidated (with runtime sentinel values),
and then the third test fails when it receives the regenerated response
instead of the build-time prerender result. This needs to be
investigated separately, but for now we disable the middleware from
setting cookies on this page to prevent the ISR revalidation request.

closes #88817
closes NAR-740

> [!TIP]
> Best reviewed with hidden whitespace changes.
parent bed512a3
'use client' 'use client'
import { ReactNode, useActionState } from 'react' import { ReactNode, useActionState } from 'react'
import { getSentinelValue } from '../getSentinelValue'
export function Form({ action }: { action: () => Promise<ReactNode> }) { export function Form({ action }: { action: () => Promise<ReactNode> }) {
const [result, formAction] = useActionState(action, 'initial') const [result, formAction] = useActionState(action, 'initial')
...@@ -11,7 +10,6 @@ export function Form({ action }: { action: () => Promise<ReactNode> }) { ...@@ -11,7 +10,6 @@ export function Form({ action }: { action: () => Promise<ReactNode> }) {
<h1>Inline Server Action with Cache Components</h1> <h1>Inline Server Action with Cache Components</h1>
<button>Submit</button> <button>Submit</button>
<p>{result}</p> <p>{result}</p>
<div id="page">{getSentinelValue()}</div>
</form> </form>
) )
} }
import { Form } from './form' import { Form } from './form'
import { getSentinelValue } from '../getSentinelValue'
export default function Page() { export default function Page() {
const simpleValue = 'result' const simpleValue = 'result'
...@@ -7,16 +8,19 @@ export default function Page() { ...@@ -7,16 +8,19 @@ export default function Page() {
// Async components emit timing chunks // Async components emit timing chunks
const timedValue = <HasTimingInfo /> const timedValue = <HasTimingInfo />
return ( return (
<Form <>
action={async () => { <Form
'use server' action={async () => {
return ( 'use server'
<> return (
{simpleValue} {jsxValue} {timedValue} <>
</> {simpleValue} {jsxValue} {timedValue}
) </>
}} )
/> }}
/>
<div id="page">{getSentinelValue()}</div>
</>
) )
} }
......
...@@ -2,43 +2,56 @@ import type { NextRequest } from 'next/server' ...@@ -2,43 +2,56 @@ import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
export function middleware(request: NextRequest) { export function middleware(request: NextRequest) {
// Clone the request headers and set a new header `x-hello-from-middleware1` if (
const requestHeaders = new Headers(request.headers) request.nextUrl.pathname.startsWith('/headers/') ||
requestHeaders.set('x-sentinel', 'hello') request.nextUrl.pathname.endsWith('dynamic_api_headers')
requestHeaders.set('x-sentinel-path', request.nextUrl.pathname) ) {
requestHeaders.set( // Clone the request headers and set a new header `x-hello-from-middleware1`
'x-sentinel-rand', const requestHeaders = new Headers(request.headers)
((Math.random() * 100000) | 0).toString(16) requestHeaders.set('x-sentinel', 'hello')
) requestHeaders.set('x-sentinel-path', request.nextUrl.pathname)
requestHeaders.set(
'x-sentinel-rand',
((Math.random() * 100000) | 0).toString(16)
)
const response = NextResponse.next({ return NextResponse.next({
request: { request: {
// New request headers // New request headers
headers: requestHeaders, headers: requestHeaders,
}, },
}) })
response.cookies.set('x-sentinel', 'hello', { }
httpOnly: true,
sameSite: 'strict', const response = NextResponse.next()
maxAge: 60 * 60 * 24 * 7, // 1 week
path: '/', if (
}) request.nextUrl.pathname.startsWith('/cookies/') ||
response.cookies.set('x-sentinel-path', request.nextUrl.pathname, { request.nextUrl.pathname.endsWith('dynamic_api_cookies')
httpOnly: true, ) {
sameSite: 'strict', response.cookies.set('x-sentinel', 'hello', {
maxAge: 60 * 60 * 24 * 7, // 1 week httpOnly: true,
path: '/', sameSite: 'strict',
}) maxAge: 60 * 60 * 24 * 7, // 1 week
response.cookies.set( path: '/',
'x-sentinel-rand', })
((Math.random() * 100000) | 0).toString(16), response.cookies.set('x-sentinel-path', request.nextUrl.pathname, {
{
httpOnly: true, httpOnly: true,
sameSite: 'strict', sameSite: 'strict',
maxAge: 60 * 60 * 24 * 7, // 1 week maxAge: 60 * 60 * 24 * 7, // 1 week
path: '/', path: '/',
} })
) response.cookies.set(
'x-sentinel-rand',
((Math.random() * 100000) | 0).toString(16),
{
httpOnly: true,
sameSite: 'strict',
maxAge: 60 * 60 * 24 * 7, // 1 week
path: '/',
}
)
}
return response return response
} }
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