From b304a183626d2fa17b2c66eb3e83ba2aba2bd97e Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 30 Nov 2022 00:55:13 +0100 Subject: [PATCH] Fix matchers in middleware manifest (#43549) Currently the generated matcher will be `"regexp": "^\(group\)/group$"` for groups which doesn't match the correct route. This is somehow related to #43458, but it only fixes the problem partially. Some other changes need to be made in the build process. ## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- .../next/build/webpack/plugins/middleware-plugin.ts | 10 +++++++--- test/e2e/app-dir/app-edge.test.ts | 13 +++++++++++++ .../e2e/app-dir/app-edge/app/(group)/group/page.tsx | 8 ++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/e2e/app-dir/app-edge/app/(group)/group/page.tsx diff --git a/packages/next/build/webpack/plugins/middleware-plugin.ts b/packages/next/build/webpack/plugins/middleware-plugin.ts index 709eb8a07a2c..c19104408754 100644 --- a/packages/next/build/webpack/plugins/middleware-plugin.ts +++ b/packages/next/build/webpack/plugins/middleware-plugin.ts @@ -28,6 +28,7 @@ import { import { Telemetry } from '../../../telemetry/storage' import { traceGlobals } from '../../../trace/shared' import { EVENT_BUILD_FEATURE_USAGE } from '../../../telemetry/events' +import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths' export interface EdgeFunctionDefinition { env: string[] @@ -160,9 +161,12 @@ function getCreateAssets(params: { continue } - const { namedRegex } = getNamedMiddlewareRegex(page, { - catchAll: !metadata.edgeSSR && !metadata.edgeApiFunction, - }) + const { namedRegex } = getNamedMiddlewareRegex( + metadata.edgeSSR?.isAppDir ? normalizeAppPath(page) : page, + { + catchAll: !metadata.edgeSSR && !metadata.edgeApiFunction, + } + ) const matchers = metadata?.edgeMiddleware?.matchers ?? [ { regexp: namedRegex }, ] diff --git a/test/e2e/app-dir/app-edge.test.ts b/test/e2e/app-dir/app-edge.test.ts index 340d99f4491f..25873b92fa38 100644 --- a/test/e2e/app-dir/app-edge.test.ts +++ b/test/e2e/app-dir/app-edge.test.ts @@ -54,4 +54,17 @@ describe('app-dir edge SSR', () => { }, /Edge!/) }) } + + if (!(globalThis as any).isNextDev) { + it('should generate matchers correctly in middleware manifest', async () => { + const manifest = JSON.parse( + await next.readFile('.next/server/middleware-manifest.json') + ) + expect(manifest.functions['/(group)/group/page'].matchers).toEqual([ + { + regexp: '^/group$', + }, + ]) + }) + } }) diff --git a/test/e2e/app-dir/app-edge/app/(group)/group/page.tsx b/test/e2e/app-dir/app-edge/app/(group)/group/page.tsx new file mode 100644 index 000000000000..619a3b6c8d20 --- /dev/null +++ b/test/e2e/app-dir/app-edge/app/(group)/group/page.tsx @@ -0,0 +1,8 @@ +export default function Page() { + if ('EdgeRuntime' in globalThis) { + return

Edge!

+ } + return

Node!

+} + +export const runtime = 'experimental-edge'