Skip to content

Commit

Permalink
fix(vercel#39396): not invoked with i18n and trailingSlash
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Aug 8, 2022
1 parent 2db89e1 commit 04e418f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 61 deletions.
4 changes: 1 addition & 3 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -208,9 +208,7 @@ function getMiddlewareRegExpStrings(
if (nextConfig.i18n?.locales) {
matcher = `/:nextInternalLocale(${nextConfig.i18n.locales
.map((locale) => escapeStringRegexp(locale))
.join('|')})${
matcher === '/' && !nextConfig.trailingSlash ? '' : matcher
}`
.join('|')})${matcher === '/' ? '' : matcher}`
}

if (nextConfig.basePath) {
Expand Down
128 changes: 70 additions & 58 deletions test/e2e/middleware-matcher/index.test.ts
@@ -1,3 +1,4 @@
/* eslint-disable jest/no-identical-title */
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { check, fetchViaHTTP } from 'next-test-utils'
Expand Down Expand Up @@ -260,7 +261,10 @@ describe('using root matcher', () => {
})
})

describe('using a single matcher with i18n', () => {
describe.each([
{ title: '' },
{ title: ' and trailingSlash', trailingSlash: true },
])('using a single matcher with i18n$title', ({ trailingSlash }) => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
Expand Down Expand Up @@ -297,6 +301,7 @@ describe('using a single matcher with i18n', () => {
`,
'next.config.js': `
module.exports = {
${trailingSlash ? 'trailingSlash: true,' : ''}
i18n: {
localeDetection: false,
locales: ['es', 'en'],
Expand Down Expand Up @@ -358,12 +363,17 @@ describe('using a single matcher with i18n', () => {
})
})

describe('using a single matcher with i18n and basePath', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
describe.each([
{ title: '' },
{ title: ' and trailingSlash', trailingSlash: true },
])(
'using a single matcher with i18n and basePath$title',
({ trailingSlash }) => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page({ message }) {
return <div>
<p>root page</p>
Expand All @@ -374,7 +384,7 @@ describe('using a single matcher with i18n and basePath', () => {
props: { message: \`(\${locale}) Hello from /\` }
})
`,
'pages/[...route].js': `
'pages/[...route].js': `
export default function Page({ message }) {
return <div>
<p>catchall page</p>
Expand All @@ -385,7 +395,7 @@ describe('using a single matcher with i18n and basePath', () => {
props: { message: \`(\${locale}) Hello from /\` + params.route.join("/") }
})
`,
'middleware.js': `
'middleware.js': `
import { NextResponse } from 'next/server'
export const config = { matcher: '/' };
export default (req) => {
Expand All @@ -394,8 +404,9 @@ describe('using a single matcher with i18n and basePath', () => {
return res;
}
`,
'next.config.js': `
'next.config.js': `
module.exports = {
${trailingSlash ? 'trailingSlash: true,' : ''}
basePath: '/root',
i18n: {
localeDetection: false,
Expand All @@ -404,56 +415,57 @@ describe('using a single matcher with i18n and basePath', () => {
}
}
`,
},
dependencies: {},
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it(`adds the header for a matched path`, async () => {
const res1 = await fetchViaHTTP(next.url, `/root`)
expect(await res1.text()).toContain(`(en) Hello from /`)
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(next.url, `/root/es`)
expect(await res2.text()).toContain(`(es) Hello from /`)
expect(res2.headers.get('X-From-Middleware')).toBe('true')
})
})
afterAll(() => next.destroy())

it(`adds the header for a matched path`, async () => {
const res1 = await fetchViaHTTP(next.url, `/root`)
expect(await res1.text()).toContain(`(en) Hello from /`)
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(next.url, `/root/es`)
expect(await res2.text()).toContain(`(es) Hello from /`)
expect(res2.headers.get('X-From-Middleware')).toBe('true')
})

it('adds the header for a mathed root path with /index', async () => {
const res1 = await fetchViaHTTP(next.url, `/root/index`)
expect(await res1.text()).toContain(`(en) Hello from /`)
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(next.url, `/root/es/index`)
expect(await res2.text()).toContain(`(es) Hello from /`)
expect(res2.headers.get('X-From-Middleware')).toBe('true')
})

it(`adds the headers for a matched data path`, async () => {
const res1 = await fetchViaHTTP(
next.url,
`/root/_next/data/${next.buildId}/en.json`,
undefined,
{ headers: { 'x-nextjs-data': '1' } }
)
expect(await res1.json()).toMatchObject({
pageProps: { message: `(en) Hello from /` },
it('adds the header for a mathed root path with /index', async () => {
const res1 = await fetchViaHTTP(next.url, `/root/index`)
expect(await res1.text()).toContain(`(en) Hello from /`)
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(next.url, `/root/es/index`)
expect(await res2.text()).toContain(`(es) Hello from /`)
expect(res2.headers.get('X-From-Middleware')).toBe('true')
})
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(
next.url,
`/root/_next/data/${next.buildId}/es.json`,
undefined,
{ headers: { 'x-nextjs-data': '1' } }
)
expect(await res2.json()).toMatchObject({
pageProps: { message: `(es) Hello from /` },

it(`adds the headers for a matched data path`, async () => {
const res1 = await fetchViaHTTP(
next.url,
`/root/_next/data/${next.buildId}/en.json`,
undefined,
{ headers: { 'x-nextjs-data': '1' } }
)
expect(await res1.json()).toMatchObject({
pageProps: { message: `(en) Hello from /` },
})
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(
next.url,
`/root/_next/data/${next.buildId}/es.json`,
undefined,
{ headers: { 'x-nextjs-data': '1' } }
)
expect(await res2.json()).toMatchObject({
pageProps: { message: `(es) Hello from /` },
})
expect(res2.headers.get('X-From-Middleware')).toBe('true')
})
expect(res2.headers.get('X-From-Middleware')).toBe('true')
})

it(`does not add the header for an unmatched path`, async () => {
const response = await fetchViaHTTP(next.url, `/root/about/me`)
expect(await response.text()).toContain('Hello from /about/me')
expect(response.headers.get('X-From-Middleware')).toBeNull()
})
})
it(`does not add the header for an unmatched path`, async () => {
const response = await fetchViaHTTP(next.url, `/root/about/me`)
expect(await response.text()).toContain('Hello from /about/me')
expect(response.headers.get('X-From-Middleware')).toBeNull()
})
}
)

0 comments on commit 04e418f

Please sign in to comment.