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

Fix invalid middleware regexp in manifest #37492

Merged
merged 1 commit into from
Jun 6, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/build/webpack/plugins/middleware-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ function getCreateAssets(params: {
const { namedRegex } = getNamedMiddlewareRegex(page, {
catchAll: !metadata.edgeSSR,
})
const regexp = metadata?.edgeMiddleware?.matcherRegexp ?? namedRegex
const regexp = metadata?.edgeMiddleware?.matcherRegexp || namedRegex

middlewareManifest.middleware[page] = {
env: Array.from(metadata.env),
Expand Down
6 changes: 6 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,12 @@ function getMiddlewareMatcher(
return stored
}

if (typeof info.regexp !== 'string' || !info.regexp) {
throw new Error(
`Invariant: invalid regexp for middleware ${JSON.stringify(info)}`
)
}

const matcher = getRouteMatcher({ re: new RegExp(info.regexp), groups: {} })
MiddlewareMatcherCache.set(info, matcher)
return matcher
Expand Down
16 changes: 16 additions & 0 deletions test/integration/middleware-general/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ describe('Middleware Runtime', () => {
})
})

it('should have valid middleware field in manifest', async () => {
const manifest = await fs.readJSON(
join(context.appDir, '.next/server/middleware-manifest.json')
)
expect(manifest.middleware).toEqual({
'/': {
env: ['MIDDLEWARE_TEST'],
files: ['server/edge-runtime-webpack.js', 'server/middleware.js'],
name: 'middleware',
page: '/',
regexp: '^/.*$',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the value prior to the change? An empty string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah an empty string so it passed the nullish check

wasm: [],
},
})
})

it('should have middleware warning during build', () => {
expect(context.buildLogs.output).toContain(middlewareWarning)
})
Expand Down