@remix-run/router 1.8.0 → 1.9.0-pre.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # `@remix-run/router`
2
2
 
3
+ ## 1.9.0-pre.1
4
+
5
+ ### Patch Changes
6
+
7
+ - In order to move towards stricter TypeScript support in the future, we're aiming to replace current usages of `any` with `unknown` on exposed typings for user-provided data. To do this in Remix v2 without introducing breaking changes in React Router v6, we have added generics to a number of shared types. These continue to default to `any` in React Router and are overridden with `unknown` in Remix. In React Router v7 we plan to move these to `unknown` as a breakjing change. ([#10843](https://github.com/remix-run/react-router/pull/10843))
8
+
9
+ - `Location` now accepts a generic for the `location.state` value
10
+ - `ActionFunctionArgs`/`ActionFunction`/`LoaderFunctionArgs`/`LoaderFunction` now accept a generic for the `context` parameter (only used in SSR usages via `createStaticHandler`)
11
+ - The return type of `useMatches` (now exported as `UIMatch`) accepts generics for `match.data` and `match.handle` - both of which were already set to `unknown`
12
+
13
+ ## 1.9.0-pre.0
14
+
15
+ ### Minor Changes
16
+
17
+ - Removed internal API only required for the Remix v1 back-compat layer and no longer needed in Remix v2 (`_isFetchActionRedirect`, `_hasFetcherDoneAnything`) ([#10715](https://github.com/remix-run/react-router/pull/10715))
18
+
19
+ ### Patch Changes
20
+
21
+ - Add method/url to error message on aborted `query`/`queryRoute` calls ([#10793](https://github.com/remix-run/react-router/pull/10793))
22
+ - Move the `@private` class export `ErrorResponse` to an `UNSAFE_ErrorResponseImpl` export since it is an implementation detail and there should be no construction of `ErrorResponse` instances in userland. This frees us up to export a `type ErrorResponse` which correlates to an instance of the class via `InstanceType`. Userland code should only ever be using `ErrorResponse` as a type and should be type-narrowing via `isRouteErrorResponse`. ([#10811](https://github.com/remix-run/react-router/pull/10811))
23
+ - Export `ShouldRevalidateFunctionArgs` interface ([#10797](https://github.com/remix-run/react-router/pull/10797))
24
+ - Fix a race-condition with loader/action-thrown errors on `route.lazy` routes ([#10778](https://github.com/remix-run/react-router/pull/10778))
25
+ - Fix type for `actionResult` on the arguments object passed to `shouldRevalidate` ([#10779](https://github.com/remix-run/react-router/pull/10779))
26
+
3
27
  ## 1.8.0
4
28
 
5
29
  ### Minor Changes
package/dist/history.d.ts CHANGED
@@ -43,11 +43,11 @@ export interface Path {
43
43
  * An entry in a history stack. A location contains information about the
44
44
  * URL path, as well as possibly some arbitrary state and a key.
45
45
  */
46
- export interface Location extends Path {
46
+ export interface Location<S = any> extends Path {
47
47
  /**
48
48
  * A value of arbitrary data associated with this location.
49
49
  */
50
- state: any;
50
+ state: S;
51
51
  /**
52
52
  * A unique string associated with this location. May be used to safely store
53
53
  * and retrieve data in some other storage API, like `localStorage`.
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- export type { ActionFunction, ActionFunctionArgs, AgnosticDataIndexRouteObject, AgnosticDataNonIndexRouteObject, AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticIndexRouteObject, AgnosticNonIndexRouteObject, AgnosticRouteMatch, AgnosticRouteObject, LazyRouteFunction, TrackedPromise, FormEncType, FormMethod, HTMLFormMethod, JsonFunction, LoaderFunction, LoaderFunctionArgs, ParamParseKey, Params, PathMatch, PathPattern, RedirectFunction, ShouldRevalidateFunction, V7_FormMethod, } from "./utils";
2
- export { AbortedDeferredError, ErrorResponse, defer, generatePath, getToPathname, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, redirect, redirectDocument, resolvePath, resolveTo, stripBasename, } from "./utils";
1
+ export type { ActionFunction, ActionFunctionArgs, AgnosticDataIndexRouteObject, AgnosticDataNonIndexRouteObject, AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticIndexRouteObject, AgnosticNonIndexRouteObject, AgnosticRouteMatch, AgnosticRouteObject, ErrorResponse, FormEncType, FormMethod, HTMLFormMethod, JsonFunction, LazyRouteFunction, LoaderFunction, LoaderFunctionArgs, ParamParseKey, Params, PathMatch, PathPattern, RedirectFunction, ShouldRevalidateFunction, ShouldRevalidateFunctionArgs, TrackedPromise, UIMatch, V7_FormMethod, } from "./utils";
2
+ export { AbortedDeferredError, defer, generatePath, getToPathname, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, redirect, redirectDocument, resolvePath, resolveTo, stripBasename, } from "./utils";
3
3
  export type { BrowserHistory, BrowserHistoryOptions, HashHistory, HashHistoryOptions, History, InitialEntry, Location, MemoryHistory, MemoryHistoryOptions, Path, To, } from "./history";
4
- export { Action, createBrowserHistory, createPath, createHashHistory, createMemoryHistory, parsePath, } from "./history";
4
+ export { Action, createBrowserHistory, createHashHistory, createMemoryHistory, createPath, parsePath, } from "./history";
5
5
  export * from "./router";
6
6
  /** @internal */
7
7
  export type { RouteManifest as UNSAFE_RouteManifest } from "./utils";
8
- export { DeferredData as UNSAFE_DeferredData, convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes, getPathContributingMatches as UNSAFE_getPathContributingMatches, } from "./utils";
8
+ export { DeferredData as UNSAFE_DeferredData, ErrorResponseImpl as UNSAFE_ErrorResponseImpl, convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes, convertRouteMatchToUiMatch as UNSAFE_convertRouteMatchToUiMatch, getPathContributingMatches as UNSAFE_getPathContributingMatches, } from "./utils";
9
9
  export { invariant as UNSAFE_invariant, warning as UNSAFE_warning, } from "./history";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v1.8.0
2
+ * @remix-run/router v1.9.0-pre.1
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -45,25 +45,23 @@ let Action = /*#__PURE__*/function (Action) {
45
45
  * The pathname, search, and hash values of a URL.
46
46
  */
47
47
 
48
+ // TODO: (v7) Change the Location generic default from `any` to `unknown` and
49
+ // remove Remix `useLocation` wrapper.
48
50
  /**
49
51
  * An entry in a history stack. A location contains information about the
50
52
  * URL path, as well as possibly some arbitrary state and a key.
51
53
  */
52
-
53
54
  /**
54
55
  * A change to the current location.
55
56
  */
56
-
57
57
  /**
58
58
  * A function that receives notifications about location changes.
59
59
  */
60
-
61
60
  /**
62
61
  * Describes a location that is the destination of some navigation, either via
63
62
  * `history.push` or `history.replace`. May be either a URL or the pieces of a
64
63
  * URL path.
65
64
  */
66
-
67
65
  /**
68
66
  * A history is an interface to the navigation stack. The history serves as the
69
67
  * source of truth for the current location, as well as provides a set of
@@ -72,7 +70,6 @@ let Action = /*#__PURE__*/function (Action) {
72
70
  * It is similar to the DOM's `window.history` object, but with a smaller, more
73
71
  * focused API.
74
72
  */
75
-
76
73
  const PopStateEventType = "popstate";
77
74
  //#endregion
78
75
 
@@ -605,28 +602,28 @@ let ResultType = /*#__PURE__*/function (ResultType) {
605
602
  * this as a private implementation detail in case they diverge in the future.
606
603
  */
607
604
 
605
+ // TODO: (v7) Change the defaults from any to unknown in and remove Remix wrappers:
606
+ // ActionFunction, ActionFunctionArgs, LoaderFunction, LoaderFunctionArgs
608
607
  /**
609
608
  * Arguments passed to loader functions
610
609
  */
611
-
612
610
  /**
613
611
  * Arguments passed to action functions
614
612
  */
615
-
616
613
  /**
617
614
  * Loaders and actions can return anything except `undefined` (`null` is a
618
615
  * valid return value if there is no data to return). Responses are preferred
619
616
  * and will ease any future migration to Remix
620
617
  */
621
-
622
618
  /**
623
619
  * Route loader function signature
624
620
  */
625
-
626
621
  /**
627
622
  * Route action function signature
628
623
  */
629
-
624
+ /**
625
+ * Arguments passed to shouldRevalidate function
626
+ */
630
627
  /**
631
628
  * Route shouldRevalidate function signature. This runs after any submission
632
629
  * (navigation or fetcher), so we flatten the navigation/fetcher submission
@@ -634,25 +631,21 @@ let ResultType = /*#__PURE__*/function (ResultType) {
634
631
  * or a fetcher, what really matters is the URLs and the formData since loaders
635
632
  * have to re-run based on the data models that were potentially mutated.
636
633
  */
637
-
638
634
  /**
639
635
  * Function provided by the framework-aware layers to set `hasErrorBoundary`
640
636
  * from the framework-aware `errorElement` prop
641
637
  *
642
638
  * @deprecated Use `mapRouteProperties` instead
643
639
  */
644
-
645
640
  /**
646
641
  * Function provided by the framework-aware layers to set any framework-specific
647
642
  * properties from framework-agnostic properties
648
643
  */
649
-
650
644
  /**
651
645
  * Keys we cannot change from within a lazy() function. We spread all other keys
652
646
  * onto the route. Either they're meaningful to the router, or they'll get
653
647
  * ignored.
654
648
  */
655
-
656
649
  const immutableRouteKeys = new Set(["lazy", "caseSensitive", "path", "id", "index", "children"]);
657
650
 
658
651
  /**
@@ -769,6 +762,20 @@ function matchRoutes(routes, locationArg, basename) {
769
762
  }
770
763
  return matches;
771
764
  }
765
+ function convertRouteMatchToUiMatch(match, loaderData) {
766
+ let {
767
+ route,
768
+ pathname,
769
+ params
770
+ } = match;
771
+ return {
772
+ id: route.id,
773
+ pathname,
774
+ params,
775
+ data: loaderData[route.id],
776
+ handle: route.handle
777
+ };
778
+ }
772
779
  function flattenRoutes(routes, branches, parentsMeta, parentPath) {
773
780
  if (branches === void 0) {
774
781
  branches = [];
@@ -1465,7 +1472,7 @@ const redirectDocument = (url, init) => {
1465
1472
  * @private
1466
1473
  * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
1467
1474
  */
1468
- class ErrorResponse {
1475
+ class ErrorResponseImpl {
1469
1476
  constructor(status, statusText, data, internal) {
1470
1477
  if (internal === void 0) {
1471
1478
  internal = false;
@@ -1482,6 +1489,9 @@ class ErrorResponse {
1482
1489
  }
1483
1490
  }
1484
1491
 
1492
+ // We don't want the class exported since usage of it at runtime is an
1493
+ // implementation detail, but we do want to export the shape so folks can
1494
+ // build their own abstractions around instances via isRouteErrorResponse()
1485
1495
  /**
1486
1496
  * Check if the given error is an ErrorResponse generated from a 4xx/5xx
1487
1497
  * Response thrown from an action/loader
@@ -2466,8 +2476,7 @@ function createRouter(init) {
2466
2476
  fetchers: new Map(state.fetchers)
2467
2477
  });
2468
2478
  return startRedirectNavigation(state, actionResult, {
2469
- submission,
2470
- isFetchActionRedirect: true
2479
+ fetcherSubmission: submission
2471
2480
  });
2472
2481
  }
2473
2482
  }
@@ -2682,18 +2691,15 @@ function createRouter(init) {
2682
2691
  async function startRedirectNavigation(state, redirect, _temp) {
2683
2692
  let {
2684
2693
  submission,
2685
- replace,
2686
- isFetchActionRedirect
2694
+ fetcherSubmission,
2695
+ replace
2687
2696
  } = _temp === void 0 ? {} : _temp;
2688
2697
  if (redirect.revalidate) {
2689
2698
  isRevalidationRequired = true;
2690
2699
  }
2691
- let redirectLocation = createLocation(state.location, redirect.location, // TODO: This can be removed once we get rid of useTransition in Remix v2
2692
- _extends({
2700
+ let redirectLocation = createLocation(state.location, redirect.location, {
2693
2701
  _isRedirect: true
2694
- }, isFetchActionRedirect ? {
2695
- _isFetchActionRedirect: true
2696
- } : {}));
2702
+ });
2697
2703
  invariant(redirectLocation, "Expected a location on the redirect navigation");
2698
2704
  if (isBrowser) {
2699
2705
  let isDocumentReload = false;
@@ -2725,11 +2731,19 @@ function createRouter(init) {
2725
2731
 
2726
2732
  // Use the incoming submission if provided, fallback on the active one in
2727
2733
  // state.navigation
2728
- let activeSubmission = submission || getSubmissionFromNavigation(state.navigation);
2734
+ let {
2735
+ formMethod,
2736
+ formAction,
2737
+ formEncType
2738
+ } = state.navigation;
2739
+ if (!submission && !fetcherSubmission && formMethod && formAction && formEncType) {
2740
+ submission = getSubmissionFromNavigation(state.navigation);
2741
+ }
2729
2742
 
2730
2743
  // If this was a 307/308 submission we want to preserve the HTTP method and
2731
2744
  // re-submit the GET/POST/PUT/PATCH/DELETE as a submission navigation to the
2732
2745
  // redirected location
2746
+ let activeSubmission = submission || fetcherSubmission;
2733
2747
  if (redirectPreserveMethodStatusCodes.has(redirect.status) && activeSubmission && isMutationMethod(activeSubmission.formMethod)) {
2734
2748
  await startNavigation(redirectHistoryAction, redirectLocation, {
2735
2749
  submission: _extends({}, activeSubmission, {
@@ -2738,20 +2752,14 @@ function createRouter(init) {
2738
2752
  // Preserve this flag across redirects
2739
2753
  preventScrollReset: pendingPreventScrollReset
2740
2754
  });
2741
- } else if (isFetchActionRedirect) {
2742
- // For a fetch action redirect, we kick off a new loading navigation
2743
- // without the fetcher submission, but we send it along for shouldRevalidate
2744
- await startNavigation(redirectHistoryAction, redirectLocation, {
2745
- overrideNavigation: getLoadingNavigation(redirectLocation),
2746
- fetcherSubmission: activeSubmission,
2747
- // Preserve this flag across redirects
2748
- preventScrollReset: pendingPreventScrollReset
2749
- });
2750
2755
  } else {
2751
- // If we have a submission, we will preserve it through the redirect navigation
2752
- let overrideNavigation = getLoadingNavigation(redirectLocation, activeSubmission);
2756
+ // If we have a navigation submission, we will preserve it through the
2757
+ // redirect navigation
2758
+ let overrideNavigation = getLoadingNavigation(redirectLocation, submission);
2753
2759
  await startNavigation(redirectHistoryAction, redirectLocation, {
2754
2760
  overrideNavigation,
2761
+ // Send fetcher submissions through for shouldRevalidate
2762
+ fetcherSubmission,
2755
2763
  // Preserve this flag across redirects
2756
2764
  preventScrollReset: pendingPreventScrollReset
2757
2765
  });
@@ -2967,7 +2975,7 @@ function createRouter(init) {
2967
2975
  }
2968
2976
  function getScrollKey(location, matches) {
2969
2977
  if (getScrollRestorationKey) {
2970
- let key = getScrollRestorationKey(location, matches.map(m => createUseMatchesMatch(m, state.loaderData)));
2978
+ let key = getScrollRestorationKey(location, matches.map(m => convertRouteMatchToUiMatch(m, state.loaderData)));
2971
2979
  return key || location.key;
2972
2980
  }
2973
2981
  return location.key;
@@ -3270,7 +3278,7 @@ function createStaticHandler(routes, opts) {
3270
3278
  });
3271
3279
  if (request.signal.aborted) {
3272
3280
  let method = isRouteRequest ? "queryRoute" : "query";
3273
- throw new Error(method + "() call aborted");
3281
+ throw new Error(method + "() call aborted: " + request.method + " " + request.url);
3274
3282
  }
3275
3283
  }
3276
3284
  if (isRedirectResult(result)) {
@@ -3389,7 +3397,7 @@ function createStaticHandler(routes, opts) {
3389
3397
  }))]);
3390
3398
  if (request.signal.aborted) {
3391
3399
  let method = isRouteRequest ? "queryRoute" : "query";
3392
- throw new Error(method + "() call aborted");
3400
+ throw new Error(method + "() call aborted: " + request.method + " " + request.url);
3393
3401
  }
3394
3402
 
3395
3403
  // Process and commit output from loaders
@@ -3846,7 +3854,17 @@ async function callLoaderOrAction(type, request, match, matches, manifest, mapRo
3846
3854
  if (match.route.lazy) {
3847
3855
  if (handler) {
3848
3856
  // Run statically defined handler in parallel with lazy()
3849
- let values = await Promise.all([runHandler(handler), loadLazyRouteModule(match.route, mapRouteProperties, manifest)]);
3857
+ let handlerError;
3858
+ let values = await Promise.all([
3859
+ // If the handler throws, don't let it immediately bubble out,
3860
+ // since we need to let the lazy() execution finish so we know if this
3861
+ // route has a boundary that can handle the error
3862
+ runHandler(handler).catch(e => {
3863
+ handlerError = e;
3864
+ }), loadLazyRouteModule(match.route, mapRouteProperties, manifest)]);
3865
+ if (handlerError) {
3866
+ throw handlerError;
3867
+ }
3850
3868
  result = values[0];
3851
3869
  } else {
3852
3870
  // Load lazy route module, then run any returned handler
@@ -3954,7 +3972,7 @@ async function callLoaderOrAction(type, request, match, matches, manifest, mapRo
3954
3972
  if (resultType === ResultType.error) {
3955
3973
  return {
3956
3974
  type: resultType,
3957
- error: new ErrorResponse(status, result.statusText, data),
3975
+ error: new ErrorResponseImpl(status, result.statusText, data),
3958
3976
  headers: result.headers
3959
3977
  };
3960
3978
  }
@@ -4233,7 +4251,7 @@ function getInternalRouterError(status, _temp4) {
4233
4251
  errorMessage = "Invalid request method \"" + method.toUpperCase() + "\"";
4234
4252
  }
4235
4253
  }
4236
- return new ErrorResponse(status || 500, statusText, new Error(errorMessage), true);
4254
+ return new ErrorResponseImpl(status || 500, statusText, new Error(errorMessage), true);
4237
4255
  }
4238
4256
 
4239
4257
  // Find any returned redirect errors, starting from the lowest match
@@ -4362,23 +4380,6 @@ async function resolveDeferredData(result, signal, unwrap) {
4362
4380
  function hasNakedIndexQuery(search) {
4363
4381
  return new URLSearchParams(search).getAll("index").some(v => v === "");
4364
4382
  }
4365
-
4366
- // Note: This should match the format exported by useMatches, so if you change
4367
- // this please also change that :) Eventually we'll DRY this up
4368
- function createUseMatchesMatch(match, loaderData) {
4369
- let {
4370
- route,
4371
- pathname,
4372
- params
4373
- } = match;
4374
- return {
4375
- id: route.id,
4376
- pathname,
4377
- params,
4378
- data: loaderData[route.id],
4379
- handle: route.handle
4380
- };
4381
- }
4382
4383
  function getTargetMatch(matches, location) {
4383
4384
  let search = typeof location === "string" ? parsePath(location).search : location.search;
4384
4385
  if (matches[matches.length - 1].route.index && hasNakedIndexQuery(search || "")) {
@@ -4481,8 +4482,7 @@ function getLoadingFetcher(submission, data) {
4481
4482
  formData: submission.formData,
4482
4483
  json: submission.json,
4483
4484
  text: submission.text,
4484
- data,
4485
- " _hasFetcherDoneAnything ": true
4485
+ data
4486
4486
  };
4487
4487
  return fetcher;
4488
4488
  } else {
@@ -4494,8 +4494,7 @@ function getLoadingFetcher(submission, data) {
4494
4494
  formData: undefined,
4495
4495
  json: undefined,
4496
4496
  text: undefined,
4497
- data,
4498
- " _hasFetcherDoneAnything ": true
4497
+ data
4499
4498
  };
4500
4499
  return fetcher;
4501
4500
  }
@@ -4509,8 +4508,7 @@ function getSubmittingFetcher(submission, existingFetcher) {
4509
4508
  formData: submission.formData,
4510
4509
  json: submission.json,
4511
4510
  text: submission.text,
4512
- data: existingFetcher ? existingFetcher.data : undefined,
4513
- " _hasFetcherDoneAnything ": true
4511
+ data: existingFetcher ? existingFetcher.data : undefined
4514
4512
  };
4515
4513
  return fetcher;
4516
4514
  }
@@ -4523,8 +4521,7 @@ function getDoneFetcher(data) {
4523
4521
  formData: undefined,
4524
4522
  json: undefined,
4525
4523
  text: undefined,
4526
- data,
4527
- " _hasFetcherDoneAnything ": true
4524
+ data
4528
4525
  };
4529
4526
  return fetcher;
4530
4527
  }
@@ -4532,12 +4529,13 @@ function getDoneFetcher(data) {
4532
4529
 
4533
4530
  exports.AbortedDeferredError = AbortedDeferredError;
4534
4531
  exports.Action = Action;
4535
- exports.ErrorResponse = ErrorResponse;
4536
4532
  exports.IDLE_BLOCKER = IDLE_BLOCKER;
4537
4533
  exports.IDLE_FETCHER = IDLE_FETCHER;
4538
4534
  exports.IDLE_NAVIGATION = IDLE_NAVIGATION;
4539
4535
  exports.UNSAFE_DEFERRED_SYMBOL = UNSAFE_DEFERRED_SYMBOL;
4540
4536
  exports.UNSAFE_DeferredData = DeferredData;
4537
+ exports.UNSAFE_ErrorResponseImpl = ErrorResponseImpl;
4538
+ exports.UNSAFE_convertRouteMatchToUiMatch = convertRouteMatchToUiMatch;
4541
4539
  exports.UNSAFE_convertRoutesToDataRoutes = convertRoutesToDataRoutes;
4542
4540
  exports.UNSAFE_getPathContributingMatches = getPathContributingMatches;
4543
4541
  exports.UNSAFE_invariant = invariant;