Unverified Commit 40e178e6 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure rewrite does not override params from page in minimal mode (#25074)

This makes sure a catch-all rewrite doesn't override the params from a dynamic route in minimal mode, it also makes sure we don't attempt applying headers and rewrites un-necessarily in minimal mode. 

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
parent eaf74c1c
...@@ -289,7 +289,13 @@ export function getUtils({ ...@@ -289,7 +289,13 @@ export function getUtils({
// on the parsed params, this is used to signal if we need // on the parsed params, this is used to signal if we need
// to parse x-now-route-matches or not // to parse x-now-route-matches or not
const isDefaultValue = Array.isArray(value) const isDefaultValue = Array.isArray(value)
? value.every((val, idx) => val === defaultRouteMatches![key][idx]) ? value.some((val) => {
const defaultValue = defaultRouteMatches![key]
return Array.isArray(defaultValue)
? defaultValue.includes(val)
: defaultValue === val
})
: value === defaultRouteMatches![key] : value === defaultRouteMatches![key]
if (isDefaultValue || typeof value === 'undefined') { if (isDefaultValue || typeof value === 'undefined') {
......
...@@ -818,28 +818,30 @@ export default class Server { ...@@ -818,28 +818,30 @@ export default class Server {
} }
// Headers come very first // Headers come very first
const headers = this.customRoutes.headers.map((r) => { const headers = this.minimalMode
const headerRoute = getCustomRoute(r, 'header') ? []
return { : this.customRoutes.headers.map((r) => {
match: headerRoute.match, const headerRoute = getCustomRoute(r, 'header')
has: headerRoute.has, return {
type: headerRoute.type, match: headerRoute.match,
name: `${headerRoute.type} ${headerRoute.source} header route`, has: headerRoute.has,
fn: async (_req, res, params, _parsedUrl) => { type: headerRoute.type,
const hasParams = Object.keys(params).length > 0 name: `${headerRoute.type} ${headerRoute.source} header route`,
fn: async (_req, res, params, _parsedUrl) => {
for (const header of (headerRoute as Header).headers) { const hasParams = Object.keys(params).length > 0
let { key, value } = header
if (hasParams) { for (const header of (headerRoute as Header).headers) {
key = compileNonPath(key, params) let { key, value } = header
value = compileNonPath(value, params) if (hasParams) {
} key = compileNonPath(key, params)
res.setHeader(key, value) value = compileNonPath(value, params)
} }
return { finished: false } res.setHeader(key, value)
}, }
} as Route return { finished: false }
}) },
} as Route
})
// since initial query values are decoded by querystring.parse // since initial query values are decoded by querystring.parse
// we need to re-encode them here but still allow passing through // we need to re-encode them here but still allow passing through
...@@ -971,12 +973,14 @@ export default class Server { ...@@ -971,12 +973,14 @@ export default class Server {
let afterFiles: Route[] = [] let afterFiles: Route[] = []
let fallback: Route[] = [] let fallback: Route[] = []
if (Array.isArray(this.customRoutes.rewrites)) { if (!this.minimalMode) {
afterFiles = this.customRoutes.rewrites.map(buildRewrite) if (Array.isArray(this.customRoutes.rewrites)) {
} else { afterFiles = this.customRoutes.rewrites.map(buildRewrite)
beforeFiles = this.customRoutes.rewrites.beforeFiles.map(buildRewrite) } else {
afterFiles = this.customRoutes.rewrites.afterFiles.map(buildRewrite) beforeFiles = this.customRoutes.rewrites.beforeFiles.map(buildRewrite)
fallback = this.customRoutes.rewrites.fallback.map(buildRewrite) afterFiles = this.customRoutes.rewrites.afterFiles.map(buildRewrite)
fallback = this.customRoutes.rewrites.fallback.map(buildRewrite)
}
} }
const catchAllRoute: Route = { const catchAllRoute: Route = {
......
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