Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
668466bc
Unverified
Commit
668466bc
authored
Jun 13, 2022
by
JJ Kasper
Committed by
GitHub
Jun 13, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix rewrite/dynamic interpolation E2E cases (#37669)
parent
3b9f180d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
27 deletions
+54
-27
utils.ts
...ext/build/webpack/loaders/next-serverless-loader/utils.ts
+8
-2
base-server.ts
packages/next/server/base-server.ts
+29
-25
required-server-files.test.ts
test/production/required-server-files.test.ts
+17
-0
No files found.
packages/next/build/webpack/loaders/next-serverless-loader/utils.ts
View file @
668466bc
...
@@ -340,7 +340,10 @@ export function getUtils({
...
@@ -340,7 +340,10 @@ export function getUtils({
}
}
}
}
function
normalizeDynamicRouteParams
(
params
:
ParsedUrlQuery
)
{
function
normalizeDynamicRouteParams
(
params
:
ParsedUrlQuery
,
ignoreOptional
?:
boolean
)
{
let
hasValidParams
=
true
let
hasValidParams
=
true
if
(
!
defaultRouteRegex
)
return
{
params
,
hasValidParams
:
false
}
if
(
!
defaultRouteRegex
)
return
{
params
,
hasValidParams
:
false
}
...
@@ -361,7 +364,10 @@ export function getUtils({
...
@@ -361,7 +364,10 @@ export function getUtils({
})
})
:
value
?.
includes
(
defaultValue
as
string
)
:
value
?.
includes
(
defaultValue
as
string
)
if
(
isDefaultValue
||
(
typeof
value
===
'undefined'
&&
!
isOptional
))
{
if
(
isDefaultValue
||
(
typeof
value
===
'undefined'
&&
!
(
isOptional
&&
ignoreOptional
))
)
{
hasValidParams
=
false
hasValidParams
=
false
}
}
...
...
packages/next/server/base-server.ts
View file @
668466bc
...
@@ -517,27 +517,20 @@ export default abstract class Server<ServerOptions extends Options = Options> {
...
@@ -517,27 +517,20 @@ export default abstract class Server<ServerOptions extends Options = Options> {
parsedUrl
.
query
parsedUrl
.
query
)
)
// for prerendered ISR paths we attempt parsing the route
// params from the URL directly as route-matches may not
// contain the correct values due to the filesystem path
// matching before the dynamic route has been matched
if
(
if
(
!
isDynamicRoute
(
normalizedUrlPath
)
||
!
paramsResult
.
hasValidParams
&&
!
isDynamicRoute
(
matchedPath
)
pageIsDynamic
&&
!
isDynamicRoute
(
normalizedUrlPath
)
)
{
)
{
// we favor matching against req.url although if there's a
let
matcherParams
=
utils
.
dynamicRouteMatcher
?.(
normalizedUrlPath
)
// rewrite and it's SSR we use the x-matched-path instead
let
matcherRes
=
utils
.
dynamicRouteMatcher
?.(
normalizedUrlPath
)
if
(
!
matcherRes
)
{
if
(
matcherParams
)
{
matcherRes
=
utils
.
dynamicRouteMatcher
?.(
matchedPath
)
Object
.
assign
(
paramsResult
.
params
,
matcherParams
)
}
paramsResult
.
hasValidParams
=
true
const
parsedResult
=
utils
.
normalizeDynamicRouteParams
(
matcherRes
||
{}
)
if
(
parsedResult
.
hasValidParams
)
{
if
(
paramsResult
.
hasValidParams
)
{
Object
.
assign
(
paramsResult
.
params
,
parsedResult
.
params
)
}
else
{
paramsResult
=
parsedResult
}
}
}
}
}
...
@@ -547,9 +540,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
...
@@ -547,9 +540,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if
(
if
(
req
.
headers
[
'x-now-route-matches'
]
&&
req
.
headers
[
'x-now-route-matches'
]
&&
(
!
paramsResult
.
hasValidParams
||
isDynamicRoute
(
matchedPath
)
&&
(
isDynamicRoute
(
matchedPath
)
&&
!
paramsResult
.
hasValidParams
isDynamicRoute
(
normalizedUrlPath
)))
)
{
)
{
const
opts
:
Record
<
string
,
string
>
=
{}
const
opts
:
Record
<
string
,
string
>
=
{}
const
routeParams
=
utils
.
getParamsFromRouteMatches
(
const
routeParams
=
utils
.
getParamsFromRouteMatches
(
...
@@ -561,17 +553,29 @@ export default abstract class Server<ServerOptions extends Options = Options> {
...
@@ -561,17 +553,29 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if
(
opts
.
locale
)
{
if
(
opts
.
locale
)
{
parsedUrl
.
query
.
__nextLocale
=
opts
.
locale
parsedUrl
.
query
.
__nextLocale
=
opts
.
locale
}
}
paramsResult
=
utils
.
normalizeDynamicRouteParams
(
routeParams
)
paramsResult
=
utils
.
normalizeDynamicRouteParams
(
routeParams
,
true
)
if
(
paramsResult
.
hasValidParams
)
{
if
(
paramsResult
.
hasValidParams
)
{
params
=
paramsResult
.
params
params
=
paramsResult
.
params
}
}
}
}
// handle the actual dynamic route name being requested
if
(
pageIsDynamic
&&
utils
.
defaultRouteMatches
&&
normalizedUrlPath
===
srcPathname
&&
!
paramsResult
.
hasValidParams
&&
!
utils
.
normalizeDynamicRouteParams
({
...
params
},
true
)
.
hasValidParams
)
{
params
=
utils
.
defaultRouteMatches
}
if
(
params
)
{
if
(
params
)
{
if
(
!
paramsResult
.
hasValidParams
)
{
params
=
utils
.
normalizeDynamicRouteParams
(
params
).
params
}
matchedPath
=
utils
.
interpolateDynamicPath
(
srcPathname
,
params
)
matchedPath
=
utils
.
interpolateDynamicPath
(
srcPathname
,
params
)
req
.
url
=
utils
.
interpolateDynamicPath
(
req
.
url
!
,
params
)
req
.
url
=
utils
.
interpolateDynamicPath
(
req
.
url
!
,
params
)
}
}
...
...
test/production/required-server-files.test.ts
View file @
668466bc
...
@@ -1029,6 +1029,23 @@ describe('should set-up next', () => {
...
@@ -1029,6 +1029,23 @@ describe('should set-up next', () => {
const
html
=
await
res
.
text
()
const
html
=
await
res
.
text
()
const
$
=
cheerio
.
load
(
html
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'#slug-page'
).
text
()).
toBe
(
'[slug] page'
)
expect
(
$
(
'#slug-page'
).
text
()).
toBe
(
'[slug] page'
)
expect
(
JSON
.
parse
(
$
(
'#router'
).
text
()).
query
).
toEqual
({
slug
:
'slug-1'
,
})
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/[slug]'
,
undefined
,
{
headers
:
{
'x-matched-path'
:
'/[slug]'
,
},
redirect
:
'manual'
,
})
const
html2
=
await
res2
.
text
()
const
$2
=
cheerio
.
load
(
html2
)
expect
(
$2
(
'#slug-page'
).
text
()).
toBe
(
'[slug] page'
)
expect
(
JSON
.
parse
(
$2
(
'#router'
).
text
()).
query
).
toEqual
({
slug
:
'[slug]'
,
})
})
})
it
(
'should have correct asPath on dynamic SSG page correctly'
,
async
()
=>
{
it
(
'should have correct asPath on dynamic SSG page correctly'
,
async
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment