Unverified Commit bce06f50 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update has query encoding when used in path (#27762)

This is a follow-up to https://github.com/vercel/next.js/pull/26963 which after discussion changes to interpolate the decoded variant of the value into the path. 

x-ref: https://github.com/vercel/next.js/issues/24775

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent 2061d6c4
...@@ -30,16 +30,6 @@ export function matchHas( ...@@ -30,16 +30,6 @@ export function matchHas(
query: Params query: Params
): false | Params { ): false | Params {
const params: 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) => { const allMatch = has.every((hasItem) => {
let value: undefined | string let value: undefined | string
...@@ -56,12 +46,7 @@ export function matchHas( ...@@ -56,12 +46,7 @@ export function matchHas(
break break
} }
case 'query': { case 'query': {
// preserve initial encoding of query values
value = query[key!] value = query[key!]
if (initialQueryValues.includes(value || '')) {
value = encodeURIComponent(value!)
}
break break
} }
case 'host': { case 'host': {
......
...@@ -198,7 +198,7 @@ module.exports = { ...@@ -198,7 +198,7 @@ module.exports = {
key: 'post', key: 'post',
}, },
], ],
destination: '/blog/:post', destination: '/blog-catchall/:post',
}, },
{ {
source: '/blog/about', source: '/blog/about',
......
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>
}
...@@ -7,6 +7,7 @@ import fs from 'fs-extra' ...@@ -7,6 +7,7 @@ import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp'
import { import {
launchApp, launchApp,
killApp, killApp,
...@@ -43,15 +44,19 @@ const runTests = (isDev = false) => { ...@@ -43,15 +44,19 @@ const runTests = (isDev = false) => {
for (const expected of [ for (const expected of [
{ {
post: 'first', post: 'first',
slug: ['first'],
}, },
{ {
post: 'hello%20world', post: 'hello%20world',
slug: ['hello world'],
}, },
{ {
post: 'hello/world', post: 'hello/world',
slug: ['hello', 'world'],
}, },
{ {
post: 'hello%2fworld', post: 'hello%2fworld',
slug: ['hello', 'world'],
}, },
]) { ]) {
const { status = 200, post } = expected const { status = 200, post } = expected
...@@ -68,8 +73,10 @@ const runTests = (isDev = false) => { ...@@ -68,8 +73,10 @@ const runTests = (isDev = false) => {
if (status === 200) { if (status === 200) {
const $ = cheerio.load(await res.text()) const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({ expect(JSON.parse($('#props').text())).toEqual({
post: decodeURIComponent(post), params: {
slug: expected.slug,
},
}) })
} }
} }
...@@ -1107,12 +1114,30 @@ const runTests = (isDev = false) => { ...@@ -1107,12 +1114,30 @@ const runTests = (isDev = false) => {
]) { ]) {
route.regex = normalizeRegEx(route.regex) route.regex = normalizeRegEx(route.regex)
} }
for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}
expect(manifest).toEqual({ expect(manifest).toEqual({
version: 3, version: 3,
pages404: true, pages404: true,
basePath: '', 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: [ redirects: [
{ {
destination: '/:path+', destination: '/:path+',
...@@ -1817,7 +1842,7 @@ const runTests = (isDev = false) => { ...@@ -1817,7 +1842,7 @@ const runTests = (isDev = false) => {
source: '/has-rewrite-7', source: '/has-rewrite-7',
}, },
{ {
destination: '/blog/:post', destination: '/blog-catchall/:post',
has: [ has: [
{ {
key: 'post', key: 'post',
...@@ -1868,6 +1893,14 @@ const runTests = (isDev = false) => { ...@@ -1868,6 +1893,14 @@ const runTests = (isDev = false) => {
post: 'post', post: 'post',
}, },
}, },
{
namedRegex: '^/blog\\-catchall/(?<slug>.+?)(?:/)?$',
page: '/blog-catchall/[...slug]',
regex: normalizeRegEx('^\\/blog\\-catchall\\/(.+?)(?:\\/)?$'),
routeKeys: {
slug: 'slug',
},
},
], ],
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment