Skip to content

Commit

Permalink
tweak a little
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Jul 7, 2020
1 parent a3bde0c commit 3fb78c2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { addBasePath, resolveHref } from '../next-server/lib/router/router'
function isLocal(url: string): boolean {
const locationOrigin = getLocationOrigin()
const resolved = new URL(url, locationOrigin)
// TODO: Investigate shouldn't we add: && resolved.pathname.startsWith(router.basePath) ?
return resolved.origin === locationOrigin
}

Expand Down Expand Up @@ -169,12 +168,12 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
const router = useRouter()

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

React.useEffect(() => {
if (p && IntersectionObserver && childElm && childElm.tagName) {
Expand Down
8 changes: 4 additions & 4 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type Url = UrlObject | string
* Resolves a given hyperlink with a certain router state (basePath not included).
* Preserves absolute urls.
*/
export function resolveHref(router: NextRouter, href: Url): string {
export function resolveHref(currentPath: string, href: Url): string {
// we use a dummy base url for relative urls
const base = new URL(router.pathname, 'http://n')
const base = new URL(currentPath, 'http://n')
const urlAsString =
typeof href === 'string' ? href : formatWithValidation(href)
const finalUrl = normalizeTrailingSlash(new URL(urlAsString, base))
Expand All @@ -58,8 +58,8 @@ 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.
return {
url: addBasePath(resolveHref(router, url)),
as: as ? addBasePath(resolveHref(router, as)) : as,
url: addBasePath(resolveHref(router.pathname, url)),
as: as ? addBasePath(resolveHref(router.pathname, as)) : as,
}
}

Expand Down

0 comments on commit 3fb78c2

Please sign in to comment.