Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(middleware): augments / matcher with /index #39397

Merged
merged 7 commits into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -178,11 +178,12 @@ function getMiddlewareConfig(

function getMiddlewareRegExpStrings(
matcherOrMatchers: unknown,
nextConfig: NextConfig
nextConfig: NextConfig,
{ doNotRecurseOnRoot }: { doNotRecurseOnRoot?: boolean } = {}
): string[] {
if (Array.isArray(matcherOrMatchers)) {
return matcherOrMatchers.flatMap((matcher) =>
getMiddlewareRegExpStrings(matcher, nextConfig)
getMiddlewareRegExpStrings(matcher, nextConfig, { doNotRecurseOnRoot })
)
}

Expand All @@ -198,6 +199,12 @@ function getMiddlewareRegExpStrings(
throw new Error('`matcher`: path matcher must start with /')
}

if (matcher === '/' && !doNotRecurseOnRoot) {
return getMiddlewareRegExpStrings(['/', '/index'], nextConfig, {
doNotRecurseOnRoot: true,
})
}

if (nextConfig.i18n?.locales) {
matcher = `/:nextInternalLocale(${nextConfig.i18n.locales
.map((locale) => escapeStringRegexp(locale))
Expand Down
61 changes: 61 additions & 0 deletions test/e2e/middleware-matcher/index.test.ts
Expand Up @@ -217,6 +217,49 @@ describe('using a single matcher', () => {
})
})

describe('using root matcher', () => {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Home({ message }) {
return <div>Hi there!</div>
}
`,
'middleware.js': `
import { NextResponse } from 'next/server'
export default (req) => {
const res = NextResponse.next();
res.headers.set('X-From-Middleware', 'true');
return res;
}

export const config = { matcher: '/' };
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('adds the header to the /', async () => {
const response = await fetchViaHTTP(next.url, '/')
expect(response.status).toBe(200)
expect(Object.fromEntries(response.headers)).toMatchObject({
'x-from-middleware': 'true',
})
})

it('adds the header to the /index', async () => {
const response = await fetchViaHTTP(next.url, '/index')
expect(response.status).toBe(404)
expect(Object.fromEntries(response.headers)).toMatchObject({
'x-from-middleware': 'true',
})
})
})

describe('using a single matcher with i18n', () => {
let next: NextInstance
beforeAll(async () => {
Expand Down Expand Up @@ -276,6 +319,15 @@ describe('using a single matcher with i18n', () => {
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, `/index`)
expect(await res1.text()).toContain(`(en) Hello from /`)
expect(res1.headers.get('X-From-Middleware')).toBe('true')
const res2 = await fetchViaHTTP(next.url, `/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,
Expand Down Expand Up @@ -367,6 +419,15 @@ describe('using a single matcher with i18n and basePath', () => {
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,
Expand Down