Skip to content

Commit

Permalink
Resolve against router pathname and not window.location
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Jul 2, 2020
1 parent a0c6832 commit da6ee53
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
formatWithValidation,
getLocationOrigin,
} from '../next-server/lib/utils'
import Router from './router'
import Router, { useRouter } from './router'
import { addBasePath } from '../next-server/lib/router/router'
import { normalizeTrailingSlash } from './normalize-trailing-slash'

Expand Down Expand Up @@ -96,33 +96,26 @@ const listenToIntersections = (el: Element, cb: () => void) => {
}
}

function getPaths(parsedHref: string, parsedAs?: string): string[] {
const { pathname } = window.location
const resolvedHref = resolve(pathname, parsedHref)
return [resolvedHref, parsedAs ? resolve(pathname, parsedAs) : resolvedHref]
}

function prefetch(href: string, as?: string, options?: PrefetchOptions): void {
if (typeof window === 'undefined') return
// Prefetch the JSON page if asked (only in the client)
const [resolvedHref, resolvedAs] = getPaths(href, as)
// We need to handle a prefetch error here since we may be
// loading with priority which can reject but we don't
// want to force navigation since this is only a prefetch
Router.prefetch(resolvedHref, resolvedAs, options).catch((err) => {
Router.prefetch(href, as, options).catch((err) => {
if (process.env.NODE_ENV !== 'production') {
// rethrow to show invalid URL errors
throw err
}
})
// Join on an invalid URI character
prefetched[resolvedHref + '%' + resolvedAs] = true
prefetched[href + '%' + as] = true
}

function linkClicked(
e: React.MouseEvent,
href: string,
as?: string,
as: string = href,
replace?: boolean,
shallow?: boolean,
scroll?: boolean
Expand All @@ -145,10 +138,6 @@ function linkClicked(
return
}

const { pathname } = window.location
href = resolve(pathname, href)
as = as ? resolve(pathname, as) : href

e.preventDefault()

// avoid scroll for urls with anchor refs
Expand Down Expand Up @@ -184,21 +173,20 @@ function Link(props: React.PropsWithChildren<LinkProps>) {

const [childElm, setChildElm] = React.useState<Element>()

const router = useRouter()

const { href, as } = React.useMemo(
() => ({
href: formatUrl(props.href),
as: props.as ? formatUrl(props.as) : props.as,
href: resolve(router.pathname, formatUrl(props.href)),
as: props.as ? resolve(router.pathname, formatUrl(props.as)) : props.as,
}),
[props.href, props.as]
[router.pathname, props.href, props.as]
)

React.useEffect(() => {
if (p && IntersectionObserver && childElm && childElm.tagName) {
const isPrefetched =
prefetched[
// Join on an invalid URI character
getPaths(href, as).join('%')
]
// Join on an invalid URI character
const isPrefetched = prefetched[href + '%' + as]
if (!isPrefetched) {
return listenToIntersections(childElm, () => {
prefetch(href, as)
Expand Down

0 comments on commit da6ee53

Please sign in to comment.