Unverified Commit fca768d3 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update to rely on dynamic param values from proxy (#12608)

This updates to rely on query values from the proxy when capable to ensure the correct values are provided. This does not remove the `x-now-route-matches` as this is still needed until the values are provided in the query instead for iSSG

x-ref: https://github.com/zeit/now/pull/4196
x-ref: https://github.com/zeit/next.js/pull/12250
x-ref:

Tested against the following updated fixtures from https://github.com/vercel/vercel/pull/4682 with https://github.com/ijjk/next-update-loader

- 08-custom-routes-catchall
- 10-export-cache-headers
- 16-base-path
- 22-ssg-v2
- 23-custom-routes-verbose
parent 1f125f8a
......@@ -29,6 +29,8 @@ export type ServerlessLoaderQuery = {
loadedEnvFiles: string
}
const vercelHeader = 'x-vercel-id'
const nextServerlessLoader: loader.Loader = function () {
const {
distDir,
......@@ -62,6 +64,30 @@ const nextServerlessLoader: loader.Loader = function () {
JSON.parse(previewProps) as __ApiPreviewProps
)
const collectDynamicRouteParams = pageIsDynamicRoute
? `
function collectDynamicRouteParams(query) {
const routeRegex = getRouteRegex("${page}")
return Object.keys(routeRegex.groups)
.reduce((prev, key) => {
let value = query[key]
${
''
// query values from the proxy aren't already split into arrays
// so make sure to normalize catch-all values
}
if (routeRegex.groups[key].repeat) {
value = value.split('/')
}
prev[key] = value
return prev
}, {})
}
`
: ''
const envLoading = `
const { processEnv } = require('next/dist/lib/load-env-config')
processEnv(${loadedEnvFiles})
......@@ -113,6 +139,7 @@ const nextServerlessLoader: loader.Loader = function () {
params,
parsedUrl.query
)
Object.assign(parsedUrl.query, parsedDestination.query, params)
delete parsedDestination.query
......@@ -169,6 +196,7 @@ const nextServerlessLoader: loader.Loader = function () {
${rewriteImports}
${dynamicRouteMatcher}
${collectDynamicRouteParams}
${handleRewrites}
......@@ -177,11 +205,19 @@ const nextServerlessLoader: loader.Loader = function () {
await initServer()
${handleBasePath}
// We need to trust the dynamic route params from the proxy
// to ensure we are using the correct values
const trustQuery = req.headers['${vercelHeader}']
const parsedUrl = handleRewrites(parse(req.url, true))
const params = ${
pageIsDynamicRoute
? `dynamicRouteMatcher(parsedUrl.pathname)`
? `
trustQuery
? collectDynamicRouteParams(parsedUrl.query)
: dynamicRouteMatcher(parsedUrl.pathname)
`
: `{}`
}
......@@ -252,6 +288,7 @@ const nextServerlessLoader: loader.Loader = function () {
export const unstable_getServerProps = ComponentInfo['unstable_getServerProp' + 's']
${dynamicRouteMatcher}
${collectDynamicRouteParams}
${handleRewrites}
export const config = ComponentInfo['confi' + 'g'] || {}
......@@ -282,7 +319,10 @@ const nextServerlessLoader: loader.Loader = function () {
let parsedUrl
try {
parsedUrl = handleRewrites(parse(req.url, true))
// We need to trust the dynamic route params from the proxy
// to ensure we are using the correct values
const trustQuery = !getStaticProps && req.headers['${vercelHeader}']
const parsedUrl = handleRewrites(parse(req.url, true))
if (parsedUrl.pathname.match(/_next\\/data/)) {
_nextData = true
......@@ -313,7 +353,16 @@ const nextServerlessLoader: loader.Loader = function () {
${
pageIsDynamicRoute
? `const params = fromExport && !getStaticProps && !getServerSideProps ? {} : dynamicRouteMatcher(parsedUrl.pathname) || {};`
? `
const params = (
fromExport &&
!getStaticProps &&
!getServerSideProps
) ? {}
: trustQuery
? collectDynamicRouteParams(parsedUrl.query)
: dynamicRouteMatcher(parsedUrl.pathname) || {};
`
: `const params = {};`
}
${
......
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