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 mixed query/hash values are handled correctly #38852

Merged
merged 4 commits into from Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/next/shared/lib/router/utils/parse-path.ts
Expand Up @@ -6,14 +6,14 @@
export function parsePath(path: string) {
const hashIndex = path.indexOf('#')
const queryIndex = path.indexOf('?')
const hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex)

if (queryIndex > -1 || hashIndex > -1) {
if (hasQuery || hashIndex > -1) {
return {
pathname: path.substring(0, queryIndex > -1 ? queryIndex : hashIndex),
query:
queryIndex > -1
? path.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined)
: '',
pathname: path.substring(0, hasQuery ? queryIndex : hashIndex),
query: hasQuery
? path.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined)
: '',
hash: hashIndex > -1 ? path.slice(hashIndex) : '',
}
}
Expand Down
34 changes: 34 additions & 0 deletions test/e2e/basepath.test.ts
Expand Up @@ -120,6 +120,40 @@ describe('basePath', () => {
return
}

it.each([
{ hash: '#hello?' },
{ hash: '#?' },
{ hash: '##' },
{ hash: '##?' },
{ hash: '##hello?' },
{ hash: '##hello' },
{ hash: '#hello?world' },
{ search: '?hello=world', hash: '#a', query: { hello: 'world' } },
{ search: '?hello', hash: '#a', query: { hello: '' } },
{ search: '?hello=', hash: '#a', query: { hello: '' } },
])(
'should handle query/hash correctly during query updating $hash $search',
async ({ hash, search, query }) => {
const browser = await webdriver(
next.url,
`${basePath}${search || ''}${hash || ''}`
)

await check(
() =>
browser.eval('window.next.router.isReady ? "ready" : "not ready"'),
'ready'
)
expect(await browser.eval('window.location.pathname')).toBe(basePath)
expect(await browser.eval('window.location.search')).toBe(search || '')
expect(await browser.eval('window.location.hash')).toBe(hash || '')
expect(await browser.eval('next.router.pathname')).toBe('/')
expect(
JSON.parse(await browser.eval('JSON.stringify(next.router.query)'))
).toEqual(query || {})
}
)

it('should navigate back correctly to a dynamic route', async () => {
const browser = await webdriver(next.url, `${basePath}`)

Expand Down
31 changes: 31 additions & 0 deletions test/integration/production/test/index.test.js
Expand Up @@ -88,6 +88,37 @@ describe('Production Usage', () => {
await browser.waitForElementByCss('.about-page')
})

it.each([
{ hash: '#hello?' },
{ hash: '#?' },
{ hash: '##' },
{ hash: '##?' },
{ hash: '##hello?' },
{ hash: '##hello' },
{ hash: '#hello?world' },
{ search: '?hello=world', hash: '#a', query: { hello: 'world' } },
{ search: '?hello', hash: '#a', query: { hello: '' } },
{ search: '?hello=', hash: '#a', query: { hello: '' } },
])(
'should handle query/hash correctly during query updating $hash $search',
async ({ hash, search, query }) => {
const browser = await webdriver(appPort, `/${search || ''}${hash || ''}`)

await check(
() =>
browser.eval('window.next.router.isReady ? "ready" : "not ready"'),
'ready'
)
expect(await browser.eval('window.location.pathname')).toBe('/')
expect(await browser.eval('window.location.hash')).toBe(hash || '')
expect(await browser.eval('window.location.search')).toBe(search || '')
expect(await browser.eval('next.router.pathname')).toBe('/')
expect(
JSON.parse(await browser.eval('JSON.stringify(next.router.query)'))
).toEqual(query || {})
}
)

it('should not show target deprecation warning', () => {
expect(output).not.toContain(
'The `target` config is deprecated and will be removed in a future version'
Expand Down
1 change: 1 addition & 0 deletions test/lib/next-test-utils.js
Expand Up @@ -93,6 +93,7 @@ export function getFullUrl(appPortOrUrl, url, hostname) {
const parsedUrl = new URL(fullUrl)
const parsedPathQuery = new URL(url, fullUrl)

parsedUrl.hash = parsedPathQuery.hash
parsedUrl.search = parsedPathQuery.search
parsedUrl.pathname = parsedPathQuery.pathname

Expand Down