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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through original matcher source in manifest #46753

Merged
merged 8 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -491,7 +491,7 @@ export default class DevServer extends Server {
if (isMiddlewareFile(rootFile)) {
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