Unverified Commit a85c636b authored by Sam Ko's avatar Sam Ko Committed by GitHub

Fix middleware matcher for i18n with negative look ahead (#45504)closes…

Fix middleware matcher for i18n with negative look ahead (#45504)closes https://github.com/vercel/next.js/issues/40305

<!--
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:
-->

Found a bug with excluding `/_next/static` from Edge Middleware due to
`/_next` being special-cased when deployed and i18n is configured.

```
export const config = {
  matcher: [
    '/',
    '/((?!api|_next/static|favicon|.well-known|auth|sitemap|robots.txt|files).*)',
  ],
};
```

closes: https://github.com/vercel/next.js/issues/40305

## Bug

- [X] Related issues linked using `fixes #number`
- [X] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent bb942876
...@@ -159,7 +159,9 @@ function getMiddlewareMatchers( ...@@ -159,7 +159,9 @@ function getMiddlewareMatchers(
const isRoot = source === '/' const isRoot = source === '/'
if (i18n?.locales && r.locale !== false) { if (i18n?.locales && r.locale !== false) {
source = `/:nextInternalLocale([^/.]{1,})${isRoot ? '' : source}` source = `/:nextInternalLocale((?!_next/)[^/.]{1,})${
isRoot ? '' : source
}`
} }
source = `/:nextData(_next/data/[^/]{1,})?${source}${ source = `/:nextData(_next/data/[^/]{1,})?${source}${
......
...@@ -53,3 +53,45 @@ describe('Middleware custom matchers i18n', () => { ...@@ -53,3 +53,45 @@ describe('Middleware custom matchers i18n', () => {
} }
) )
}) })
describe('Middleware custom matchers with root', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, '../app', 'pages')),
'next.config.js': new FileRef(
join(__dirname, '../app', 'next.config.js')
),
'middleware.js': `
import { NextResponse } from 'next/server'
export const config = {
matcher: [
'/',
'/((?!api|_next/static|favicon|.well-known|auth|sitemap|robots.txt|files).*)',
],
};
export default function middleware(request) {
const nextUrl = request.nextUrl.clone()
nextUrl.pathname = '/'
const res = NextResponse.rewrite(nextUrl)
res.headers.set('X-From-Middleware', 'true')
return res
}`,
},
})
})
afterAll(() => next.destroy())
it('should not match', async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/static/${next.buildId}/_buildManifest.js`
)
expect(res.status).toBe(200)
expect(res.headers.get('x-from-middleware')).toBeFalsy()
})
})
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