cloudflare-next-intl 0.9.34 → 0.9.35
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,5 +1,6 @@
|
|
|
1
1
|
import { type LinkProps } from 'next/link.js';
|
|
2
2
|
import { type ComponentProps } from 'react';
|
|
3
|
+
export declare const PENDING_NAVIGATION_EVENT = "cloudflare-next-intl:pending-navigation";
|
|
3
4
|
export type PrefetchType = 'custom' | 'eager' | 'default';
|
|
4
5
|
type NextLinkProps = Omit<ComponentProps<'a'>, keyof LinkProps> & Omit<LinkProps, 'locale'> & {
|
|
5
6
|
prefetchType?: PrefetchType;
|
|
@@ -5,6 +5,7 @@ import { forwardRef, useCallback, useEffect, useRef, useState, useTransition, }
|
|
|
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
|
+
export const PENDING_NAVIGATION_EVENT = 'cloudflare-next-intl:pending-navigation';
|
|
8
9
|
const prefetchedRoutes = new Set();
|
|
9
10
|
function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', hoverPrefetchDelayMs = 100, onClick, onMouseEnter, onMouseLeave, onPointerDown, ...rest }, ref) {
|
|
10
11
|
const localeValue = getLocaleCache();
|
|
@@ -25,8 +26,14 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', hoverPref
|
|
|
25
26
|
const pathname = usePathname();
|
|
26
27
|
const [isPending, startTransition] = useTransition();
|
|
27
28
|
const [isNavigating, setIsNavigating] = useState(false);
|
|
29
|
+
const isFirstPathnameEffect = useRef(true);
|
|
28
30
|
useEffect(() => {
|
|
29
31
|
setIsNavigating(false);
|
|
32
|
+
if (isFirstPathnameEffect.current) {
|
|
33
|
+
isFirstPathnameEffect.current = false;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
window.dispatchEvent(new CustomEvent(PENDING_NAVIGATION_EVENT, { detail: null }));
|
|
30
37
|
}, [pathname]);
|
|
31
38
|
useEffect(() => {
|
|
32
39
|
if (!isNavigating)
|
|
@@ -84,6 +91,8 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', hoverPref
|
|
|
84
91
|
onClick?.(e);
|
|
85
92
|
if (e.defaultPrevented)
|
|
86
93
|
return;
|
|
94
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0)
|
|
95
|
+
return;
|
|
87
96
|
if (isCustom && (isNavigating || isPending)) {
|
|
88
97
|
e.preventDefault();
|
|
89
98
|
return;
|
|
@@ -92,6 +101,7 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', hoverPref
|
|
|
92
101
|
const targetPath = typeof pathnames === 'string' ? pathnames : urlString;
|
|
93
102
|
if (pathname !== targetPath) {
|
|
94
103
|
setIsNavigating(true);
|
|
104
|
+
window.dispatchEvent(new CustomEvent(PENDING_NAVIGATION_EVENT, { detail: targetPath }));
|
|
95
105
|
}
|
|
96
106
|
startTransition(() => {
|
|
97
107
|
router.push(targetPath);
|