@remix-run/router 1.18.0 → 1.19.0-pre.0

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/dist/router.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { History, Location, Path, To } from "./history";
2
2
  import { Action as HistoryAction } from "./history";
3
- import type { AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticRouteObject, DataStrategyFunction, DeferredData, DetectErrorBoundaryFunction, FormEncType, HTMLFormMethod, MapRoutePropertiesFunction, RouteData, Submission, UIMatch, AgnosticPatchRoutesOnMissFunction } from "./utils";
3
+ import type { AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticRouteObject, DataStrategyFunction, DeferredData, DetectErrorBoundaryFunction, FormEncType, HTMLFormMethod, MapRoutePropertiesFunction, RouteData, Submission, UIMatch, AgnosticPatchRoutesOnMissFunction, DataWithResponseInit } from "./utils";
4
4
  /**
5
5
  * A Router instance manages all navigation and data loading/mutations
6
6
  */
@@ -520,5 +520,6 @@ export declare function createStaticHandler(routes: AgnosticRouteObject[], opts?
520
520
  * provide an updated StaticHandlerContext suitable for a second SSR render
521
521
  */
522
522
  export declare function getStaticContextFromError(routes: AgnosticDataRouteObject[], context: StaticHandlerContext, error: any): StaticHandlerContext;
523
+ export declare function isDataWithResponseInit(value: any): value is DataWithResponseInit<unknown>;
523
524
  export declare function isDeferredData(value: any): value is DeferredData;
524
525
  export {};
package/dist/router.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v1.18.0
2
+ * @remix-run/router v1.19.0-pre.0
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -1059,6 +1059,22 @@ const json = function json(data, init) {
1059
1059
  headers
1060
1060
  }));
1061
1061
  };
1062
+ class DataWithResponseInit {
1063
+ constructor(data, init) {
1064
+ this.type = "DataWithResponseInit";
1065
+ this.data = data;
1066
+ this.init = init || null;
1067
+ }
1068
+ }
1069
+ /**
1070
+ * Create "responses" that contain `status`/`headers` without forcing
1071
+ * serialization into an actual `Response` - used by Remix single fetch
1072
+ */
1073
+ function data(data, init) {
1074
+ return new DataWithResponseInit(data, typeof init === "number" ? {
1075
+ status: init
1076
+ } : init);
1077
+ }
1062
1078
  class AbortedDeferredError extends Error {}
1063
1079
  class DeferredData {
1064
1080
  constructor(data, responseInit) {
@@ -1236,6 +1252,17 @@ const redirectDocument = (url, init) => {
1236
1252
  response.headers.set("X-Remix-Reload-Document", "true");
1237
1253
  return response;
1238
1254
  };
1255
+ /**
1256
+ * A redirect response that will perform a `history.replaceState` instead of a
1257
+ * `history.pushState` for client-side navigation redirects.
1258
+ * Sets the status code and the `Location` header.
1259
+ * Defaults to "302 Found".
1260
+ */
1261
+ const replace = (url, init) => {
1262
+ let response = redirect(url, init);
1263
+ response.headers.set("X-Remix-Replace", "true");
1264
+ return response;
1265
+ };
1239
1266
  /**
1240
1267
  * @private
1241
1268
  * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
@@ -1386,7 +1413,7 @@ function createRouter(init) {
1386
1413
  // In SSR apps (with `hydrationData`), we expect that the server will send
1387
1414
  // up the proper matched routes so we don't want to run lazy discovery on
1388
1415
  // initial hydration and want to hydrate into the splat route.
1389
- if (initialMatches && patchRoutesOnMissImpl && !init.hydrationData) {
1416
+ if (initialMatches && !init.hydrationData) {
1390
1417
  let fogOfWar = checkFogOfWar(initialMatches, dataRoutes, init.history.location.pathname);
1391
1418
  if (fogOfWar.active) {
1392
1419
  initialMatches = null;
@@ -1394,9 +1421,17 @@ function createRouter(init) {
1394
1421
  }
1395
1422
  let initialized;
1396
1423
  if (!initialMatches) {
1397
- // We need to run patchRoutesOnMiss in initialize()
1398
1424
  initialized = false;
1399
1425
  initialMatches = [];
1426
+ // If partial hydration and fog of war is enabled, we will be running
1427
+ // `patchRoutesOnMiss` during hydration so include any partial matches as
1428
+ // the initial matches so we can properly render `HydrateFallback`'s
1429
+ if (future.v7_partialHydration) {
1430
+ let fogOfWar = checkFogOfWar(null, dataRoutes, init.history.location.pathname);
1431
+ if (fogOfWar.active && fogOfWar.matches) {
1432
+ initialMatches = fogOfWar.matches;
1433
+ }
1434
+ }
1400
1435
  } else if (initialMatches.some(m => m.route.lazy)) {
1401
1436
  // All initialMatches need to be loaded before we're ready. If we have lazy
1402
1437
  // functions around still then we'll need to run them in initialize()
@@ -1478,7 +1513,7 @@ function createRouter(init) {
1478
1513
  let cancelledDeferredRoutes = [];
1479
1514
  // Use this internal array to capture fetcher loads that were cancelled by an
1480
1515
  // action navigation and require revalidation
1481
- let cancelledFetcherLoads = [];
1516
+ let cancelledFetcherLoads = new Set();
1482
1517
  // AbortControllers for any in-flight fetchers
1483
1518
  let fetchControllers = new Map();
1484
1519
  // Track loads based on the order in which they started
@@ -1749,7 +1784,6 @@ function createRouter(init) {
1749
1784
  isUninterruptedRevalidation = false;
1750
1785
  isRevalidationRequired = false;
1751
1786
  cancelledDeferredRoutes = [];
1752
- cancelledFetcherLoads = [];
1753
1787
  }
1754
1788
  // Trigger a navigation event, which can either be a numerical POP or a PUSH
1755
1789
  // replace with an optional submission
@@ -2698,7 +2732,7 @@ function createRouter(init) {
2698
2732
  // There's no need to abort on redirects, since we don't detect the
2699
2733
  // redirect until the action/loaders have settled
2700
2734
  pendingNavigationController = null;
2701
- let redirectHistoryAction = replace === true ? Action.Replace : Action.Push;
2735
+ let redirectHistoryAction = replace === true || redirect.response.headers.has("X-Remix-Replace") ? Action.Replace : Action.Push;
2702
2736
  // Use the incoming submission if provided, fallback on the active one in
2703
2737
  // state.navigation
2704
2738
  let {
@@ -2787,7 +2821,7 @@ function createRouter(init) {
2787
2821
  // Abort in-flight fetcher loads
2788
2822
  fetchLoadMatches.forEach((_, key) => {
2789
2823
  if (fetchControllers.has(key)) {
2790
- cancelledFetcherLoads.push(key);
2824
+ cancelledFetcherLoads.add(key);
2791
2825
  abortFetcher(key);
2792
2826
  }
2793
2827
  });
@@ -2841,6 +2875,7 @@ function createRouter(init) {
2841
2875
  fetchReloadIds.delete(key);
2842
2876
  fetchRedirectIds.delete(key);
2843
2877
  deletedFetchers.delete(key);
2878
+ cancelledFetcherLoads.delete(key);
2844
2879
  state.fetchers.delete(key);
2845
2880
  }
2846
2881
  function deleteFetcherAndUpdateState(key) {
@@ -3909,8 +3944,9 @@ function getMatchesToLoad(history, state, matches, submission, location, isIniti
3909
3944
  if (fetchRedirectIds.has(key)) {
3910
3945
  // Never trigger a revalidation of an actively redirecting fetcher
3911
3946
  shouldRevalidate = false;
3912
- } else if (cancelledFetcherLoads.includes(key)) {
3913
- // Always revalidate if the fetcher was cancelled
3947
+ } else if (cancelledFetcherLoads.has(key)) {
3948
+ // Always mark for revalidation if the fetcher was cancelled
3949
+ cancelledFetcherLoads.delete(key);
3914
3950
  shouldRevalidate = true;
3915
3951
  } else if (fetcher && fetcher.state !== "idle" && fetcher.data === undefined) {
3916
3952
  // If the fetcher hasn't ever completed loading yet, then this isn't a
@@ -4219,8 +4255,7 @@ async function callLoaderOrAction(type, request, match, manifest, mapRouteProper
4219
4255
  async function convertHandlerResultToDataResult(handlerResult) {
4220
4256
  let {
4221
4257
  result,
4222
- type,
4223
- status
4258
+ type
4224
4259
  } = handlerResult;
4225
4260
  if (isResponse(result)) {
4226
4261
  let data;
@@ -4259,25 +4294,46 @@ async function convertHandlerResultToDataResult(handlerResult) {
4259
4294
  };
4260
4295
  }
4261
4296
  if (type === ResultType.error) {
4297
+ if (isDataWithResponseInit(result)) {
4298
+ var _result$init2;
4299
+ if (result.data instanceof Error) {
4300
+ var _result$init;
4301
+ return {
4302
+ type: ResultType.error,
4303
+ error: result.data,
4304
+ statusCode: (_result$init = result.init) == null ? void 0 : _result$init.status
4305
+ };
4306
+ }
4307
+ // Convert thrown unstable_data() to ErrorResponse instances
4308
+ result = new ErrorResponseImpl(((_result$init2 = result.init) == null ? void 0 : _result$init2.status) || 500, undefined, result.data);
4309
+ }
4262
4310
  return {
4263
4311
  type: ResultType.error,
4264
4312
  error: result,
4265
- statusCode: isRouteErrorResponse(result) ? result.status : status
4313
+ statusCode: isRouteErrorResponse(result) ? result.status : undefined
4266
4314
  };
4267
4315
  }
4268
4316
  if (isDeferredData(result)) {
4269
- var _result$init, _result$init2;
4317
+ var _result$init3, _result$init4;
4270
4318
  return {
4271
4319
  type: ResultType.deferred,
4272
4320
  deferredData: result,
4273
- statusCode: (_result$init = result.init) == null ? void 0 : _result$init.status,
4274
- headers: ((_result$init2 = result.init) == null ? void 0 : _result$init2.headers) && new Headers(result.init.headers)
4321
+ statusCode: (_result$init3 = result.init) == null ? void 0 : _result$init3.status,
4322
+ headers: ((_result$init4 = result.init) == null ? void 0 : _result$init4.headers) && new Headers(result.init.headers)
4323
+ };
4324
+ }
4325
+ if (isDataWithResponseInit(result)) {
4326
+ var _result$init5, _result$init6;
4327
+ return {
4328
+ type: ResultType.data,
4329
+ data: result.data,
4330
+ statusCode: (_result$init5 = result.init) == null ? void 0 : _result$init5.status,
4331
+ headers: (_result$init6 = result.init) != null && _result$init6.headers ? new Headers(result.init.headers) : undefined
4275
4332
  };
4276
4333
  }
4277
4334
  return {
4278
4335
  type: ResultType.data,
4279
- data: result,
4280
- statusCode: status
4336
+ data: result
4281
4337
  };
4282
4338
  }
4283
4339
  // Support relative routing in internal redirects
@@ -4629,6 +4685,9 @@ function isErrorResult(result) {
4629
4685
  function isRedirectResult(result) {
4630
4686
  return (result && result.type) === ResultType.redirect;
4631
4687
  }
4688
+ function isDataWithResponseInit(value) {
4689
+ return typeof value === "object" && value != null && "type" in value && "data" in value && "init" in value && value.type === "DataWithResponseInit";
4690
+ }
4632
4691
  function isDeferredData(value) {
4633
4692
  let deferred = value;
4634
4693
  return deferred && typeof deferred === "object" && typeof deferred.data === "object" && typeof deferred.subscribe === "function" && typeof deferred.cancel === "function" && typeof deferred.resolveData === "function";
@@ -4881,5 +4940,5 @@ function persistAppliedTransitions(_window, transitions) {
4881
4940
  }
4882
4941
  //#endregion
4883
4942
 
4884
- export { AbortedDeferredError, Action, IDLE_BLOCKER, IDLE_FETCHER, IDLE_NAVIGATION, UNSAFE_DEFERRED_SYMBOL, DeferredData as UNSAFE_DeferredData, ErrorResponseImpl as UNSAFE_ErrorResponseImpl, convertRouteMatchToUiMatch as UNSAFE_convertRouteMatchToUiMatch, convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes, decodePath as UNSAFE_decodePath, getResolveToMatches as UNSAFE_getResolveToMatches, invariant as UNSAFE_invariant, warning as UNSAFE_warning, createBrowserHistory, createHashHistory, createMemoryHistory, createPath, createRouter, createStaticHandler, defer, generatePath, getStaticContextFromError, getToPathname, isDeferredData, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, parsePath, redirect, redirectDocument, resolvePath, resolveTo, stripBasename };
4943
+ export { AbortedDeferredError, Action, IDLE_BLOCKER, IDLE_FETCHER, IDLE_NAVIGATION, UNSAFE_DEFERRED_SYMBOL, DeferredData as UNSAFE_DeferredData, ErrorResponseImpl as UNSAFE_ErrorResponseImpl, convertRouteMatchToUiMatch as UNSAFE_convertRouteMatchToUiMatch, convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes, decodePath as UNSAFE_decodePath, getResolveToMatches as UNSAFE_getResolveToMatches, invariant as UNSAFE_invariant, warning as UNSAFE_warning, createBrowserHistory, createHashHistory, createMemoryHistory, createPath, createRouter, createStaticHandler, defer, generatePath, getStaticContextFromError, getToPathname, isDataWithResponseInit, isDeferredData, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, parsePath, redirect, redirectDocument, replace, resolvePath, resolveTo, stripBasename, data as unstable_data };
4885
4944
  //# sourceMappingURL=router.js.map