Skip to content

Commit b1ef76d

Browse files
authored
Fix asPath normalizing for non-dynamic pages (vercel#15960)
1 parent db69e47 commit b1ef76d

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

packages/next/build/webpack/loaders/next-serverless-loader.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,20 @@ const nextServerlessLoader: loader.Loader = function () {
439439
440440
// make sure to normalize req.url on Vercel to strip dynamic params
441441
// from the query which are added during routing
442-
if (trustQuery) {
443-
const _parsedUrl = parseUrl(req.url, true)
444-
delete _parsedUrl.search
442+
${
443+
pageIsDynamicRoute
444+
? `
445+
if (trustQuery) {
446+
const _parsedUrl = parseUrl(req.url, true)
447+
delete _parsedUrl.search
445448
446-
for (const param of Object.keys(defaultRouteRegex.groups)) {
447-
delete _parsedUrl.query[param]
449+
for (const param of Object.keys(defaultRouteRegex.groups)) {
450+
delete _parsedUrl.query[param]
451+
}
452+
req.url = formatUrl(_parsedUrl)
448453
}
449-
req.url = formatUrl(_parsedUrl)
454+
`
455+
: ''
450456
}
451457
452458
const isFallback = parsedUrl.query.__nextFallback
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export default function Page(props) {
2-
return <div>about</div>
2+
return <div id="content">about</div>
3+
}
4+
5+
export const getServerSideProps = () => {
6+
return {
7+
props: {},
8+
}
39
}

test/integration/dynamic-optional-routing/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const server = http.createServer((req, res) => {
1616
const { pathname } = url.parse(req.url)
1717

1818
switch (pathname) {
19+
case '/about':
20+
return render('/about.js')
1921
case '/api/post':
2022
case '/api/post/hello':
2123
case '/api/post/hello/world': {

test/integration/dynamic-optional-routing/test/index.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ describe('Dynamic Optional Routing', () => {
370370
}).then((res) => res.text())
371371
}
372372

373+
it('should render normal (non-dynamic) page', async () => {
374+
const html = await render('/about')
375+
const $ = cheerio.load(html)
376+
expect($('#content').text()).toBe('about')
377+
})
378+
373379
it('should render top level optional catch-all root', async () => {
374380
const html = await render('/', { optionalName: '' })
375381
const $ = cheerio.load(html)

0 commit comments

Comments
 (0)