cloudflare-next-intl 0.9.29 → 0.9.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { type LinkProps } from 'next/link.js';
|
|
2
2
|
import { type ComponentProps } from 'react';
|
|
3
|
-
export type PrefetchType = 'custom' | 'default';
|
|
3
|
+
export type PrefetchType = 'custom' | 'eager' | 'default';
|
|
4
4
|
type NextLinkProps = Omit<ComponentProps<'a'>, keyof LinkProps> & Omit<LinkProps, 'locale'> & {
|
|
5
5
|
prefetchType?: PrefetchType;
|
|
6
6
|
};
|
|
7
|
+
export declare const PENDING_NAVIGATION_EVENT = "cloudflare-next-intl:pending-navigation";
|
|
7
8
|
declare const Link: import("react").ForwardRefExoticComponent<Omit<NextLinkProps, "ref"> & import("react").RefAttributes<HTMLAnchorElement>>;
|
|
8
9
|
export default Link;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import LinkComponent from 'next/link.js';
|
|
4
|
-
import { forwardRef, useCallback, useEffect, useState, useTransition, } from 'react';
|
|
4
|
+
import { forwardRef, useCallback, useEffect, useRef, useState, useTransition, } from 'react';
|
|
5
5
|
import config from '../../config/intl_config.js';
|
|
6
6
|
import { getLocaleCache } from '../../general/cache_variables.js';
|
|
7
7
|
import { usePathname, useRouter } from 'next/navigation.js';
|
|
8
8
|
const prefetchedRoutes = new Set();
|
|
9
|
-
|
|
9
|
+
export const PENDING_NAVIGATION_EVENT = 'cloudflare-next-intl:pending-navigation';
|
|
10
|
+
function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick, onMouseEnter, onMouseLeave, onPointerDown, ...rest }, ref) {
|
|
10
11
|
const localeValue = getLocaleCache();
|
|
11
12
|
const router = useRouter();
|
|
13
|
+
prefetch ?? (prefetch = config.link?.defaultPrefetch ?? false);
|
|
12
14
|
const needsLangPath = localeValue !== config.defaultLocale || !localeValue;
|
|
13
15
|
let pathnames;
|
|
14
16
|
let urlString;
|
|
@@ -26,6 +28,7 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
|
|
|
26
28
|
const [isNavigating, setIsNavigating] = useState(false);
|
|
27
29
|
useEffect(() => {
|
|
28
30
|
setIsNavigating(false);
|
|
31
|
+
window.dispatchEvent(new CustomEvent(PENDING_NAVIGATION_EVENT, { detail: null }));
|
|
29
32
|
}, [pathname]);
|
|
30
33
|
useEffect(() => {
|
|
31
34
|
if (!isNavigating)
|
|
@@ -33,9 +36,11 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
|
|
|
33
36
|
const timer = setTimeout(() => setIsNavigating(false), 10000);
|
|
34
37
|
return () => clearTimeout(timer);
|
|
35
38
|
}, [isNavigating]);
|
|
36
|
-
const isCustom = prefetchType === 'custom'
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
+
const isCustom = prefetchType === 'custom' || prefetchType === 'eager';
|
|
40
|
+
const prefetchEnabled = isCustom && prefetch !== false;
|
|
41
|
+
const isEager = prefetchType === 'eager' && prefetchEnabled;
|
|
42
|
+
const doPrefetch = useCallback(() => {
|
|
43
|
+
if (!urlString || urlString.startsWith('#') || prefetchedRoutes.has(urlString)) {
|
|
39
44
|
return;
|
|
40
45
|
}
|
|
41
46
|
prefetchedRoutes.add(urlString);
|
|
@@ -44,18 +49,35 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
|
|
|
44
49
|
}
|
|
45
50
|
catch {
|
|
46
51
|
}
|
|
47
|
-
}, [
|
|
48
|
-
|
|
52
|
+
}, [urlString, pathnames, router]);
|
|
53
|
+
const triggerPointerDownPrefetch = useCallback(() => {
|
|
49
54
|
if (!isCustom)
|
|
50
55
|
return;
|
|
51
|
-
|
|
56
|
+
doPrefetch();
|
|
57
|
+
}, [isCustom, doPrefetch]);
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!isEager)
|
|
60
|
+
return;
|
|
61
|
+
const timer = setTimeout(doPrefetch, 100);
|
|
52
62
|
return () => clearTimeout(timer);
|
|
53
|
-
}, [urlString,
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
}, [urlString, isEager, doPrefetch]);
|
|
64
|
+
const hoverTimerRef = useRef(null);
|
|
65
|
+
const clearHoverTimer = () => {
|
|
66
|
+
if (hoverTimerRef.current) {
|
|
67
|
+
clearTimeout(hoverTimerRef.current);
|
|
68
|
+
hoverTimerRef.current = null;
|
|
57
69
|
}
|
|
58
70
|
};
|
|
71
|
+
const handleHoverStart = () => {
|
|
72
|
+
if (!prefetchEnabled)
|
|
73
|
+
return;
|
|
74
|
+
clearHoverTimer();
|
|
75
|
+
hoverTimerRef.current = setTimeout(doPrefetch, 100);
|
|
76
|
+
};
|
|
77
|
+
const handleHoverEnd = () => {
|
|
78
|
+
clearHoverTimer();
|
|
79
|
+
};
|
|
80
|
+
useEffect(() => clearHoverTimer, []);
|
|
59
81
|
const handleClick = (e) => {
|
|
60
82
|
onClick?.(e);
|
|
61
83
|
if (e.defaultPrevented)
|
|
@@ -68,6 +90,12 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
|
|
|
68
90
|
const targetPath = typeof pathnames === 'string' ? pathnames : urlString;
|
|
69
91
|
if (pathname !== targetPath) {
|
|
70
92
|
setIsNavigating(true);
|
|
93
|
+
try {
|
|
94
|
+
window.history.replaceState(window.history.state, '', targetPath);
|
|
95
|
+
window.dispatchEvent(new CustomEvent(PENDING_NAVIGATION_EVENT, { detail: targetPath }));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
}
|
|
71
99
|
}
|
|
72
100
|
startTransition(() => {
|
|
73
101
|
router.push(targetPath);
|
|
@@ -77,10 +105,13 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
|
|
|
77
105
|
};
|
|
78
106
|
const effectivePrefetch = isCustom ? false : prefetch;
|
|
79
107
|
return _jsx(LinkComponent, { ref: ref, href: pathnames, prefetch: effectivePrefetch, onClick: handleClick, onMouseEnter: (e) => {
|
|
80
|
-
|
|
108
|
+
handleHoverStart();
|
|
81
109
|
onMouseEnter?.(e);
|
|
110
|
+
}, onMouseLeave: (e) => {
|
|
111
|
+
handleHoverEnd();
|
|
112
|
+
onMouseLeave?.(e);
|
|
82
113
|
}, onPointerDown: (e) => {
|
|
83
|
-
|
|
114
|
+
triggerPointerDownPrefetch();
|
|
84
115
|
onPointerDown?.(e);
|
|
85
116
|
}, ...rest });
|
|
86
117
|
}
|
|
@@ -19,6 +19,10 @@ export interface RoutingConfig<AppLocales extends Locales, AppLocalePrefixMode e
|
|
|
19
19
|
cookieConsent?: CookieConsentRoutingConfig;
|
|
20
20
|
generate?: GenerateRoutingConfig;
|
|
21
21
|
errorHandling?: ErrorHandlingRoutingConfig;
|
|
22
|
+
link?: LinkRoutingConfig;
|
|
23
|
+
}
|
|
24
|
+
export interface LinkRoutingConfig {
|
|
25
|
+
defaultPrefetch?: boolean;
|
|
22
26
|
}
|
|
23
27
|
export interface GenerateRoutingConfig {
|
|
24
28
|
env?: object | Record<string, unknown> | (() => object | Record<string, unknown> | Promise<object | Record<string, unknown>>);
|