Unverified Commit 9bbb968c authored by Ishank Sharma's avatar Ishank Sharma Committed by GitHub

Fix/link router 24075 take asPath instead of pathName in router (#24199)



Fixes: #24075

## Bug
 
- [X] Related issues linked using `fixes #number`
- [X] Integration tests added


## Documentation / Examples

- [x] Make sure the linting passes
parent d5de1c53
......@@ -213,7 +213,7 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
const p = props.prefetch !== false
const router = useRouter()
const pathname = (router && router.pathname) || '/'
const pathname = (router && router.asPath) || '/'
const { href, as } = React.useMemo(() => {
const [resolvedHref, resolvedAs] = resolveHref(pathname, props.href, true)
......
......@@ -271,7 +271,14 @@ export function resolveHref(
resolveAs?: boolean
): string {
// we use a dummy base url for relative urls
const base = new URL(currentPath, 'http://n')
let base: URL
try {
base = new URL(currentPath, 'http://n')
} catch (_) {
// fallback to / for invalid asPath values e.g. //
base = new URL('/', 'http://n')
}
const urlAsString =
typeof href === 'string' ? href : formatWithValidation(href)
// Return because it cannot be routed by the Next.js router
......@@ -328,7 +335,7 @@ function stripOrigin(url: string) {
function prepareUrlAs(router: NextRouter, url: Url, as?: Url) {
// If url and as provided as an object representation,
// we'll format them into the string version here.
let [resolvedHref, resolvedAs] = resolveHref(router.pathname, url, true)
let [resolvedHref, resolvedAs] = resolveHref(router.asPath, url, true)
const origin = getLocationOrigin()
const hrefHadOrigin = resolvedHref.startsWith(origin)
const asHadOrigin = resolvedAs && resolvedAs.startsWith(origin)
......@@ -338,7 +345,7 @@ function prepareUrlAs(router: NextRouter, url: Url, as?: Url) {
const preparedUrl = hrefHadOrigin ? resolvedHref : addBasePath(resolvedHref)
const preparedAs = as
? stripOrigin(resolveHref(router.pathname, as))
? stripOrigin(resolveHref(router.asPath, as))
: resolvedAs || resolvedHref
return {
......
import Link from 'next/link'
import { useRouter } from 'next/router'
const Page = () => {
......@@ -5,6 +6,14 @@ const Page = () => {
const { query } = router
return (
<>
<Link href="#only-hash">
<a id="dynamic-route-only-hash">Dynamic route only hash</a>
</Link>
<br />
<Link href={{ hash: 'only-hash-obj' }}>
<a id="dynamic-route-only-hash-obj">Dynamic route only hash object</a>
</Link>
<br />
<p id="asdf">This is {query.name}</p>
<p id="query">{JSON.stringify(query)}</p>
</>
......
......@@ -30,6 +30,32 @@ const appDir = join(__dirname, '../')
const buildIdPath = join(appDir, '.next/BUILD_ID')
function runTests(dev) {
it('should handle only hash on dynamic route', async () => {
const browser = await webdriver(appPort, '/post-1')
const parsedHref = url.parse(
await browser
.elementByCss('#dynamic-route-only-hash')
.getAttribute('href')
)
expect(parsedHref.pathname).toBe('/post-1')
expect(parsedHref.hash).toBe('#only-hash')
const parsedHref2 = url.parse(
await browser
.elementByCss('#dynamic-route-only-hash-obj')
.getAttribute('href')
)
expect(parsedHref2.pathname).toBe('/post-1')
expect(parsedHref2.hash).toBe('#only-hash-obj')
expect(await browser.eval('window.location.hash')).toBe('')
await browser.elementByCss('#dynamic-route-only-hash').click()
expect(await browser.eval('window.location.hash')).toBe('#only-hash')
await browser.elementByCss('#dynamic-route-only-hash-obj').click()
expect(await browser.eval('window.location.hash')).toBe('#only-hash-obj')
})
it('should navigate with hash to dynamic route with link', async () => {
const browser = await webdriver(appPort, '/')
await browser.eval('window.beforeNav = 1')
......
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