Unverified Commit 3874b55d authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Add fallback param deployment regression coverage (#98883)

Add permanent end-to-end regression coverage for omitted optional
catch-all route parameters with the Vercel adapter. The fix in
nextjs/adapter-vercel#126 is now released in the Vercel CLI; these tests
keep the behavior covered in Next.js.

The fixtures exercise `/[locale]/[[...filterSlugs]]` at `/en` during bot
and draft-mode prerender bypasses and a fetch action. They also check
optional route parameters alongside query parameters, draft-mode Cached
Navigations, and locale data requests. This PR adds tests and fixtures
without changing Next.js runtime behavior.
parent 8b067039
'use client'
import { useActionState } from 'react'
import { submitAction } from './action'
export function ActionForm() {
const [result, formAction] = useActionState(submitAction, 'initial', '/en')
return (
<form action={formAction} id="action-form">
<button id="submit-action" type="submit">
Submit action
</button>
<p id="action-result">{result}</p>
</form>
)
}
'use server'
export async function submitAction(
_previousState: string,
_formData: FormData
) {
return 'submitted'
}
import { Suspense } from 'react'
import { ActionForm } from './action-form'
type Params = {
locale: string
filterSlugs?: string[]
}
export function generateStaticParams(): Array<Pick<Params, 'locale'>> {
return [{ locale: 'en' }]
}
async function ParamsValue({ params }: { params: Promise<Params> }) {
const { locale, filterSlugs } = await params
const mappedSlugs = filterSlugs?.map((slug) => slug.toUpperCase()) ?? []
return (
<p id="params">
{JSON.stringify({
locale,
filterSlugs: filterSlugs ?? null,
mappedSlugs,
})}
</p>
)
}
export default function Page({ params }: { params: Promise<Params> }) {
return (
<>
<Suspense fallback={<p>Loading params</p>}>
<ParamsValue params={params} />
</Suspense>
<ActionForm />
</>
)
}
import { draftMode } from 'next/headers'
export async function GET() {
;(await draftMode()).enable()
return new Response('Draft mode enabled')
}
...@@ -6,6 +6,19 @@ function countSubstring(str: string, substr: string): number { ...@@ -6,6 +6,19 @@ function countSubstring(str: string, substr: string): number {
return str.split(substr).length - 1 return str.split(substr).length - 1
} }
function expectOptionalCatchallParams(html: string) {
expect(html).not.toContain('%%drp:')
const $ = cheerio.load(html)
expect($('#params').text()).toBe(
JSON.stringify({
locale: 'en',
filterSlugs: null,
mappedSlugs: [],
})
)
}
;(isNextDev ? describe.skip : describe)( ;(isNextDev ? describe.skip : describe)(
'metadata streaming with Cache Components and the default bot list', 'metadata streaming with Cache Components and the default bot list',
() => { () => {
...@@ -109,6 +122,55 @@ function countSubstring(str: string, substr: string): number { ...@@ -109,6 +122,55 @@ function countSubstring(str: string, substr: string): number {
expect($('#dynamic-content').text()).toBe('dynamic content') expect($('#dynamic-content').text()).toBe('dynamic content')
}) })
it('should not expose fallback placeholders for an omitted optional catch-all during a bot bypass', async () => {
const response = await next.fetch('/en', {
headers: {
'user-agent': 'Twitterbot',
},
})
expect(response.status).toBe(200)
expectOptionalCatchallParams(await response.text())
})
it('should not expose fallback placeholders during a draft mode bypass', async () => {
const draftResponse = await next.fetch('/api/draft/enable')
const cookie = draftResponse.headers.get('set-cookie')?.split(';', 1)[0]
expect(cookie).toBeTruthy()
const response = await next.fetch('/en', {
headers: {
cookie: cookie!,
},
})
expect(response.status).toBe(200)
expectOptionalCatchallParams(await response.text())
})
it('should not expose fallback placeholders during a Next-Action request bypass', async () => {
const browser = await next.browser('/en', {
pushErrorAsConsoleLog: true,
})
await browser.elementById('submit-action').click()
await retry(async () => {
expect(await browser.elementById('action-result').text()).toBe(
'submitted'
)
expect(await browser.elementById('params').text()).toBe(
JSON.stringify({
locale: 'en',
filterSlugs: null,
mappedSlugs: [],
})
)
})
await assertNoConsoleErrors(browser)
})
describe('Cache Components metadata streaming', () => { describe('Cache Components metadata streaming', () => {
it('should generate metadata in head when page is fully static', async () => { it('should generate metadata in head when page is fully static', async () => {
const $ = await next.render$('/fully-static') const $ = await next.render$('/fully-static')
......
export async function GET(
_request: Request,
{ params }: { params: Promise<{ locale: string; filterSlugs?: string[] }> }
) {
return Response.json(await params)
}
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>
}
export async function GET(
_request: Request,
{ params }: { params: Promise<{ slug?: string[] }> }
) {
return Response.json(await params)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {}
export default nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('optional-catchall-route-params', () => {
const { next } = nextTestSetup({ files: __dirname })
it.each(['index', ''])(
'preserves pathname params with slug=%s in the query',
async (slug) => {
const res = await next.fetch(`/shop/books?slug=${slug}`)
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ slug: ['books'] })
}
)
it('omits an absent optional catch-all', async () => {
const res = await next.fetch('/en')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ locale: 'en' })
})
})
...@@ -1291,6 +1291,63 @@ describe('cached navigations', () => { ...@@ -1291,6 +1291,63 @@ describe('cached navigations', () => {
) )
}) })
it('reuses cached page segment across fallback params after a draft mode HTML load', async () => {
let page: Playwright.Page
const browser = await next.browser(
'/api/draft/enable?to=/with-fallback-params/foo',
{
async beforePageLoad(p: Playwright.Page) {
page = p
},
}
)
const act = createRouterAct(page)
try {
await retry(async () => {
expect(
await browser.elementById('connection-boundary').text()
).toContain('Dynamic content')
})
expect(await browser.elementById('params-boundary').text()).toContain(
'Param: foo'
)
await act(async () => {
await act(
async () => {
await browser
.elementByCss('a[href="/with-fallback-params/bar"]')
.click()
},
{
includes: 'Dynamic content',
block: true,
}
)
expect(await browser.elementById('cached-content').text()).toContain(
'Cached content'
)
expect(await browser.elementById('params-boundary').text()).toBe(
'Loading params...'
)
expect(await browser.elementById('connection-boundary').text()).toBe(
'Loading connection...'
)
})
expect(await browser.elementById('params-boundary').text()).toContain(
'Param: bar'
)
expect(await browser.elementById('connection-boundary').text()).toContain(
'Dynamic content'
)
} finally {
await page.context().clearCookies()
}
})
it('does not leak resolved param-specific content across params when using prefetch={true}', async () => { it('does not leak resolved param-specific content across params when using prefetch={true}', async () => {
let page: Playwright.Page let page: Playwright.Page
const browser = await next.browser('/with-fallback-params', { const browser = await next.browser('/with-fallback-params', {
......
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
export async function GET(request: Request) {
;(await draftMode()).enable()
const destination = new URL(request.url).searchParams.get('to') ?? '/'
redirect(destination)
}
...@@ -52,6 +52,13 @@ describe('i18n Support Root Catch-all', () => { ...@@ -52,6 +52,13 @@ describe('i18n Support Root Catch-all', () => {
}) })
}) })
// @force-gate !dev
it('omits the optional catch-all in a non-prerendered locale data request', async () => {
const res = await next.fetch(`/_next/data/${next.buildId}/nl-NL.json`)
expect(res.status).toBe(200)
expect((await res.json()).pageProps.params).toEqual({})
})
it('should navigate to other locale index and back', async () => { it('should navigate to other locale index and back', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
......
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