Skip to content

Commit

Permalink
Pass through original matcher source in manifest (#46753)
Browse files Browse the repository at this point in the history
This ensures we pass through the original matcher source in the
middleware manifest for posterity.
  • Loading branch information
ijjk committed Mar 4, 2023
1 parent 25efdfa commit 8303c56
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 17 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface MiddlewareMatcher {
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
originalSource: string
}

export interface PageStaticInfo {
Expand Down Expand Up @@ -197,6 +198,7 @@ export function getMiddlewareMatchers(
return {
...rest,
regexp: parsedPage.regexStr,
originalSource: source,
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {

if (isMiddlewareFile(page)) {
middlewareMatchers = staticInfo.middleware?.matchers ?? [
{ regexp: '.*' },
{ regexp: '.*', originalSource: '/:path*' },
]
}

Expand Down
20 changes: 13 additions & 7 deletions packages/next/src/build/webpack/plugins/middleware-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ export default class DevServer extends Server {
}
this.actualMiddlewareFile = rootFile
middlewareMatchers = staticInfo.middleware?.matchers || [
{ regexp: '.*' },
{ regexp: '.*', originalSource: '/:path*' },
]
continue
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/route-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/',
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-edge/app-edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ createNextDescribe(
expect(manifest.functions['/(group)/group/page'].matchers).toEqual([
{
regexp: '^/group$',
originalSource: '/group',
},
])
})
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*' }])
})
}

Expand All @@ -131,7 +131,7 @@ describe('Middleware Runtime', () => {
]),
name: 'middleware',
page: '/',
matchers: [{ regexp: '^/.*$' }],
matchers: [{ regexp: '^/.*$', originalSource: '/:path*' }],
wasm: [],
assets: [],
regions: 'auto',
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/middleware-trailing-slash/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('Middleware Runtime trailing slash', () => {
name: 'middleware',
env: [],
page: '/',
matchers: [{ regexp: '^/.*$' }],
matchers: [{ regexp: '^/.*$', originalSource: '/:path*' }],
wasm: [],
assets: [],
},
Expand Down
16 changes: 12 additions & 4 deletions test/e2e/switchable-runtime/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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: [],
},
},
Expand Down Expand Up @@ -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': {
Expand All @@ -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: [],
},
},
Expand Down

0 comments on commit 8303c56

Please sign in to comment.