Unverified Commit 3003bff6 authored by Sean Massa's avatar Sean Massa Committed by GitHub

ensure original matcher source is free of mutations (#47980)

In a previous PR (https://github.com/vercel/next.js/pull/46753), the
`originalSource` was saved to be passed along through the build process.
This was done a bit too late in the flow, which made `originalSource`
include some transformations.

Because `originalSource` is used for display purposes, we need it
unmodified. This PR preserves `originalSource`.
parent 805183e3
...@@ -166,9 +166,14 @@ export function getMiddlewareMatchers( ...@@ -166,9 +166,14 @@ export function getMiddlewareMatchers(
} }
const { i18n } = nextConfig const { i18n } = nextConfig
let routes = matchers.map( const originalSourceMap = new Map<Middleware, string>()
(m) => (typeof m === 'string' ? { source: m } : m) as Middleware let routes = matchers.map((m) => {
) let middleware = (typeof m === 'string' ? { source: m } : m) as Middleware
if (middleware) {
originalSourceMap.set(middleware, middleware.source)
}
return middleware
})
// check before we process the routes and after to ensure // check before we process the routes and after to ensure
// they are still valid // they are still valid
...@@ -195,7 +200,8 @@ export function getMiddlewareMatchers( ...@@ -195,7 +200,8 @@ export function getMiddlewareMatchers(
source = `${nextConfig.basePath}${source}` source = `${nextConfig.basePath}${source}`
} }
return { ...r, source } r.source = source
return r
}) })
checkCustomRoutes(routes, 'middleware') checkCustomRoutes(routes, 'middleware')
...@@ -208,10 +214,12 @@ export function getMiddlewareMatchers( ...@@ -208,10 +214,12 @@ export function getMiddlewareMatchers(
throw new Error(`Invalid source: ${source}`) throw new Error(`Invalid source: ${source}`)
} }
const originalSource = originalSourceMap.get(r)
return { return {
...rest, ...rest,
regexp: parsedPage.regexStr, regexp: parsedPage.regexStr,
originalSource: source, originalSource: originalSource || source,
} }
}) })
} }
......
...@@ -146,9 +146,7 @@ describe('Errors on invalid custom middleware matchers', () => { ...@@ -146,9 +146,7 @@ describe('Errors on invalid custom middleware matchers', () => {
stderr += msg stderr += msg
}, },
}) })
await fetchViaHTTP(port, '/') await fetchViaHTTP(port, '/').catch(() => {})
// suppress error
.catch(() => {})
return stderr return stderr
} }
}) })
......
/* eslint-env jest */
import { getMiddlewareMatchers } from 'next/dist/build/analysis/get-page-static-info'
describe('get-page-static-infos', () => {
describe('getMiddlewareMatchers', () => {
it('sets originalSource with one matcher', () => {
const matchers = '/middleware/path'
const expected = [
{
originalSource: '/middleware/path',
regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(.json)?[\\/#\\?]?$',
},
]
const result = getMiddlewareMatchers(matchers, { i18n: undefined })
expect(result).toStrictEqual(expected)
})
it('sets originalSource with multiple matchers', () => {
const matchers = ['/middleware/path', '/middleware/another-path']
const expected = [
{
originalSource: '/middleware/path',
regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/path(.json)?[\\/#\\?]?$',
},
{
originalSource: '/middleware/another-path',
regexp:
'^(?:\\/(_next\\/data\\/[^/]{1,}))?\\/middleware\\/another-path(.json)?[\\/#\\?]?$',
},
]
const result = getMiddlewareMatchers(matchers, { i18n: undefined })
expect(result).toStrictEqual(expected)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment