clear-react-router 2.5.1 → 2.5.2

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
@@ -947,15 +947,21 @@ await invalidate(['/posts', '/profile', '/settings']);
947
947
 
948
948
  #### Dynamic routes
949
949
 
950
- When a dynamic route pattern is provided, every cached route that matches the pattern will be revalidated.
950
+ Pass a concrete dynamic pathname to revalidate only that cached route:
951
951
 
952
- For example:
952
+ ```tsx
953
+ await invalidate('/post/17');
954
+ ```
955
+
956
+ Only `/post/17` is refetched; cached `/post/1` and `/post/42` are left untouched.
957
+
958
+ Pass a dynamic route pattern to revalidate every cached route that matches it:
953
959
 
954
960
  ```tsx
955
- await invalidate('/post/[id]');
961
+ await invalidate('/post/[id]'); // or await invalidate('/post/:id')
956
962
  ```
957
963
 
958
- will revalidate all cached routes such as:
964
+ This revalidates all cached routes such as:
959
965
 
960
966
  ```text
961
967
  /post/1
@@ -963,14 +969,17 @@ will revalidate all cached routes such as:
963
969
  /post/42
964
970
  ```
965
971
 
972
+ Pattern-based invalidation is cache-only: it never sends `:param`/`[param]` to a loader.
973
+ If no cached route matches, nothing is fetched.
974
+
966
975
  This also works for nested dynamic routes:
967
976
 
968
977
  ```tsx
969
- await invalidate('/post/[id]/comment/[id]');
978
+ await invalidate('/post/[id]/comment/:commentId');
970
979
  ```
971
980
 
972
981
  #### Force revalidation
973
- By default the exact path(s) you pass are always revalidated, even if they were never cached
982
+ By default concrete pathnames are always revalidated, even if they were never cached
974
983
  — this also makes `invalidate()` work on an error page with an empty cache.
975
984
  Pass `{ force: false }` to go back to cache-only revalidation:
976
985
 
@@ -978,8 +987,8 @@ Pass `{ force: false }` to go back to cache-only revalidation:
978
987
  await invalidate('/about', { force: false });
979
988
  ```
980
989
 
981
- Explicit `{ force: true }` behaves the same as the default; it exists for readability
982
- and for combining with other options (see below).
990
+ Explicit `{ force: true }` behaves the same as the default for concrete paths. Pattern
991
+ targets stay cache-only regardless of `force`.
983
992
 
984
993
  #### Stale-only revalidation
985
994
  By default, `invalidate` deletes matching cache entries and refetches them.
@@ -1047,7 +1056,7 @@ Each object represents a revalidated route, where `path` is the route pathname,
1047
1056
 
1048
1057
  #### Notes
1049
1058
 
1050
- * Without `force: false`, the exact pathnames you pass are always revalidated (and stored in the cache), even if they were never cached. Pass `{ force: false }` to revalidate **only routes that already have cached data**.
1059
+ * Without `force: false`, concrete pathnames you pass are always revalidated (and stored in the cache), even if they were never cached. Dynamic patterns only revalidate cached matches and never fetch the pattern itself. Pass `{ force: false }` to revalidate **only routes that already have cached data**.
1051
1060
  * With `{ staleOnly: true }`, uncached paths are never fetched — the default force does not apply.
1052
1061
  * Cached data is cleared before the new loader starts.
1053
1062
  * When used as an event handler, wrap the call in an arrow function:
package/dist/index.js CHANGED
@@ -143,6 +143,12 @@ var parseWindowLocation = (location) => ({
143
143
  search: location.search,
144
144
  state: history.state ?? void 0
145
145
  });
146
+ var isParamSegment = (segment) => segment.startsWith(":") || /^\[[^\]]+]$/.test(segment);
147
+ var matchPathPattern = (pattern, pathname) => {
148
+ const patternSegments = pattern.split("/").filter(Boolean);
149
+ const pathSegments = pathname.split("/").filter(Boolean);
150
+ return patternSegments.length === pathSegments.length ? patternSegments.every((segment, index) => isParamSegment(segment) || segment === pathSegments[index]) : false;
151
+ };
146
152
  var comparePaths = (route, pathname) => {
147
153
  const pattern = route.pattern.split("/").filter(Boolean);
148
154
  const current = pathname.split("/").filter(Boolean);
@@ -427,7 +433,7 @@ var createNavigate = (routerState, revalidateCache, syncCacheSnapshot) => {
427
433
  };
428
434
  //#endregion
429
435
  //#region runtime/invalidate.ts
430
- var separatePathname = (text) => text.split("?")[0];
436
+ var separatePathname = (text) => text.split("?")[0].replace(/\/+$/, "") || "/";
431
437
  var createInvalidate = ({ routeDataState, loaderMap, loadingPromises }, revalidateCache, syncCacheSnapshot) => {
432
438
  const invalidatePath = async (routeItem, pathname, options) => {
433
439
  const routePathname = routeDataState.getState().location.pathname;
@@ -463,11 +469,15 @@ var createInvalidate = ({ routeDataState, loaderMap, loadingPromises }, revalida
463
469
  const invalidateItem = async (pathname, options) => {
464
470
  const routeItem = findRoute(separatePathname(pathname));
465
471
  if (!routeItem) return [];
472
+ const targetPathname = separatePathname(pathname);
473
+ const isPattern = targetPathname.split("/").filter(Boolean).some(isParamSegment);
466
474
  const pathnameSet = /* @__PURE__ */ new Set();
467
- for (const [key] of loaderMap) if (comparePaths(routeItem, separatePathname(key))) pathnameSet.add(key);
468
- const hasUnresolvedParams = separatePathname(pathname).split("/").some((segment) => segment.startsWith(":"));
475
+ for (const [key] of loaderMap) {
476
+ const keyPathname = separatePathname(key);
477
+ if (isPattern ? matchPathPattern(targetPathname, keyPathname) : keyPathname === targetPathname) pathnameSet.add(key);
478
+ }
469
479
  const implicitForce = options?.force === void 0 && !options?.staleOnly;
470
- if (options?.force === true || implicitForce && !hasUnresolvedParams) pathnameSet.add(pathname);
480
+ if (!isPattern && (options?.force === true || implicitForce)) pathnameSet.add(pathname);
471
481
  const currentResults = await Promise.all([...pathnameSet].map((pathname) => invalidatePath(routeItem, pathname, options)));
472
482
  if (!options?.withChildren || !routeItem.children?.length) return currentResults;
473
483
  const [parentPath] = pathname.split("?");
@@ -2,6 +2,8 @@ import { Store } from '../create';
2
2
  import { Location, RouteItem, RouteData, RouterState, ScrollMap, SearchObject } from '../types';
3
3
  export declare const getParams: (location: Location, routeItem?: RouteItem) => Record<string, string>;
4
4
  export declare const parseWindowLocation: (location: typeof window.location) => Location;
5
+ export declare const isParamSegment: (segment: string) => boolean;
6
+ export declare const matchPathPattern: (pattern: string, pathname: string) => boolean;
5
7
  export declare const comparePaths: (route: RouteItem, pathname: string) => boolean;
6
8
  export declare const isMobile: () => boolean;
7
9
  export declare const sleep: (ms: number) => Promise<unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.5.1",
3
+ "version": "2.5.2",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {