Skip to content

Commit 2b76f90

Browse files
authored
(DO NOT MERGE) test: page to have correct dynamic params after middle… (#78012)
…ware rewrites <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent a64de7f commit 2b76f90

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { type Params, expectedParams } from '../../../expected'
2+
3+
export default async function CatchAll({
4+
params,
5+
}: {
6+
params: Promise<Params>
7+
}) {
8+
const received = await params
9+
10+
return <p>{JSON.stringify(received)}</p>
11+
}
12+
13+
export async function generateStaticParams(): Promise<Params[]> {
14+
return [expectedParams]
15+
}
16+
17+
export const dynamic = 'force-dynamic'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Link from 'next/link'
2+
3+
export default function Home() {
4+
return <Link href="/1/2">Go to /1/2</Link>
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Params = {
2+
locale: string
3+
rest: string[]
4+
}
5+
6+
export const expectedParams: Params = {
7+
locale: 'en',
8+
rest: ['1', '2'],
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
export function middleware(request: NextRequest) {
5+
return NextResponse.rewrite(
6+
new URL('/en' + request.nextUrl.pathname, request.url)
7+
)
8+
}
9+
10+
export const config = {
11+
matcher: [
12+
/*
13+
* Match all request paths except for the ones starting with:
14+
* - api (API routes)
15+
* - _next/static (static files)
16+
* - _next/image (image optimization files)
17+
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
18+
*/
19+
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
20+
],
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {
5+
experimental: {
6+
ppr: true,
7+
},
8+
}
9+
10+
module.exports = nextConfig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
import { expectedParams as expected } from './expected'
3+
4+
describe('ppr-middleware-rewrite-force-dynamic-generate-static-params', () => {
5+
const { next } = nextTestSetup({
6+
files: __dirname,
7+
})
8+
9+
const expectedParams = JSON.stringify(expected)
10+
11+
it('should have correct dynamic params', async () => {
12+
// should be rewritten with /en
13+
const browser = await next.browser('/')
14+
expect(await browser.elementByCss('a').text()).toBe('Go to /1/2')
15+
16+
// navigate to /1/2
17+
await browser.elementByCss('a').click()
18+
19+
// should be rewritten with /en/1/2 with correct params
20+
expect(await browser.elementByCss('p').text()).toBe(expectedParams)
21+
22+
// reloading the page should have the same params
23+
await browser.refresh()
24+
expect(await browser.elementByCss('p').text()).toBe(expectedParams)
25+
})
26+
})

0 commit comments

Comments
 (0)