Skip to content

Commit

Permalink
Merge branch 'canary' into fix/streaming-404
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Nov 6, 2021
2 parents 76c1d0a + d2c1888 commit baf7ef4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
9 changes: 7 additions & 2 deletions packages/next/build/webpack/plugins/serverless-plugin.ts
Expand Up @@ -14,7 +14,8 @@ export class ServerlessPlugin {
hook.tap('ServerlessPlugin', (chunks) => {
for (const chunk of chunks) {
// If chunk is not an entry point skip them
if (!chunk.hasEntryModule()) {
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
if (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0) {
continue
}

Expand All @@ -26,7 +27,11 @@ export class ServerlessPlugin {
dynamicChunk
)) {
// Add module back into the entry chunk
chunk.addModule(module)
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
if (!compilation.chunkGraph.isModuleInChunk(module, chunk)) {
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
compilation.chunkGraph.connectChunkAndModule(chunk, module)
}
}
}
}
Expand Down
82 changes: 46 additions & 36 deletions packages/next/server/next-server.ts
Expand Up @@ -425,51 +425,61 @@ export default class Server {
rewrites: combinedRewrites,
})

utils.handleRewrites(req, parsedUrl)

// interpolate dynamic params and normalize URL if needed
if (pageIsDynamic) {
let params: ParsedUrlQuery | false = {}
try {
utils.handleRewrites(req, parsedUrl)

Object.assign(parsedUrl.query, query)
const paramsResult = utils.normalizeDynamicRouteParams(parsedUrl.query)
// interpolate dynamic params and normalize URL if needed
if (pageIsDynamic) {
let params: ParsedUrlQuery | false = {}

if (paramsResult.hasValidParams) {
params = paramsResult.params
} else if (req.headers['x-now-route-matches']) {
const opts: Record<string, string> = {}
params = utils.getParamsFromRouteMatches(
req,
opts,
(parsedUrl.query.__nextLocale as string | undefined) || ''
Object.assign(parsedUrl.query, query)
const paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query
)

if (opts.locale) {
parsedUrl.query.__nextLocale = opts.locale
if (paramsResult.hasValidParams) {
params = paramsResult.params
} else if (req.headers['x-now-route-matches']) {
const opts: Record<string, string> = {}
params = utils.getParamsFromRouteMatches(
req,
opts,
(parsedUrl.query.__nextLocale as string | undefined) || ''
)

if (opts.locale) {
parsedUrl.query.__nextLocale = opts.locale
}
} else {
params = utils.dynamicRouteMatcher!(matchedPathnameNoExt)
}
} else {
params = utils.dynamicRouteMatcher!(matchedPathnameNoExt)
}

if (params) {
params = utils.normalizeDynamicRouteParams(params).params
if (params) {
params = utils.normalizeDynamicRouteParams(params).params

matchedPathname = utils.interpolateDynamicPath(
matchedPathname,
params
)
req.url = utils.interpolateDynamicPath(req.url!, params)
}
matchedPathname = utils.interpolateDynamicPath(
matchedPathname,
params
)
req.url = utils.interpolateDynamicPath(req.url!, params)
}

if (reqUrlIsDataUrl && matchedPathIsDataUrl) {
req.url = formatUrl({
...parsedPath,
pathname: matchedPathname,
})
}
if (reqUrlIsDataUrl && matchedPathIsDataUrl) {
req.url = formatUrl({
...parsedPath,
pathname: matchedPathname,
})
}

Object.assign(parsedUrl.query, params)
utils.normalizeVercelUrl(req, true)
Object.assign(parsedUrl.query, params)
utils.normalizeVercelUrl(req, true)
}
} catch (err) {
if (err instanceof DecodeError) {
res.statusCode = 400
return this.renderError(null, req, res, '/_error', {})
}
throw err
}

parsedUrl.pathname = `${basePath || ''}${
Expand Down
19 changes: 19 additions & 0 deletions test/production/required-server-files.test.ts
Expand Up @@ -39,6 +39,10 @@ describe('should set-up next', () => {
source: '/some-catch-all/:path*',
destination: '/',
},
{
source: '/to-dynamic/:path',
destination: '/dynamic/:path',
},
]
},
},
Expand Down Expand Up @@ -553,6 +557,21 @@ describe('should set-up next', () => {
})
})

it('should handle bad request correctly with rewrite', async () => {
const res = await fetchViaHTTP(
appPort,
'/to-dynamic/%c0.%c0.',
'?path=%c0.%c0.',
{
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
)
expect(res.status).toBe(400)
expect(await res.text()).toContain('Bad Request')
})

it('should bubble error correctly for gip page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
Expand Down

0 comments on commit baf7ef4

Please sign in to comment.