Skip to content

Commit

Permalink
fix: restore locale handing
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Oct 28, 2022
1 parent 160dba8 commit ff11ef7
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/next/client/link.tsx
Expand Up @@ -108,7 +108,7 @@ type LinkPropsOptional = OptionalKeys<InternalLinkProps>

const prefetched = new Set<string>()

type PrefetchOptions = Omit<RouterPrefetchOptions, 'locale'> & {
type PrefetchOptions = RouterPrefetchOptions & {
/**
* bypassPrefetchedCheck will bypass the check to see if the `href` has
* already been fetched.
Expand All @@ -120,7 +120,7 @@ function prefetch(
router: NextRouter | AppRouterInstance,
href: string,
as: string,
options?: PrefetchOptions
options: PrefetchOptions
): void {
if (typeof window === 'undefined') {
return
Expand All @@ -132,18 +132,25 @@ function prefetch(

// We should only dedupe requests when experimental.optimisticClientCache is
// disabled.
if (!options?.bypassPrefetchedCheck) {
const locale = 'locale' in router ? router.locale : undefined

const key = href + '%' + as + (locale ? '%' + locale : '')
if (!options.bypassPrefetchedCheck) {
const locale =
// Let the link's locale prop override the default router locale.
typeof options.locale === 'string'
? options.locale
: // Otherwise fallback to the router's locale.
'locale' in router
? router.locale
: undefined

const prefetchedKey = href + '%' + as + (locale ? '%' + locale : '')

// If we've already fetched the key, then don't prefetch it again!
if (prefetched.has(key)) {
if (prefetched.has(prefetchedKey)) {
return
}

// Mark this URL as prefetched.
prefetched.add(key)
prefetched.add(prefetchedKey)
}

// Prefetch the JSON page if asked (only in the client)
Expand Down Expand Up @@ -501,7 +508,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
}

// Prefetch the URL.
prefetch(router, href, as)
prefetch(router, href, as, { locale })
}, [
as,
href,
Expand Down Expand Up @@ -584,6 +591,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
}

prefetch(router, href, as, {
locale,
priority: true,
// @see {https://github.com/vercel/next.js/discussions/40268?sort=top#discussioncomment-3572642}
bypassPrefetchedCheck: true,
Expand Down Expand Up @@ -611,6 +619,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
}

prefetch(router, href, as, {
locale,
priority: true,
// @see {https://github.com/vercel/next.js/discussions/40268?sort=top#discussioncomment-3572642}
bypassPrefetchedCheck: true,
Expand Down

0 comments on commit ff11ef7

Please sign in to comment.