Skip to content

Commit

Permalink
Update has query encoding when used in path (#27762)
Browse files Browse the repository at this point in the history
This is a follow-up to #26963 which after discussion changes to interpolate the decoded variant of the value into the path. 

x-ref: #24775

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
ijjk authored Aug 4, 2021
1 parent 2061d6c commit bce06f5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
15 changes: 0 additions & 15 deletions packages/next/shared/lib/router/utils/prepare-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ export function matchHas(
query: Params
): false | Params {
const params: Params = {}
let initialQueryValues: string[] = []

if (typeof window === 'undefined') {
initialQueryValues = Object.values((req as any).__NEXT_INIT_QUERY)
}
if (typeof window !== 'undefined') {
initialQueryValues = Array.from(
new URLSearchParams(location.search).values()
)
}

const allMatch = has.every((hasItem) => {
let value: undefined | string
Expand All @@ -56,12 +46,7 @@ export function matchHas(
break
}
case 'query': {
// preserve initial encoding of query values
value = query[key!]

if (initialQueryValues.includes(value || '')) {
value = encodeURIComponent(value!)
}
break
}
case 'host': {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/custom-routes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module.exports = {
key: 'post',
},
],
destination: '/blog/:post',
destination: '/blog-catchall/:post',
},
{
source: '/blog/about',
Expand Down
18 changes: 18 additions & 0 deletions test/integration/custom-routes/pages/blog-catchall/[...slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getStaticProps = ({ params }) => {
return {
props: {
params,
},
}
}

export const getStaticPaths = () => {
return {
paths: [],
fallback: 'blocking',
}
}

export default function Page(props) {
return <p id="props">{JSON.stringify(props)}</p>
}
41 changes: 37 additions & 4 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp'
import {
launchApp,
killApp,
Expand Down Expand Up @@ -43,15 +44,19 @@ const runTests = (isDev = false) => {
for (const expected of [
{
post: 'first',
slug: ['first'],
},
{
post: 'hello%20world',
slug: ['hello world'],
},
{
post: 'hello/world',
slug: ['hello', 'world'],
},
{
post: 'hello%2fworld',
slug: ['hello', 'world'],
},
]) {
const { status = 200, post } = expected
Expand All @@ -68,8 +73,10 @@ const runTests = (isDev = false) => {

if (status === 200) {
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
post: decodeURIComponent(post),
expect(JSON.parse($('#props').text())).toEqual({
params: {
slug: expected.slug,
},
})
}
}
Expand Down Expand Up @@ -1107,12 +1114,30 @@ const runTests = (isDev = false) => {
]) {
route.regex = normalizeRegEx(route.regex)
}
for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}

expect(manifest).toEqual({
version: 3,
pages404: true,
basePath: '',
dataRoutes: [],
dataRoutes: [
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeRegex(
buildId
)}/blog\\-catchall/(.+?)\\.json$`
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/blog\\-catchall/(?<slug>.+?)\\.json$`,
page: '/blog-catchall/[...slug]',
routeKeys: {
slug: 'slug',
},
},
],
redirects: [
{
destination: '/:path+',
Expand Down Expand Up @@ -1817,7 +1842,7 @@ const runTests = (isDev = false) => {
source: '/has-rewrite-7',
},
{
destination: '/blog/:post',
destination: '/blog-catchall/:post',
has: [
{
key: 'post',
Expand Down Expand Up @@ -1868,6 +1893,14 @@ const runTests = (isDev = false) => {
post: 'post',
},
},
{
namedRegex: '^/blog\\-catchall/(?<slug>.+?)(?:/)?$',
page: '/blog-catchall/[...slug]',
regex: normalizeRegEx('^\\/blog\\-catchall\\/(.+?)(?:\\/)?$'),
routeKeys: {
slug: 'slug',
},
},
],
})
})
Expand Down

0 comments on commit bce06f5

Please sign in to comment.