clear-react-router 1.9.7 → 1.9.8

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.
package/README.md CHANGED
@@ -773,11 +773,13 @@ function ProductFilter() {
773
773
 
774
774
  ## Lazy Loading
775
775
 
776
- Clear Router supports code-splitting out of the box. Simply pass a function that returns a dynamic import:
776
+ Clear Router supports code-splitting out of the box. Simply wrap dynamic import into a library's `lazy` function:
777
777
  ```tsx
778
+ import { lazy } from 'clear-react-router';
779
+
778
780
  {
779
781
  path: '/heavy-page',
780
- element: () => import('./pages/HeavyComponent'),
782
+ element: lazy(() => import('./pages/HeavyComponent')),
781
783
  fallback: () => <div>Loading...</div>,
782
784
  }
783
785
  ```
package/dist/index.d.ts CHANGED
@@ -11,5 +11,6 @@ export { useAction } from './hooks/useAction';
11
11
  export { useRouterContext } from './hooks/useRouterContext';
12
12
  export { useSearchParams } from './hooks/useSearchParams';
13
13
  export { useFormContext } from './hooks/useFormContext';
14
+ export { lazy } from './utils/lazy';
14
15
  export { createRouter } from './utils/utils';
15
16
  export type { RouteItem, BlockerState, Location, RouterProps, ElementProps } from './types';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Suspense, createContext, lazy, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
1
+ import { Suspense, createContext, lazy as lazy$1, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
2
2
  //#region \0rolldown/runtime.js
3
3
  var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
4
4
  //#endregion
@@ -162,7 +162,7 @@ var import_jsx_runtime = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
162
162
  })))();
163
163
  var createLazyComponent = (importFn, fallback) => {
164
164
  const load = () => importFn().then((module) => ({ default: module.default || module }));
165
- const LazyComp = lazy(load);
165
+ const LazyComp = lazy$1(load);
166
166
  const Component = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Suspense, {
167
167
  fallback: typeof fallback === "function" ? fallback() : fallback || null,
168
168
  children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LazyComp, {})
@@ -173,12 +173,15 @@ var createLazyComponent = (importFn, fallback) => {
173
173
  };
174
174
  };
175
175
  //#endregion
176
+ //#region types.ts
177
+ var LAZY_MARKER = Symbol("clear-router-lazy");
178
+ //#endregion
176
179
  //#region utils/utils.ts
177
- var isLazy = (el) => typeof el.element === "function" && el.element.toString().includes("import(");
180
+ var isLazy = (el) => typeof el.element === "object" && el.element !== null && LAZY_MARKER in el.element;
178
181
  var parseClientRouteItem = (el, parentPattern = "") => {
179
182
  const pattern = `${parentPattern}/${el.path}`.replace(/\/+/g, "/");
180
- const preloadElement = isLazy(el) ? createLazyComponent(el.element, el.fallback).preloadElement : void 0;
181
- const resolvedElement = isLazy(el) ? createLazyComponent(el.element, el.fallback).Component : el.element;
183
+ const preloadElement = isLazy(el) ? createLazyComponent(el.element.importFn, el.fallback).preloadElement : void 0;
184
+ const resolvedElement = isLazy(el) ? createLazyComponent(el.element.importFn, el.fallback).Component : el.element;
182
185
  return [{
183
186
  ...el,
184
187
  pattern,
@@ -288,7 +291,7 @@ var createNavigate = (routerState, revalidateCache) => {
288
291
  } : void 0);
289
292
  }
290
293
  };
291
- const afterEachLoad = (routeItem) => {
294
+ const afterEachLoad = (routeItem, location) => {
292
295
  if (!routeItem?.pollingInterval) return;
293
296
  interval = window.setInterval(() => revalidateCache({
294
297
  routeItem,
@@ -303,7 +306,7 @@ var createNavigate = (routerState, revalidateCache) => {
303
306
  pathname: location.pathname,
304
307
  search: location.search
305
308
  });
306
- afterEachLoad(routeItem);
309
+ afterEachLoad(routeItem, location);
307
310
  };
308
311
  const afterLoad = async (routeItem, params) => {
309
312
  const { defaultAfterLoad } = routerConfig;
@@ -465,7 +468,8 @@ var createRevalidateCache = (routerState) => {
465
468
  if (retry.delay) await sleep(retry.delay);
466
469
  await revalidateCache({
467
470
  routeItem,
468
- pathname
471
+ pathname,
472
+ search
469
473
  }, retried + 1);
470
474
  return {
471
475
  data: null,
@@ -709,7 +713,8 @@ var renderElement = (Component) => {
709
713
  //#endregion
710
714
  //#region components/Router.tsx
711
715
  var EmptyBoundary = ({ children }) => children;
712
- var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, spinner = true, defaultPreserveScroll = true, showFallbackOnAnimation = false, defaultPrefetch = isMobile() ? "viewport" : "hover", defaultHoverPrefetchDelay = 150, errorBoundary: ErrorBoundary = EmptyBoundary }) => {
716
+ var IS_MOBILE = isMobile();
717
+ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, spinner = true, defaultPreserveScroll = true, showFallbackOnAnimation = false, defaultPrefetch = IS_MOBILE ? "viewport" : "hover", defaultHoverPrefetchDelay = 150, errorBoundary: ErrorBoundary = EmptyBoundary }) => {
713
718
  const { useRouteItemData, usePendingState } = router.hooks;
714
719
  const [routeItemData] = useRouteItemData();
715
720
  const [pendingState] = usePendingState();
@@ -1006,4 +1011,10 @@ var useSearchParams = () => {
1006
1011
  //#region hooks/useFormContext.ts
1007
1012
  var useFormContext = () => useContext(FormContext);
1008
1013
  //#endregion
1009
- export { Form, Link, Router, createRouter, useAction, useBlocker, useFormContext, useInvalidate, useLoaderState, useLocation, useNavigate, useParams, useRouterContext, useSearchParams };
1014
+ //#region utils/lazy.ts
1015
+ var lazy = (importFn) => ({
1016
+ [LAZY_MARKER]: true,
1017
+ importFn
1018
+ });
1019
+ //#endregion
1020
+ export { Form, Link, Router, createRouter, lazy, useAction, useBlocker, useFormContext, useInvalidate, useLoaderState, useLocation, useNavigate, useParams, useRouterContext, useSearchParams };
package/dist/types.d.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  import { ComponentType, type CSSProperties, Dispatch, MouseEvent, ReactElement, ReactNode, Ref, SetStateAction } from 'react';
2
2
  import { Store, useGlobalState } from './create';
3
3
  import { Cell } from './cell';
4
- export type LazyComponent = () => Promise<{
5
- default: ComponentType<unknown>;
6
- }>;
4
+ export declare const LAZY_MARKER: unique symbol;
5
+ export type LazyComponent = {
6
+ readonly [LAZY_MARKER]: true;
7
+ importFn: () => Promise<{
8
+ default: ComponentType<unknown>;
9
+ }>;
10
+ };
7
11
  export type RenderElement = (() => ReactElement) | ReactElement;
8
12
  export type BeforeLoad = (arg: {
9
13
  context: Record<string, unknown>;
@@ -0,0 +1,5 @@
1
+ import { ComponentType } from 'react';
2
+ import { LazyComponent } from '../types';
3
+ export declare const lazy: (importFn: () => Promise<{
4
+ default: ComponentType<unknown>;
5
+ }>) => LazyComponent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.9.7",
3
+ "version": "1.9.8",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {