diff --git a/packages/next/build/webpack/loaders/next-serverless-loader.ts b/packages/next/build/webpack/loaders/next-serverless-loader.ts index 6d678bb227ba1..e21329d52f1df 100644 --- a/packages/next/build/webpack/loaders/next-serverless-loader.ts +++ b/packages/next/build/webpack/loaders/next-serverless-loader.ts @@ -70,6 +70,7 @@ const nextServerlessLoader: loader.Loader = function() { } else { return ` import {parse} from 'url' + import {parse as parseQs} from 'querystring' import {renderToHTML} from 'next/dist/next-server/server/render'; import {sendHTML} from 'next/dist/next-server/server/send-html'; ${ @@ -138,7 +139,30 @@ const nextServerlessLoader: loader.Loader = function() { // removing reliance on `req.url` and using `req.query` instead // (which is needed for "custom routes" anyway). isDynamicRoute(page) - ? `const nowParams = (req.headers && req.headers["x-now-route-params"]) ? querystring.parse(req.headers["x-now-route-params"]) : null;` + ? `const nowParams = req.headers && req.headers["x-now-route-params"] + ? getRouteMatcher( + (function() { + const { re, groups } = getRouteRegex("${page}"); + return { + re: { + // Simulate a RegExp match from the \`req.url\` input + exec: str => { + const obj = parseQs(str); + return Object.keys(obj).reduce( + (prev, key) => + Object.assign(prev, { + [key]: encodeURIComponent(obj[key]) + }), + {} + ); + } + }, + groups + }; + })() + )(req.headers["x-now-route-params"]) + : null; + ` : `const nowParams = null;` } const result = await renderToHTML(req, res, "${page}", Object.assign({}, unstable_getStaticProps ? {} : parsedUrl.query, nowParams ? nowParams : params, sprData ? { _nextSprData: '1' } : {}), renderOpts) diff --git a/test/integration/serverless/pages/dr/[slug].js b/test/integration/serverless/pages/dr/[slug].js new file mode 100644 index 0000000000000..0f29909605636 --- /dev/null +++ b/test/integration/serverless/pages/dr/[slug].js @@ -0,0 +1,5 @@ +const SlugPage = ({ query }) =>