Skip to content

Commit

Permalink
Merge branch 'canary' into example/serverless-nextjs
Browse files Browse the repository at this point in the history
  • Loading branch information
lone-cloud authored Jun 23, 2020
2 parents 010830e + fca768d commit a9a676b
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions packages/next/build/webpack/loaders/next-serverless-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export type ServerlessLoaderQuery = {
loadedEnvFiles: string
}

const vercelHeader = 'x-vercel-id'

const nextServerlessLoader: loader.Loader = function () {
const {
distDir,
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -113,6 +139,7 @@ const nextServerlessLoader: loader.Loader = function () {
params,
parsedUrl.query
)
Object.assign(parsedUrl.query, parsedDestination.query, params)
delete parsedDestination.query
Expand Down Expand Up @@ -169,6 +196,7 @@ const nextServerlessLoader: loader.Loader = function () {
${rewriteImports}
${dynamicRouteMatcher}
${collectDynamicRouteParams}
${handleRewrites}
Expand All @@ -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)
`
: `{}`
}
Expand Down Expand Up @@ -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'] || {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {};`
}
${
Expand Down

0 comments on commit a9a676b

Please sign in to comment.