Unverified Commit 3cb82f9c authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

test: unflake prefetch-fallback-static in deploy (#99366)

the ISR fallback tests were written in a way that only worked if the
first retry yielded a concrete prerender. this is enough in `next
start`, but in deploy, the ISR fallback retry does not always succeed in
one attempt. this PR adjusts the `act()` assertions to support multiple
retry attempts. this is not easy to express using usual `act()`
patterns, so we try the assertion, and retry it if it threw an error
that indicates that an expected response didn't show up (other errors
are rethrown)
parent f08c4536
import { Suspense } from 'react'
import { DebugLinkAccordion } from '../components/link-accordion' import { DebugLinkAccordion } from '../components/link-accordion'
import { io } from 'next/cache'
export default function Page() { type SearchParams = { unique?: string }
export default async function Page({
searchParams,
}: {
searchParams: Promise<SearchParams>
}) {
return (
<Suspense fallback={<p>Loading...</p>}>
<Inner searchParams={searchParams} />
</Suspense>
)
}
async function Inner({
searchParams,
}: {
searchParams: Promise<SearchParams>
}) {
await io()
const unique =
(await searchParams).unique ?? (await io().then(() => Date.now()))
// NOTE: all "not-prerendered" slugs should be suffixed with the unique value
// (which is different for each test) to guarantee that two tests don't
// accidentally use the same slug.
const slugWithUnique = (slug: string) => `${slug}_u${unique}`
const linkForSlug = (slug: string) => `/static-for-some-params/${slug}` const linkForSlug = (slug: string) => `/static-for-some-params/${slug}`
return ( return (
<main> <main>
<div>
Current unique seed: {unique} (
<a href={`/unique=${unique}`}>Permalink</a>)
</div>
<h2>Prerendered, did not use cookies</h2> <h2>Prerendered, did not use cookies</h2>
<ul> <ul>
<li> <li>
...@@ -34,21 +68,22 @@ export default function Page() { ...@@ -34,21 +68,22 @@ export default function Page() {
<ul> <ul>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={linkForSlug('not-prerendered_no-cookies_prefetch-auto')} href={linkForSlug(slugWithUnique('not-prerendered_no-cookies'))}
prefetch="auto" prefetch="auto"
/> />
</li> </li>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={linkForSlug('not-prerendered_no-cookies_prefetch-true')} href={linkForSlug(slugWithUnique('not-prerendered_no-cookies'))}
prefetch={true} prefetch={true}
/> />
</li> </li>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={ href={
linkForSlug('not-prerendered_no-cookies_ensure-static-shell') + linkForSlug(
'/ensure-static/shell' slugWithUnique('not-prerendered_no-cookies_es-shell')
) + '/ensure-static/shell'
} }
prefetch={true} prefetch={true}
/> />
...@@ -56,8 +91,9 @@ export default function Page() { ...@@ -56,8 +91,9 @@ export default function Page() {
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={ href={
linkForSlug('not-prerendered_no-cookies_ensure-static-prefetch') + linkForSlug(
'/ensure-static/prefetch' slugWithUnique('not-prerendered_no-cookies_es-prefetch')
) + '/ensure-static/prefetch'
} }
prefetch={true} prefetch={true}
/> />
...@@ -65,33 +101,42 @@ export default function Page() { ...@@ -65,33 +101,42 @@ export default function Page() {
</ul> </ul>
<h2>Not prerendered, did use cookies</h2> <h2>Not prerendered, did use cookies</h2>
<h3>Without ensureStatic</h3>
<ul> <ul>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={linkForSlug('not-prerendered_yes-cookies_prefetch-auto')} href={linkForSlug(slugWithUnique('not-prerendered_yes-cookies'))}
prefetch="auto" prefetch="auto"
/> />
</li> </li>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={linkForSlug('not-prerendered_yes-cookies_prefetch-true')} href={linkForSlug(slugWithUnique('not-prerendered_yes-cookies'))}
prefetch={true} prefetch={true}
/> />
</li> </li>
</ul>
<h3>with ensureStatic = "shell"</h3>
<ul>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={ href={
linkForSlug('not-prerendered_yes-cookies_ensure-static-shell') + linkForSlug(
'/ensure-static/shell' slugWithUnique('not-prerendered_yes-cookies_es-shell')
) + '/ensure-static/shell'
} }
prefetch={true} prefetch={true}
/> />
</li> </li>
</ul>
<h3>with ensureStatic = "prefetch"</h3>
<ul>
<li> <li>
<DebugLinkAccordion <DebugLinkAccordion
href={ href={
linkForSlug( linkForSlug(
'not-prerendered_yes-cookies_ensure-static-prefetch' slugWithUnique(`not-prerendered_yes-cookies_es-prefetch`)
) + '/ensure-static/prefetch' ) + '/ensure-static/prefetch'
} }
prefetch={true} prefetch={true}
......
'use client'
import { useState } from 'react'
import { LinkAccordion } from './link-accordion'
export function DynamicLinkAccordion({
hrefPattern,
placeholder,
initialValue,
}: {
hrefPattern: string
placeholder: string
initialValue: string
}) {
const [value, setValue] = useState(initialValue)
const href = hrefPattern.replace(placeholder, value)
return (
<div
data-dynamic-link-accordion={hrefPattern}
style={{ border: '1px solid lightgrey', padding: '1em' }}
>
<div>
<span>
Dynamic link generator{' '}
<label>
Value
<input type="text" onChange={(e) => setValue(e.target.value)} />
</label>
</span>
<div>
<code>{hrefPattern}</code>
</div>
</div>
<div>
<LinkAccordion key={href} href={href} prefetch="auto">
{href} (prefetch="auto")
</LinkAccordion>
</div>
<div>
<LinkAccordion key={href} href={href} prefetch={true}>
{href} {`(prefetch={true})`}
</LinkAccordion>
</div>
</div>
)
}
import { isNextDeploy, nextTestSetup } from 'e2e-utils' import { isNextDeploy, nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright' import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act' import {
createRouterAct,
isActMissingResponseError,
waitForSuccessfulISRFallbackRetry,
} from 'router-act'
import { retry } from '../../../../lib/next-test-utils' import { retry } from '../../../../lib/next-test-utils'
const REPRODUCE_UNNECESSARY_RUNTIME_PREFETCH = const REPRODUCE_UNNECESSARY_RUNTIME_PREFETCH =
...@@ -2733,24 +2737,6 @@ describe('static App Shell prefetch attempt', () => { ...@@ -2733,24 +2737,6 @@ describe('static App Shell prefetch attempt', () => {
// TODO(ensure-static): In deploy, we apparently serve an ISR fallback for RSC prefetches // TODO(ensure-static): In deploy, we apparently serve an ISR fallback for RSC prefetches
// even though the route is configured as blocking. // even though the route is configured as blocking.
// The client router should retry the request. // The client router should retry the request.
const isActMissingSlugError = (thrown: unknown) => {
if (
thrown &&
typeof thrown === 'object' &&
'message' in thrown &&
typeof thrown.message === 'string'
) {
const error = thrown as Error
return (
error.message.includes(
'Expected a response containing the given string'
) && error.message.includes(`Slug: ${slug}`)
)
}
return false
}
try { try {
await act(async () => { await act(async () => {
await browser await browser
...@@ -2769,52 +2755,19 @@ describe('static App Shell prefetch attempt', () => { ...@@ -2769,52 +2755,19 @@ describe('static App Shell prefetch attempt', () => {
]) ])
} catch (err) { } catch (err) {
// We might not get the slug in the initial request. // We might not get the slug in the initial request.
// TODO: nested with block: true? if (isActMissingResponseError(err, `Slug: ${slug}`)) {
if (!isActMissingSlugError(err)) {
throw err
}
console.error( console.error(
'initial link reveal failed (likely an ISR fallback)', 'initial link reveal failed (likely an ISR fallback)',
err err
) )
const interval = 2_000 await waitForSuccessfulISRFallbackRetry(act, {
const maxAttempts = 5
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
await act(async () => {
await new Promise<void>((resolve) =>
setTimeout(resolve, interval + 500)
)
}, [
// Static prefetch
{
includes: `Slug: ${slug}`, includes: `Slug: ${slug}`,
kind: 'static', kind: 'static',
}, })
// No runtime requests (e.g. runtime follow-up due to ISR fallback)
{ includes: '', kind: 'runtime', block: 'reject' },
])
// if act() succeeded, then we got a complete prerender, and can stop waiting
// for retries.
break
} catch (err) {
// act() threw.
if (!isActMissingSlugError(err)) {
throw err
}
// The router did a retry, but got an ISR fallback again.
if (attempt < maxAttempts) {
console.error(`Retry ${attempt} failed:`, err)
} else { } else {
console.error(`Retry ${attempt} failed, aborting`)
throw err throw err
} }
} }
}
}
} else { } else {
await act(async () => { await act(async () => {
await browser await browser
...@@ -2862,24 +2815,6 @@ describe('static App Shell prefetch attempt', () => { ...@@ -2862,24 +2815,6 @@ describe('static App Shell prefetch attempt', () => {
// TODO(ensure-static): In deploy, we apparently serve an ISR fallback for RSC prefetches // TODO(ensure-static): In deploy, we apparently serve an ISR fallback for RSC prefetches
// even though the route is configured as blocking. // even though the route is configured as blocking.
// The client router should retry the request. // The client router should retry the request.
const isActMissingSlugError = (thrown: unknown) => {
if (
thrown &&
typeof thrown === 'object' &&
'message' in thrown &&
typeof thrown.message === 'string'
) {
const error = thrown as Error
return (
error.message.includes(
'Expected a response containing the given string'
) && error.message.includes(`Slug: ${slug}`)
)
}
return false
}
try { try {
await act(async () => { await act(async () => {
await browser await browser
...@@ -2898,52 +2833,19 @@ describe('static App Shell prefetch attempt', () => { ...@@ -2898,52 +2833,19 @@ describe('static App Shell prefetch attempt', () => {
]) ])
} catch (err) { } catch (err) {
// We might not get the slug in the initial request. // We might not get the slug in the initial request.
// TODO: nested with block: true? if (isActMissingResponseError(err, `Slug: ${slug}`)) {
if (!isActMissingSlugError(err)) {
throw err
}
console.error( console.error(
'initial link reveal failed (likely an ISR fallback)', 'initial link reveal failed (likely an ISR fallback)',
err err
) )
const interval = 2_000 await waitForSuccessfulISRFallbackRetry(act, {
const maxAttempts = 5
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
await act(async () => {
await new Promise<void>((resolve) =>
setTimeout(resolve, interval + 500)
)
}, [
// Static prefetch
{
includes: `Slug: ${slug}`, includes: `Slug: ${slug}`,
kind: 'static', kind: 'static',
}, })
// No runtime requests (e.g. runtime follow-up due to ISR fallback)
{ includes: '', kind: 'runtime', block: 'reject' },
])
// if act() succeeded, then we got a complete prerender, and can stop waiting
// for retries.
break
} catch (err) {
// act() threw.
if (!isActMissingSlugError(err)) {
throw err
}
// The router did a retry, but got an ISR fallback again.
if (attempt < maxAttempts) {
console.error(`Retry ${attempt} failed:`, err)
} else { } else {
console.error(`Retry ${attempt} failed, aborting`)
throw err throw err
} }
} }
}
}
} else { } else {
await act(async () => { await act(async () => {
await browser await browser
......
...@@ -120,6 +120,8 @@ type ActConfig = ...@@ -120,6 +120,8 @@ type ActConfig =
| 'no-requests' | 'no-requests'
| null | null
export type RouterAct = ReturnType<typeof createRouterAct>
export function createRouterAct( export function createRouterAct(
page: Playwright.Page, page: Playwright.Page,
options?: { options?: {
...@@ -827,10 +829,14 @@ ${fulfilled.body} ...@@ -827,10 +829,14 @@ ${fulfilled.body}
// Print a helpful error message. // Print a helpful error message.
if (expectedResponses.length === 1) { if (expectedResponses.length === 1) {
const includes = expectedResponses[0].includes
error.message = error.message =
'Expected a response containing the given string:\n\n' + 'Expected a response containing the given string:\n\n' +
expectedResponses[0].includes + includes +
'\n' '\n'
;(error as ActMissingResponseError).code =
MISSING_RESPONSE_ERROR_CODE
;(error as ActMissingResponseError).includes = includes
} else { } else {
const expectedSubstrings = expectedResponses.map( const expectedSubstrings = expectedResponses.map(
(item) => item.includes (item) => item.includes
...@@ -892,3 +898,83 @@ ${fulfilled.body} ...@@ -892,3 +898,83 @@ ${fulfilled.body}
return act return act
} }
const MISSING_RESPONSE_ERROR_CODE = 'MISSING_RESPONSE'
type ActMissingResponseError = Error & {
code: typeof MISSING_RESPONSE_ERROR_CODE
includes: string
}
export function isActMissingResponseError(
thrown: unknown,
expected: string
): thrown is ActMissingResponseError {
if (
thrown &&
typeof thrown === 'object' &&
'message' in thrown &&
typeof thrown.message === 'string' &&
'code' in thrown &&
thrown.code === MISSING_RESPONSE_ERROR_CODE
) {
const error = thrown as ActMissingResponseError
return error.includes === expected
}
return false
}
/**
* Waits for the ISR fallback retry loop to receive a concrete response.
*
* @param concretePrerenderAssertion - An `act()` assertion that matches the concrete prerender.
* If the response needs to be delayed (e.g. to assert that it's followed with a runtime prefetch),
* nest the call in an another `act()` and set `block: true`.
* */
export async function waitForSuccessfulISRFallbackRetry(
act: RouterAct,
concretePrerenderAssertion: {
includes: string
kind: 'static'
block?: boolean
}
) {
// The router retries ISR fallbacks in 2s intervals.
const interval = 2_000
const maxAttempts = 5
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
await act(async () => {
await new Promise<void>((resolve) =>
setTimeout(resolve, interval + 100)
)
}, [
// The retried request eventually yields a concrete prerender.
concretePrerenderAssertion,
// NO Runtime prefetch to get the content
// or runtime requests of any kind
{ includes: '', kind: 'runtime', block: 'reject' },
])
// if act() succeeded, then we got a complete prerender, and can stop waiting
// for retries.
break
} catch (err) {
// act() threw. Check if it's because we still didn't get a slug in the response.
// This may happen if the concrete prerender hasn't finished yet.
if (isActMissingResponseError(err, concretePrerenderAssertion.includes)) {
// The router did a retry, but got an ISR fallback again.
if (attempt < maxAttempts) {
console.error(`Retry ${attempt} failed:`, err)
} else {
throw new Error(
`Did not receive a concrete prerender after ${maxAttempts} attempts`,
{ cause: err }
)
}
} else {
// All other errors get reported as-is.
throw err
}
}
}
}
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