@@ -21,15 +21,32 @@ type Props = NextLinkProps & {
2121 localeCookie : InitializedLocaleCookieConfig ;
2222} ;
2323
24- function BaseLink (
25- { href, locale, localeCookie, onClick, prefetch, ...rest } : Props ,
26- ref : Ref < HTMLAnchorElement >
27- ) {
28- const curLocale = useLocale ( ) ;
29- const isChangingLocale = locale != null && locale !== curLocale ;
24+ type LocaleChangingLinkProps = NextLinkProps & {
25+ curLocale : Locale ;
26+ linkRef : Ref < HTMLAnchorElement > ;
27+ locale : Locale ;
28+ localeCookie : InitializedLocaleCookieConfig ;
29+ } ;
30+
31+ // Somehow the types for `next/link` don't work as expected
32+ // when `moduleResolution: "nodenext"` is used.
33+ const Link = NextLink as unknown as ( props : NextLinkProps ) => ReactNode ;
3034
35+ // Links that change the locale are handled in a separate component,
36+ // since reading the pathname (necessary for syncing the locale cookie)
37+ // requires a Suspense boundary when Cache Components are used. Due to
38+ // this split, regular links are not subject to this requirement.
39+ function LocaleChangingLink ( {
40+ curLocale,
41+ linkRef,
42+ locale,
43+ localeCookie,
44+ onClick,
45+ prefetch,
46+ ...rest
47+ } : LocaleChangingLinkProps ) {
3148 // The types aren't entirely correct here. Outside of Next.js
32- // `useParams ` can be called, but the return type is `null`.
49+ // `usePathname ` can be called, but the return type is `null`.
3350 const pathname = usePathname ( ) as ReturnType < typeof usePathname > | null ;
3451
3552 function onLinkClick ( event : MouseEvent < HTMLAnchorElement > ) {
@@ -40,29 +57,43 @@ function BaseLink(
4057 if ( onClick ) onClick ( event ) ;
4158 }
4259
43- if ( isChangingLocale ) {
44- if ( prefetch && process . env . NODE_ENV !== 'production' ) {
45- console . error (
46- 'The `prefetch` prop is currently not supported when using the `locale` prop on `Link` to switch the locale.`'
47- ) ;
48- }
49- prefetch = false ;
60+ if ( prefetch && process . env . NODE_ENV !== 'production' ) {
61+ console . error (
62+ 'The `prefetch` prop is currently not supported when using the `locale` prop on `Link` to switch the locale.`'
63+ ) ;
5064 }
5165
52- // Somehow the types for `next/link` don't work as expected
53- // when `moduleResolution: "nodenext"` is used.
54- const Link = NextLink as unknown as ( props : NextLinkProps ) => ReactNode ;
55-
5666 return (
5767 < Link
58- ref = { ref }
59- href = { href }
60- hrefLang = { isChangingLocale ? locale : undefined }
68+ ref = { linkRef }
69+ hrefLang = { locale }
6170 onClick = { onLinkClick }
62- prefetch = { prefetch }
71+ prefetch = { false }
6372 { ...rest }
6473 />
6574 ) ;
6675}
6776
77+ function BaseLink (
78+ { locale, localeCookie, ...rest } : Props ,
79+ ref : Ref < HTMLAnchorElement >
80+ ) {
81+ const curLocale = useLocale ( ) ;
82+ const isChangingLocale = locale != null && locale !== curLocale ;
83+
84+ if ( isChangingLocale ) {
85+ return (
86+ < LocaleChangingLink
87+ curLocale = { curLocale }
88+ linkRef = { ref }
89+ locale = { locale }
90+ localeCookie = { localeCookie }
91+ { ...rest }
92+ />
93+ ) ;
94+ }
95+
96+ return < Link ref = { ref } { ...rest } /> ;
97+ }
98+
6899export default forwardRef ( BaseLink ) ;
0 commit comments