Unverified Commit 4821e15e authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add route handler startup preload regression test

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 41215d77
export const dynamic = 'force-dynamic'
// A marker observes the cold module evaluation directly, without adding an
// artificial delay or relying on request latency thresholds.
console.log('preload-test:route-evaluated')
export function GET() {
return Response.json({ ok: true })
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
console.log('preload-test:page-evaluated')
export function getServerSideProps() {
return { props: {} }
}
export default function Control() {
return <p>control</p>
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
// Startup preloading is specific to the self-hosted production server.
describe.each([undefined, true, false])(
'preloadEntriesOnStart: %s',
(preloadEntriesOnStart) => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
nextConfig: {
experimental: {
...(preloadEntriesOnStart === undefined
? {}
: { preloadEntriesOnStart }),
},
},
})
beforeAll(async () => {
const { exitCode } = await next.build()
if (exitCode !== 0) {
throw new Error(`next build failed with exit code ${exitCode}`)
}
// start() clears cliOutput, excluding build-time module evaluation.
await next.start({ skipBuild: true })
})
it('respects the option before the first route handler request', async () => {
if (preloadEntriesOnStart === false) {
// A request to another entry must not evaluate the route handler.
const response = await next.fetch('/control')
expect(response.status).toBe(200)
expect(next.cliOutput).not.toContain('preload-test:route-evaluated')
} else {
// Preloading is asynchronous. The Pages Router control confirms that
// the entry preloader is running, without warming the route by HTTP.
await retry(async () => {
expect(next.cliOutput).toContain('preload-test:page-evaluated')
})
await retry(async () => {
expect(next.cliOutput).toContain('preload-test:route-evaluated')
})
}
})
it('serves the route and evaluates its module only once', async () => {
for (let i = 0; i < 2; i++) {
const response = await next.fetch('/api/heavy')
expect(response.status).toBe(200)
expect(await response.json()).toEqual({ ok: true })
}
await retry(async () => {
expect(
next.cliOutput.split('preload-test:route-evaluated').length - 1
).toBe(1)
})
})
}
)
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