@remix-run/router 1.0.4 → 1.0.5-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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v1.0.4
2
+ * @remix-run/router v1.0.5-pre.1
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -283,6 +283,16 @@
283
283
  //#region UTILS
284
284
  ////////////////////////////////////////////////////////////////////////////////
285
285
 
286
+ /**
287
+ * @private
288
+ */
289
+
290
+ function invariant(value, message) {
291
+ if (value === false || value === null || typeof value === "undefined") {
292
+ throw new Error(message);
293
+ }
294
+ }
295
+
286
296
  function warning$1(cond, message) {
287
297
  if (!cond) {
288
298
  // eslint-disable-next-line no-console
@@ -381,12 +391,13 @@
381
391
 
382
392
  return parsedPath;
383
393
  }
384
- function createURL(location) {
394
+ function createClientSideURL(location) {
385
395
  // window.location.origin is "null" (the literal string value) in Firefox
386
396
  // under certain conditions, notably when serving from a local HTML file
387
397
  // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
388
- let base = typeof window !== "undefined" && typeof window.location !== "undefined" && window.location.origin !== "null" ? window.location.origin : "unknown://unknown";
398
+ let base = typeof window !== "undefined" && typeof window.location !== "undefined" && window.location.origin !== "null" ? window.location.origin : window.location.href;
389
399
  let href = typeof location === "string" ? location : createPath(location);
400
+ invariant(base, "No window.location.(origin|href) available to create URL for href: " + href);
390
401
  return new URL(href, base);
391
402
  }
392
403
 
@@ -481,7 +492,7 @@
481
492
 
482
493
  encodeLocation(to) {
483
494
  // Encode a Location the same way window.location would
484
- let url = createURL(typeof to === "string" ? to : createPath(to));
495
+ let url = createClientSideURL(typeof to === "string" ? to : createPath(to));
485
496
  return {
486
497
  pathname: url.pathname,
487
498
  search: url.search,
@@ -557,7 +568,7 @@
557
568
  /**
558
569
  * Matches the given routes to a location and returns the match data.
559
570
  *
560
- * @see https://reactrouter.com/docs/en/v6/utils/match-routes
571
+ * @see https://reactrouter.com/utils/match-routes
561
572
  */
562
573
 
563
574
  function matchRoutes(routes, locationArg, basename) {
@@ -720,7 +731,7 @@
720
731
  /**
721
732
  * Returns a path with params interpolated.
722
733
  *
723
- * @see https://reactrouter.com/docs/en/v6/utils/generate-path
734
+ * @see https://reactrouter.com/utils/generate-path
724
735
  */
725
736
 
726
737
 
@@ -753,7 +764,7 @@
753
764
  * Performs pattern matching on a URL pathname and returns information about
754
765
  * the match.
755
766
  *
756
- * @see https://reactrouter.com/docs/en/v6/utils/match-path
767
+ * @see https://reactrouter.com/utils/match-path
757
768
  */
758
769
  function matchPath(pattern, pathname) {
759
770
  if (typeof pattern === "string") {
@@ -875,15 +886,6 @@
875
886
  * @private
876
887
  */
877
888
 
878
- function invariant(value, message) {
879
- if (value === false || value === null || typeof value === "undefined") {
880
- throw new Error(message);
881
- }
882
- }
883
- /**
884
- * @private
885
- */
886
-
887
889
  function warning(cond, message) {
888
890
  if (!cond) {
889
891
  // eslint-disable-next-line no-console
@@ -902,7 +904,7 @@
902
904
  /**
903
905
  * Returns a resolved path object relative to the given pathname.
904
906
  *
905
- * @see https://reactrouter.com/docs/en/v6/utils/resolve-path
907
+ * @see https://reactrouter.com/utils/resolve-path
906
908
  */
907
909
 
908
910
  function resolvePath(to, fromPathname) {
@@ -1626,7 +1628,7 @@
1626
1628
 
1627
1629
 
1628
1630
  pendingNavigationController = new AbortController();
1629
- let request = createRequest(location, pendingNavigationController.signal, opts && opts.submission);
1631
+ let request = createClientSideRequest(location, pendingNavigationController.signal, opts && opts.submission);
1630
1632
  let pendingActionData;
1631
1633
  let pendingError;
1632
1634
 
@@ -1656,7 +1658,11 @@
1656
1658
  location
1657
1659
  }, opts.submission);
1658
1660
 
1659
- loadingNavigation = navigation;
1661
+ loadingNavigation = navigation; // Create a GET request for the loaders
1662
+
1663
+ request = new Request(request.url, {
1664
+ signal: request.signal
1665
+ });
1660
1666
  } // Call loaders
1661
1667
 
1662
1668
 
@@ -1945,7 +1951,7 @@
1945
1951
  }); // Call the action for the fetcher
1946
1952
 
1947
1953
  let abortController = new AbortController();
1948
- let fetchRequest = createRequest(path, abortController.signal, submission);
1954
+ let fetchRequest = createClientSideRequest(path, abortController.signal, submission);
1949
1955
  fetchControllers.set(key, abortController);
1950
1956
  let actionResult = await callLoaderOrAction("action", fetchRequest, match, requestMatches, router.basename);
1951
1957
 
@@ -1989,7 +1995,7 @@
1989
1995
 
1990
1996
 
1991
1997
  let nextLocation = state.navigation.location || state.location;
1992
- let revalidationRequest = createRequest(nextLocation, abortController.signal);
1998
+ let revalidationRequest = createClientSideRequest(nextLocation, abortController.signal);
1993
1999
  let matches = state.navigation.state !== "idle" ? matchRoutes(dataRoutes, state.navigation.location, init.basename) : state.matches;
1994
2000
  invariant(matches, "Didn't find any matches after fetcher action");
1995
2001
  let loadId = ++incrementingLoadId;
@@ -2109,7 +2115,7 @@
2109
2115
  }); // Call the loader for this fetcher route match
2110
2116
 
2111
2117
  let abortController = new AbortController();
2112
- let fetchRequest = createRequest(path, abortController.signal);
2118
+ let fetchRequest = createClientSideRequest(path, abortController.signal);
2113
2119
  fetchControllers.set(key, abortController);
2114
2120
  let result = await callLoaderOrAction("loader", fetchRequest, match, matches, router.basename); // Deferred isn't supported or fetcher loads, await everything and treat it
2115
2121
  // as a normal load. resolveDeferredData will return undefined if this
@@ -2189,21 +2195,27 @@
2189
2195
 
2190
2196
 
2191
2197
  async function startRedirectNavigation(state, redirect, replace) {
2198
+ var _window;
2199
+
2192
2200
  if (redirect.revalidate) {
2193
2201
  isRevalidationRequired = true;
2194
2202
  }
2195
2203
 
2196
2204
  let redirectLocation = createLocation(state.location, redirect.location);
2197
- invariant(redirectLocation, "Expected a location on the redirect navigation");
2205
+ invariant(redirectLocation, "Expected a location on the redirect navigation"); // Check if this an external redirect that goes to a new origin
2198
2206
 
2199
- if (redirect.external && typeof window !== "undefined" && typeof window.location !== "undefined") {
2200
- if (replace) {
2201
- window.location.replace(redirect.location);
2202
- } else {
2203
- window.location.assign(redirect.location);
2204
- }
2207
+ if (typeof ((_window = window) == null ? void 0 : _window.location) !== "undefined") {
2208
+ let newOrigin = createClientSideURL(redirect.location).origin;
2205
2209
 
2206
- return;
2210
+ if (window.location.origin !== newOrigin) {
2211
+ if (replace) {
2212
+ window.location.replace(redirect.location);
2213
+ } else {
2214
+ window.location.assign(redirect.location);
2215
+ }
2216
+
2217
+ return;
2218
+ }
2207
2219
  } // There's no need to abort on redirects, since we don't detect the
2208
2220
  // redirect until the action/loaders have settled
2209
2221
 
@@ -2250,7 +2262,7 @@
2250
2262
  // accordingly
2251
2263
  let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, router.basename)), ...fetchersToLoad.map(_ref8 => {
2252
2264
  let [, href, match, fetchMatches] = _ref8;
2253
- return callLoaderOrAction("loader", createRequest(href, request.signal), match, fetchMatches, router.basename);
2265
+ return callLoaderOrAction("loader", createClientSideRequest(href, request.signal), match, fetchMatches, router.basename);
2254
2266
  })]);
2255
2267
  let loaderResults = results.slice(0, matchesToLoad.length);
2256
2268
  let fetcherResults = results.slice(matchesToLoad.length);
@@ -2534,7 +2546,7 @@
2534
2546
 
2535
2547
  let result = await queryImpl(request, location, matches);
2536
2548
 
2537
- if (result instanceof Response) {
2549
+ if (isResponse(result)) {
2538
2550
  return result;
2539
2551
  } // When returning StaticHandlerContext, we patch back in the location here
2540
2552
  // since we need it for React Context. But this helps keep our submit and
@@ -2600,7 +2612,7 @@
2600
2612
 
2601
2613
  let result = await queryImpl(request, location, matches, match);
2602
2614
 
2603
- if (result instanceof Response) {
2615
+ if (isResponse(result)) {
2604
2616
  return result;
2605
2617
  }
2606
2618
 
@@ -2629,7 +2641,7 @@
2629
2641
  }
2630
2642
 
2631
2643
  let result = await loadRouteData(request, matches, routeMatch);
2632
- return result instanceof Response ? result : _extends({}, result, {
2644
+ return isResponse(result) ? result : _extends({}, result, {
2633
2645
  actionData: null,
2634
2646
  actionHeaders: {}
2635
2647
  });
@@ -2661,7 +2673,7 @@
2661
2673
  if (!actionMatch.route.action) {
2662
2674
  let error = getInternalRouterError(405, {
2663
2675
  method: request.method,
2664
- pathname: createURL(request.url).pathname,
2676
+ pathname: new URL(request.url).pathname,
2665
2677
  routeId: actionMatch.route.id
2666
2678
  });
2667
2679
 
@@ -2736,9 +2748,13 @@
2736
2748
  [actionMatch.route.id]: result.headers
2737
2749
  } : {})
2738
2750
  });
2739
- }
2751
+ } // Create a GET request for the loaders
2752
+
2740
2753
 
2741
- let context = await loadRouteData(request, matches);
2754
+ let loaderRequest = new Request(request.url, {
2755
+ signal: request.signal
2756
+ });
2757
+ let context = await loadRouteData(loaderRequest, matches);
2742
2758
  return _extends({}, context, result.statusCode ? {
2743
2759
  statusCode: result.statusCode
2744
2760
  } : {}, {
@@ -2757,7 +2773,7 @@
2757
2773
  if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
2758
2774
  throw getInternalRouterError(400, {
2759
2775
  method: request.method,
2760
- pathname: createURL(request.url).pathname,
2776
+ pathname: new URL(request.url).pathname,
2761
2777
  routeId: routeMatch == null ? void 0 : routeMatch.route.id
2762
2778
  });
2763
2779
  }
@@ -2951,9 +2967,9 @@
2951
2967
  }
2952
2968
 
2953
2969
  function shouldRevalidateLoader(currentLocation, currentMatch, submission, location, match, isRevalidationRequired, actionResult) {
2954
- let currentUrl = createURL(currentLocation);
2970
+ let currentUrl = createClientSideURL(currentLocation);
2955
2971
  let currentParams = currentMatch.params;
2956
- let nextUrl = createURL(location);
2972
+ let nextUrl = createClientSideURL(location);
2957
2973
  let nextParams = match.params; // This is the default implementation as to when we revalidate. If the route
2958
2974
  // provides it's own implementation, then we give them full control but
2959
2975
  // provide this value so they can leverage it if needed after they check
@@ -3023,20 +3039,18 @@
3023
3039
  request.signal.removeEventListener("abort", onReject);
3024
3040
  }
3025
3041
 
3026
- if (result instanceof Response) {
3042
+ if (isResponse(result)) {
3027
3043
  let status = result.status; // Process redirects
3028
3044
 
3029
3045
  if (redirectStatusCodes.has(status)) {
3030
3046
  let location = result.headers.get("Location");
3031
- invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header"); // Check if this an external redirect that goes to a new origin
3032
-
3033
- let external = createURL(location).origin !== createURL("/").origin; // Support relative routing in internal redirects
3047
+ invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header");
3048
+ let isAbsolute = /^[a-z+]+:\/\//i.test(location) || location.startsWith("//"); // Support relative routing in internal redirects
3034
3049
 
3035
- if (!external) {
3050
+ if (!isAbsolute) {
3036
3051
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
3037
3052
  let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
3038
- let requestPath = createURL(request.url).pathname;
3039
- let resolvedLocation = resolveTo(location, routePathnames, requestPath);
3053
+ let resolvedLocation = resolveTo(location, routePathnames, new URL(request.url).pathname);
3040
3054
  invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
3041
3055
 
3042
3056
  if (basename) {
@@ -3060,8 +3074,7 @@
3060
3074
  type: ResultType.redirect,
3061
3075
  status,
3062
3076
  location,
3063
- revalidate: result.headers.get("X-Remix-Revalidate") !== null,
3064
- external
3077
+ revalidate: result.headers.get("X-Remix-Revalidate") !== null
3065
3078
  };
3066
3079
  } // For SSR single-route requests, we want to hand Responses back directly
3067
3080
  // without unwrapping. We do this with the QueryRouteResponse wrapper
@@ -3119,10 +3132,13 @@
3119
3132
  type: ResultType.data,
3120
3133
  data: result
3121
3134
  };
3122
- }
3135
+ } // Utility method for creating the Request instances for loaders/actions during
3136
+ // client-side navigations and fetches. During SSR we will always have a
3137
+ // Request instance from the static handler (query/queryRoute)
3138
+
3123
3139
 
3124
- function createRequest(location, signal, submission) {
3125
- let url = createURL(stripHashFromPath(location)).toString();
3140
+ function createClientSideRequest(location, signal, submission) {
3141
+ let url = createClientSideURL(stripHashFromPath(location)).toString();
3126
3142
  let init = {
3127
3143
  signal
3128
3144
  };
@@ -3376,8 +3392,12 @@
3376
3392
  return (result && result.type) === ResultType.redirect;
3377
3393
  }
3378
3394
 
3395
+ function isResponse(value) {
3396
+ return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
3397
+ }
3398
+
3379
3399
  function isRedirectResponse(result) {
3380
- if (!(result instanceof Response)) {
3400
+ if (!isResponse(result)) {
3381
3401
  return false;
3382
3402
  }
3383
3403
 
@@ -3387,7 +3407,7 @@
3387
3407
  }
3388
3408
 
3389
3409
  function isQueryRouteResponse(obj) {
3390
- return obj && obj.response instanceof Response && (obj.type === ResultType.data || ResultType.error);
3410
+ return obj && isResponse(obj.response) && (obj.type === ResultType.data || ResultType.error);
3391
3411
  }
3392
3412
 
3393
3413
  function isValidMethod(method) {