Pass through original matcher source in manifest (#46753)

This ensures we pass through the original matcher source in the
middleware manifest for posterity.
This commit is contained in:
JJ Kasper 2023-03-03 16:55:41 -08:00 committed by GitHub
parent 25efdfad36
commit 8303c5648a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 17 deletions

View file

@ -30,6 +30,7 @@ export interface MiddlewareMatcher {
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
originalSource: string
}
export interface PageStaticInfo {
@ -197,6 +198,7 @@ export function getMiddlewareMatchers(
return {
...rest,
regexp: parsedPage.regexStr,
originalSource: source,
}
})
}

View file

@ -416,7 +416,7 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
if (isMiddlewareFile(page)) {
middlewareMatchers = staticInfo.middleware?.matchers ?? [
{ regexp: '.*' },
{ regexp: '.*', originalSource: '/:path*' },
]
}

View file

@ -171,14 +171,20 @@ function getCreateAssets(params: {
continue
}
const { namedRegex } = getNamedMiddlewareRegex(
metadata.edgeSSR?.isAppDir ? normalizeAppPath(page) : page,
{
catchAll: !metadata.edgeSSR && !metadata.edgeApiFunction,
}
)
const matcherSource = metadata.edgeSSR?.isAppDir
? normalizeAppPath(page)
: page
const catchAll = !metadata.edgeSSR && !metadata.edgeApiFunction
const { namedRegex } = getNamedMiddlewareRegex(matcherSource, {
catchAll,
})
const matchers = metadata?.edgeMiddleware?.matchers ?? [
{ regexp: namedRegex },
{
regexp: namedRegex,
originalSource: page === '/' && catchAll ? '/:path*' : matcherSource,
},
]
const edgeFunctionDefinition: EdgeFunctionDefinition = {

View file

@ -497,7 +497,7 @@ export default class DevServer extends Server {
}
this.actualMiddlewareFile = rootFile
middlewareMatchers = staticInfo.middleware?.matchers || [
{ regexp: '.*' },
{ regexp: '.*', originalSource: '/:path*' },
]
continue
}

View file

@ -88,7 +88,7 @@ export async function makeResolver(
if (middleware.files?.length) {
const matchers = middleware.matcher
? getMiddlewareMatchers(middleware.matcher, nextConfig)
: [{ regexp: '.*' }]
: [{ regexp: '.*', originalSource: '/:path*' }]
// @ts-expect-error
devServer.middleware = {
page: '/',

View file

@ -61,6 +61,7 @@ createNextDescribe(
expect(manifest.functions['/(group)/group/page'].matchers).toEqual([
{
regexp: '^/group$',
originalSource: '/group',
},
])
})

View file

@ -109,7 +109,7 @@ describe('Middleware Runtime', () => {
`/_next/static/${next.buildId}/_devMiddlewareManifest.json`
)
const matchers = await res.json()
expect(matchers).toEqual([{ regexp: '.*' }])
expect(matchers).toEqual([{ regexp: '.*', originalSource: '/:path*' }])
})
}
@ -131,7 +131,7 @@ describe('Middleware Runtime', () => {
]),
name: 'middleware',
page: '/',
matchers: [{ regexp: '^/.*$' }],
matchers: [{ regexp: '^/.*$', originalSource: '/:path*' }],
wasm: [],
assets: [],
regions: 'auto',

View file

@ -115,7 +115,7 @@ describe('Middleware Runtime trailing slash', () => {
name: 'middleware',
env: [],
page: '/',
matchers: [{ regexp: '^/.*$' }],
matchers: [{ regexp: '^/.*$', originalSource: '/:path*' }],
wasm: [],
assets: [],
},

View file

@ -194,7 +194,9 @@ describe('Switchable runtime', () => {
],
name: 'pages/api/hello',
page: '/api/hello',
matchers: [{ regexp: '^/api/hello$' }],
matchers: [
{ regexp: '^/api/hello$', originalSource: '/api/hello' },
],
wasm: [],
},
'/api/edge': {
@ -205,7 +207,9 @@ describe('Switchable runtime', () => {
],
name: 'pages/api/edge',
page: '/api/edge',
matchers: [{ regexp: '^/api/edge$' }],
matchers: [
{ regexp: '^/api/edge$', originalSource: '/api/edge' },
],
wasm: [],
},
},
@ -626,7 +630,9 @@ describe('Switchable runtime', () => {
],
name: 'pages/api/hello',
page: '/api/hello',
matchers: [{ regexp: '^/api/hello$' }],
matchers: [
{ regexp: '^/api/hello$', originalSource: '/api/hello' },
],
wasm: [],
},
'/api/edge': {
@ -637,7 +643,9 @@ describe('Switchable runtime', () => {
],
name: 'pages/api/edge',
page: '/api/edge',
matchers: [{ regexp: '^/api/edge$' }],
matchers: [
{ regexp: '^/api/edge$', originalSource: '/api/edge' },
],
wasm: [],
},
},