Unverified Commit 35583121 authored by Jude Gao's avatar Jude Gao Committed by GitHub

Fix: Middleware does not match when use :path (#72056)

For a given path expression, we want it to optionally match a `.json`
suffix. However, the original implementation was flawed:

The path `/:id` was altered to `/:id(.json)?`, which then converted to
the Regex pattern `^(?:\/(.json))?[\/#\?]?$`. This Regex incorrectly
matched only `/` or `/_json` — clearly not the intended result.

The fix involved adjusting the way we append the `.json` suffix. Instead
of `/:id(.json)?`, we now use `/:id{(\\.json)}?`, which converts to the
Regex `^(?:\/([^\/#\?]+?))(\.json)?[\/#\?]?$`. This pattern correctly
matches both cases, with or without the `.json` suffix.

-----

Fixes https://github.com/vercel/next.js/issues/53840
Fixes https://github.com/vercel/next.js/issues/55558
Closes DX-2088
parent 4638db58
......@@ -8,7 +8,7 @@ describe('get-page-static-infos', () => {
{
originalSource: '/middleware/path',
regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(.json)?[\\/#\\?]?$',
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(\\.json)?[\\/#\\?]?$',
},
]
const result = getMiddlewareMatchers(matchers, { i18n: undefined })
......@@ -21,16 +21,25 @@ describe('get-page-static-infos', () => {
{
originalSource: '/middleware/path',
regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(.json)?[\\/#\\?]?$',
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(\\.json)?[\\/#\\?]?$',
},
{
originalSource: '/middleware/another-path',
regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/another-path(.json)?[\\/#\\?]?$',
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/another-path(\\.json)?[\\/#\\?]?$',
},
]
const result = getMiddlewareMatchers(matchers, { i18n: undefined })
expect(result).toStrictEqual(expected)
})
it('matches /:id and /:id.json', () => {
const matchers = ['/:id']
const result = getMiddlewareMatchers(matchers, { i18n: undefined })[0]
.regexp
const regex = new RegExp(result)
expect(regex.test('/apple')).toBe(true)
expect(regex.test('/apple.json')).toBe(true)
})
})
})
......@@ -346,7 +346,7 @@ export function getMiddlewareMatchers(
source = `/:nextData(_next/data/[^/]{1,})?${source}${
isRoot
? `(${nextConfig.i18n ? '|\\.json|' : ''}/?index|/?index\\.json)?`
: '(.json)?'
: '{(\\.json)}?'
}`
if (nextConfig.basePath) {
......
export default function Home() {
return <p>Chat/ID</p>
}
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Home() {
return <p>Home</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('app dir - middleware with custom matcher', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
it('should match /:id (without asterisk)', async () => {
const browser = await next.browser('/chat/123')
expect(await browser.elementByCss('p').text()).toBe('Home')
})
})
import { NextResponse } from 'next/server'
export default function middleware(req, res) {
const url = req.nextUrl.clone()
url.pathname = '/'
return NextResponse.rewrite(url)
}
export const config = {
matcher: ['/chat/:id'],
}
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