Unverified Commit 366a04b7 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure mixed query/hash values are handled correctly (#38852)

This ensures we correctly detect query and hash values when there are `?` characters in the hash. A regression test to ensure this working properly has been added. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: https://github.com/vercel/next.js/issues/38783
parent 14463ddd
...@@ -6,14 +6,14 @@ ...@@ -6,14 +6,14 @@
export function parsePath(path: string) { export function parsePath(path: string) {
const hashIndex = path.indexOf('#') const hashIndex = path.indexOf('#')
const queryIndex = path.indexOf('?') const queryIndex = path.indexOf('?')
const hasQuery = queryIndex > -1 && (hashIndex < 0 || queryIndex < hashIndex)
if (queryIndex > -1 || hashIndex > -1) { if (hasQuery || hashIndex > -1) {
return { return {
pathname: path.substring(0, queryIndex > -1 ? queryIndex : hashIndex), pathname: path.substring(0, hasQuery ? queryIndex : hashIndex),
query: query: hasQuery
queryIndex > -1 ? path.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined)
? path.substring(queryIndex, hashIndex > -1 ? hashIndex : undefined) : '',
: '',
hash: hashIndex > -1 ? path.slice(hashIndex) : '', hash: hashIndex > -1 ? path.slice(hashIndex) : '',
} }
} }
......
...@@ -120,6 +120,40 @@ describe('basePath', () => { ...@@ -120,6 +120,40 @@ describe('basePath', () => {
return 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 () => { it('should navigate back correctly to a dynamic route', async () => {
const browser = await webdriver(next.url, `${basePath}`) const browser = await webdriver(next.url, `${basePath}`)
......
...@@ -88,6 +88,37 @@ describe('Production Usage', () => { ...@@ -88,6 +88,37 @@ describe('Production Usage', () => {
await browser.waitForElementByCss('.about-page') 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', () => { it('should not show target deprecation warning', () => {
expect(output).not.toContain( expect(output).not.toContain(
'The `target` config is deprecated and will be removed in a future version' 'The `target` config is deprecated and will be removed in a future version'
......
...@@ -93,6 +93,7 @@ export function getFullUrl(appPortOrUrl, url, hostname) { ...@@ -93,6 +93,7 @@ export function getFullUrl(appPortOrUrl, url, hostname) {
const parsedUrl = new URL(fullUrl) const parsedUrl = new URL(fullUrl)
const parsedPathQuery = new URL(url, fullUrl) const parsedPathQuery = new URL(url, fullUrl)
parsedUrl.hash = parsedPathQuery.hash
parsedUrl.search = parsedPathQuery.search parsedUrl.search = parsedPathQuery.search
parsedUrl.pathname = parsedPathQuery.pathname parsedUrl.pathname = parsedPathQuery.pathname
......
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