Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure default params are detected after rewrite #25205

Merged
merged 4 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,23 @@ export function getUtils({

function normalizeDynamicRouteParams(params: ParsedUrlQuery) {
let hasValidParams = true
if (!defaultRouteRegex) return { params, hasValidParams }
if (!defaultRouteRegex) return { params, hasValidParams: false }

params = Object.keys(defaultRouteRegex.groups).reduce((prev, key) => {
let value: string | string[] | undefined = params[key]

// if the value matches the default value we can't rely
// on the parsed params, this is used to signal if we need
// to parse x-now-route-matches or not
const isDefaultValue = Array.isArray(value)
? value.some((val) => {
const defaultValue = defaultRouteMatches![key]
const defaultValue = defaultRouteMatches![key]

return Array.isArray(defaultValue)
? defaultValue.includes(val)
: defaultValue === val
const isDefaultValue = Array.isArray(defaultValue)
? defaultValue.some((defaultVal) => {
return Array.isArray(value)
? value.some((val) => val.includes(defaultVal))
: value?.includes(defaultVal)
})
: value === defaultRouteMatches![key]
: value?.includes(defaultValue as string)

if (isDefaultValue || typeof value === 'undefined') {
hasValidParams = false
Expand Down
14 changes: 14 additions & 0 deletions test/integration/required-server-files/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ describe('Required Server Files', () => {
expect($2('#slug').text()).toBe('second')
expect(isNaN(data2.random)).toBe(false)
expect(data2.random).not.toBe(data.random)

const html3 = await renderViaHTTP(appPort, '/some-other-path', undefined, {
headers: {
'x-matched-path': '/dynamic/[slug]?slug=%5Bslug%5D.json',
'x-now-route-matches': '1=second&slug=second',
},
})
const $3 = cheerio.load(html3)
const data3 = JSON.parse($3('#props').text())

expect($3('#dynamic').text()).toBe('dynamic page')
expect($3('#slug').text()).toBe('second')
expect(isNaN(data3.random)).toBe(false)
expect(data3.random).not.toBe(data.random)
})

it('should render fallback page correctly with x-matched-path and routes-matches', async () => {
Expand Down