cloudflare-next-intl 0.9.29 → 0.9.33

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
+ hoverPrefetchDelayMs?: number;
6
7
  };
7
8
  declare const Link: import("react").ForwardRefExoticComponent<Omit<NextLinkProps, "ref"> & import("react").RefAttributes<HTMLAnchorElement>>;
8
9
  export default Link;
@@ -1,14 +1,15 @@
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
- function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick, onMouseEnter, onPointerDown, ...rest }, ref) {
9
+ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', hoverPrefetchDelayMs = 100, onClick, onMouseEnter, onMouseLeave, onPointerDown, ...rest }, ref) {
10
10
  const localeValue = getLocaleCache();
11
11
  const router = useRouter();
12
+ prefetch ?? (prefetch = config.link?.defaultPrefetch ?? true);
12
13
  const needsLangPath = localeValue !== config.defaultLocale || !localeValue;
13
14
  let pathnames;
14
15
  let urlString;
@@ -33,9 +34,11 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
33
34
  const timer = setTimeout(() => setIsNavigating(false), 10000);
34
35
  return () => clearTimeout(timer);
35
36
  }, [isNavigating]);
36
- const isCustom = prefetchType === 'custom' && prefetch !== false;
37
- const triggerPrefetch = useCallback(() => {
38
- if (!isCustom || !urlString || urlString.startsWith('#') || prefetchedRoutes.has(urlString)) {
37
+ const isCustom = prefetchType === 'custom' || prefetchType === 'eager';
38
+ const prefetchEnabled = isCustom && prefetch !== false;
39
+ const isEager = prefetchType === 'eager' && prefetchEnabled;
40
+ const doPrefetch = useCallback(() => {
41
+ if (!urlString || urlString.startsWith('#') || prefetchedRoutes.has(urlString)) {
39
42
  return;
40
43
  }
41
44
  prefetchedRoutes.add(urlString);
@@ -44,18 +47,39 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
44
47
  }
45
48
  catch {
46
49
  }
47
- }, [isCustom, urlString, pathnames, router]);
48
- useEffect(() => {
50
+ }, [urlString, pathnames, router]);
51
+ const triggerPointerDownPrefetch = useCallback(() => {
49
52
  if (!isCustom)
50
53
  return;
51
- const timer = setTimeout(triggerPrefetch, 100);
54
+ doPrefetch();
55
+ }, [isCustom, doPrefetch]);
56
+ useEffect(() => {
57
+ if (!isEager)
58
+ return;
59
+ const timer = setTimeout(doPrefetch, 100);
52
60
  return () => clearTimeout(timer);
53
- }, [urlString, isCustom, triggerPrefetch]);
54
- const handleHoverPrefetch = () => {
55
- if (isCustom) {
56
- triggerPrefetch();
61
+ }, [urlString, isEager, doPrefetch]);
62
+ const hoverTimerRef = useRef(null);
63
+ const clearHoverTimer = () => {
64
+ if (hoverTimerRef.current) {
65
+ clearTimeout(hoverTimerRef.current);
66
+ hoverTimerRef.current = null;
57
67
  }
58
68
  };
69
+ const handleHoverStart = () => {
70
+ if (!prefetchEnabled)
71
+ return;
72
+ clearHoverTimer();
73
+ if (hoverPrefetchDelayMs <= 0) {
74
+ doPrefetch();
75
+ return;
76
+ }
77
+ hoverTimerRef.current = setTimeout(doPrefetch, hoverPrefetchDelayMs);
78
+ };
79
+ const handleHoverEnd = () => {
80
+ clearHoverTimer();
81
+ };
82
+ useEffect(() => clearHoverTimer, []);
59
83
  const handleClick = (e) => {
60
84
  onClick?.(e);
61
85
  if (e.defaultPrevented)
@@ -77,10 +101,13 @@ function CustomLinkFunction({ href, prefetch, prefetchType = 'custom', onClick,
77
101
  };
78
102
  const effectivePrefetch = isCustom ? false : prefetch;
79
103
  return _jsx(LinkComponent, { ref: ref, href: pathnames, prefetch: effectivePrefetch, onClick: handleClick, onMouseEnter: (e) => {
80
- handleHoverPrefetch();
104
+ handleHoverStart();
81
105
  onMouseEnter?.(e);
106
+ }, onMouseLeave: (e) => {
107
+ handleHoverEnd();
108
+ onMouseLeave?.(e);
82
109
  }, onPointerDown: (e) => {
83
- handleHoverPrefetch();
110
+ triggerPointerDownPrefetch();
84
111
  onPointerDown?.(e);
85
112
  }, ...rest });
86
113
  }
@@ -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>>);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudflare-next-intl",
3
- "version": "0.9.29",
3
+ "version": "0.9.33",
4
4
  "description": "Optimized Next Intl Package Special for App Router and Cloudflare",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",