Unverified Commit f2622b36 authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

Avoid deep-cloning URLs during rewrite handling

Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent cf10c50a
......@@ -72,6 +72,46 @@ describe('getParamsFromRouteMatches', () => {
})
})
describe('handleRewrites', () => {
it('does not mutate the original URL or query', () => {
const { handleRewrites } = getServerUtils({
page: '/destination',
basePath: '',
rewrites: {
beforeFiles: [
{
source: '/source',
destination: '/destination?added=value&shared=updated',
},
],
},
i18n: undefined,
pageIsDynamic: false,
caseSensitive: false,
})
const shared = ['one', 'two']
const parsedUrl = {
pathname: '/source',
query: { keep: 'yes', shared },
}
const { rewrittenParsedUrl } = handleRewrites(
{} as Parameters<typeof handleRewrites>[0],
parsedUrl
)
expect(parsedUrl).toEqual({
pathname: '/source',
query: { keep: 'yes', shared: ['one', 'two'] },
})
expect(parsedUrl.query.shared).toBe(shared)
expect(rewrittenParsedUrl).toMatchObject({
pathname: '/destination',
query: { keep: 'yes', shared: 'updated', added: 'value' },
})
})
})
describe('normalizeDynamicRouteParams', () => {
it('should reject encoded default placeholders for dynamic params', () => {
const { normalizeDynamicRouteParams } = getServerUtils({
......
......@@ -250,11 +250,13 @@ export function getServerUtils({
req: BaseNextRequest | IncomingMessage,
parsedUrl: DeepReadonly<NextUrlWithParsedQuery>
) {
// Here we deep clone the parsedUrl to avoid mutating the original. We also
// cast this to a mutable type so we can mutate it within this scope.
const rewrittenParsedUrl = structuredClone(
parsedUrl
) as NextUrlWithParsedQuery
// Clone the URL and query before applying rewrites so the original is not
// mutated. Rewrite processing only changes top-level query properties, so
// cloning nested values would be unnecessary work on every request.
const rewrittenParsedUrl = {
...parsedUrl,
query: { ...parsedUrl.query },
} as NextUrlWithParsedQuery
const rewriteParams: Record<string, string> = {}
let fsPathname = rewrittenParsedUrl.pathname
......
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