@remix-run/router 1.0.4 → 1.0.5-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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v1.0.4
2
+ * @remix-run/router v1.0.5-pre.0
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
@@ -2250,7 +2256,7 @@
2250
2256
  // accordingly
2251
2257
  let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, router.basename)), ...fetchersToLoad.map(_ref8 => {
2252
2258
  let [, href, match, fetchMatches] = _ref8;
2253
- return callLoaderOrAction("loader", createRequest(href, request.signal), match, fetchMatches, router.basename);
2259
+ return callLoaderOrAction("loader", createClientSideRequest(href, request.signal), match, fetchMatches, router.basename);
2254
2260
  })]);
2255
2261
  let loaderResults = results.slice(0, matchesToLoad.length);
2256
2262
  let fetcherResults = results.slice(matchesToLoad.length);
@@ -2661,7 +2667,7 @@
2661
2667
  if (!actionMatch.route.action) {
2662
2668
  let error = getInternalRouterError(405, {
2663
2669
  method: request.method,
2664
- pathname: createURL(request.url).pathname,
2670
+ pathname: new URL(request.url).pathname,
2665
2671
  routeId: actionMatch.route.id
2666
2672
  });
2667
2673
 
@@ -2736,9 +2742,13 @@
2736
2742
  [actionMatch.route.id]: result.headers
2737
2743
  } : {})
2738
2744
  });
2739
- }
2745
+ } // Create a GET request for the loaders
2746
+
2740
2747
 
2741
- let context = await loadRouteData(request, matches);
2748
+ let loaderRequest = new Request(request.url, {
2749
+ signal: request.signal
2750
+ });
2751
+ let context = await loadRouteData(loaderRequest, matches);
2742
2752
  return _extends({}, context, result.statusCode ? {
2743
2753
  statusCode: result.statusCode
2744
2754
  } : {}, {
@@ -2757,7 +2767,7 @@
2757
2767
  if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
2758
2768
  throw getInternalRouterError(400, {
2759
2769
  method: request.method,
2760
- pathname: createURL(request.url).pathname,
2770
+ pathname: new URL(request.url).pathname,
2761
2771
  routeId: routeMatch == null ? void 0 : routeMatch.route.id
2762
2772
  });
2763
2773
  }
@@ -2951,9 +2961,9 @@
2951
2961
  }
2952
2962
 
2953
2963
  function shouldRevalidateLoader(currentLocation, currentMatch, submission, location, match, isRevalidationRequired, actionResult) {
2954
- let currentUrl = createURL(currentLocation);
2964
+ let currentUrl = createClientSideURL(currentLocation);
2955
2965
  let currentParams = currentMatch.params;
2956
- let nextUrl = createURL(location);
2966
+ let nextUrl = createClientSideURL(location);
2957
2967
  let nextParams = match.params; // This is the default implementation as to when we revalidate. If the route
2958
2968
  // provides it's own implementation, then we give them full control but
2959
2969
  // provide this value so they can leverage it if needed after they check
@@ -3030,13 +3040,15 @@
3030
3040
  let location = result.headers.get("Location");
3031
3041
  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
3042
 
3033
- let external = createURL(location).origin !== createURL("/").origin; // Support relative routing in internal redirects
3043
+ let currentUrl = new URL(request.url);
3044
+ let currentOrigin = currentUrl.origin;
3045
+ let newOrigin = new URL(location, currentOrigin).origin;
3046
+ let external = newOrigin !== currentOrigin; // Support relative routing in internal redirects
3034
3047
 
3035
3048
  if (!external) {
3036
3049
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
3037
3050
  let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
3038
- let requestPath = createURL(request.url).pathname;
3039
- let resolvedLocation = resolveTo(location, routePathnames, requestPath);
3051
+ let resolvedLocation = resolveTo(location, routePathnames, currentUrl.pathname);
3040
3052
  invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
3041
3053
 
3042
3054
  if (basename) {
@@ -3119,10 +3131,13 @@
3119
3131
  type: ResultType.data,
3120
3132
  data: result
3121
3133
  };
3122
- }
3134
+ } // Utility method for creating the Request instances for loaders/actions during
3135
+ // client-side navigations and fetches. During SSR we will always have a
3136
+ // Request instance from the static handler (query/queryRoute)
3137
+
3123
3138
 
3124
- function createRequest(location, signal, submission) {
3125
- let url = createURL(stripHashFromPath(location)).toString();
3139
+ function createClientSideRequest(location, signal, submission) {
3140
+ let url = createClientSideURL(stripHashFromPath(location)).toString();
3126
3141
  let init = {
3127
3142
  signal
3128
3143
  };