@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.
package/dist/router.js CHANGED
@@ -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
  *
@@ -253,10 +253,12 @@ function createHashHistory(options) {
253
253
  }
254
254
 
255
255
  return getUrlBasedHistory(createHashLocation, createHashHref, validateHashLocation, options);
256
- } //#endregion
257
- ////////////////////////////////////////////////////////////////////////////////
258
- //#region UTILS
259
- ////////////////////////////////////////////////////////////////////////////////
256
+ }
257
+ function invariant(value, message) {
258
+ if (value === false || value === null || typeof value === "undefined") {
259
+ throw new Error(message);
260
+ }
261
+ }
260
262
 
261
263
  function warning$1(cond, message) {
262
264
  if (!cond) {
@@ -356,12 +358,13 @@ function parsePath(path) {
356
358
 
357
359
  return parsedPath;
358
360
  }
359
- function createURL(location) {
361
+ function createClientSideURL(location) {
360
362
  // window.location.origin is "null" (the literal string value) in Firefox
361
363
  // under certain conditions, notably when serving from a local HTML file
362
364
  // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
363
- let base = typeof window !== "undefined" && typeof window.location !== "undefined" && window.location.origin !== "null" ? window.location.origin : "unknown://unknown";
365
+ let base = typeof window !== "undefined" && typeof window.location !== "undefined" && window.location.origin !== "null" ? window.location.origin : window.location.href;
364
366
  let href = typeof location === "string" ? location : createPath(location);
367
+ invariant(base, "No window.location.(origin|href) available to create URL for href: " + href);
365
368
  return new URL(href, base);
366
369
  }
367
370
 
@@ -456,7 +459,7 @@ function getUrlBasedHistory(getLocation, createHref, validateLocation, options)
456
459
 
457
460
  encodeLocation(to) {
458
461
  // Encode a Location the same way window.location would
459
- let url = createURL(typeof to === "string" ? to : createPath(to));
462
+ let url = createClientSideURL(typeof to === "string" ? to : createPath(to));
460
463
  return {
461
464
  pathname: url.pathname,
462
465
  search: url.search,
@@ -525,7 +528,7 @@ function convertRoutesToDataRoutes(routes, parentPath, allIds) {
525
528
  /**
526
529
  * Matches the given routes to a location and returns the match data.
527
530
  *
528
- * @see https://reactrouter.com/docs/en/v6/utils/match-routes
531
+ * @see https://reactrouter.com/utils/match-routes
529
532
  */
530
533
 
531
534
  function matchRoutes(routes, locationArg, basename) {
@@ -688,7 +691,7 @@ function matchRouteBranch(branch, pathname) {
688
691
  /**
689
692
  * Returns a path with params interpolated.
690
693
  *
691
- * @see https://reactrouter.com/docs/en/v6/utils/generate-path
694
+ * @see https://reactrouter.com/utils/generate-path
692
695
  */
693
696
 
694
697
 
@@ -717,7 +720,7 @@ function generatePath(path, params) {
717
720
  * Performs pattern matching on a URL pathname and returns information about
718
721
  * the match.
719
722
  *
720
- * @see https://reactrouter.com/docs/en/v6/utils/match-path
723
+ * @see https://reactrouter.com/utils/match-path
721
724
  */
722
725
 
723
726
  function matchPath(pattern, pathname) {
@@ -836,11 +839,6 @@ function stripBasename(pathname, basename) {
836
839
 
837
840
  return pathname.slice(startIndex) || "/";
838
841
  }
839
- function invariant(value, message) {
840
- if (value === false || value === null || typeof value === "undefined") {
841
- throw new Error(message);
842
- }
843
- }
844
842
  /**
845
843
  * @private
846
844
  */
@@ -863,7 +861,7 @@ function warning(cond, message) {
863
861
  /**
864
862
  * Returns a resolved path object relative to the given pathname.
865
863
  *
866
- * @see https://reactrouter.com/docs/en/v6/utils/resolve-path
864
+ * @see https://reactrouter.com/utils/resolve-path
867
865
  */
868
866
 
869
867
  function resolvePath(to, fromPathname) {
@@ -1580,7 +1578,7 @@ function createRouter(init) {
1580
1578
 
1581
1579
 
1582
1580
  pendingNavigationController = new AbortController();
1583
- let request = createRequest(location, pendingNavigationController.signal, opts && opts.submission);
1581
+ let request = createClientSideRequest(location, pendingNavigationController.signal, opts && opts.submission);
1584
1582
  let pendingActionData;
1585
1583
  let pendingError;
1586
1584
 
@@ -1610,7 +1608,11 @@ function createRouter(init) {
1610
1608
  location
1611
1609
  }, opts.submission);
1612
1610
 
1613
- loadingNavigation = navigation;
1611
+ loadingNavigation = navigation; // Create a GET request for the loaders
1612
+
1613
+ request = new Request(request.url, {
1614
+ signal: request.signal
1615
+ });
1614
1616
  } // Call loaders
1615
1617
 
1616
1618
 
@@ -1899,7 +1901,7 @@ function createRouter(init) {
1899
1901
  }); // Call the action for the fetcher
1900
1902
 
1901
1903
  let abortController = new AbortController();
1902
- let fetchRequest = createRequest(path, abortController.signal, submission);
1904
+ let fetchRequest = createClientSideRequest(path, abortController.signal, submission);
1903
1905
  fetchControllers.set(key, abortController);
1904
1906
  let actionResult = await callLoaderOrAction("action", fetchRequest, match, requestMatches, router.basename);
1905
1907
 
@@ -1943,7 +1945,7 @@ function createRouter(init) {
1943
1945
 
1944
1946
 
1945
1947
  let nextLocation = state.navigation.location || state.location;
1946
- let revalidationRequest = createRequest(nextLocation, abortController.signal);
1948
+ let revalidationRequest = createClientSideRequest(nextLocation, abortController.signal);
1947
1949
  let matches = state.navigation.state !== "idle" ? matchRoutes(dataRoutes, state.navigation.location, init.basename) : state.matches;
1948
1950
  invariant(matches, "Didn't find any matches after fetcher action");
1949
1951
  let loadId = ++incrementingLoadId;
@@ -2063,7 +2065,7 @@ function createRouter(init) {
2063
2065
  }); // Call the loader for this fetcher route match
2064
2066
 
2065
2067
  let abortController = new AbortController();
2066
- let fetchRequest = createRequest(path, abortController.signal);
2068
+ let fetchRequest = createClientSideRequest(path, abortController.signal);
2067
2069
  fetchControllers.set(key, abortController);
2068
2070
  let result = await callLoaderOrAction("loader", fetchRequest, match, matches, router.basename); // Deferred isn't supported or fetcher loads, await everything and treat it
2069
2071
  // as a normal load. resolveDeferredData will return undefined if this
@@ -2204,7 +2206,7 @@ function createRouter(init) {
2204
2206
  // accordingly
2205
2207
  let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, router.basename)), ...fetchersToLoad.map(_ref8 => {
2206
2208
  let [, href, match, fetchMatches] = _ref8;
2207
- return callLoaderOrAction("loader", createRequest(href, request.signal), match, fetchMatches, router.basename);
2209
+ return callLoaderOrAction("loader", createClientSideRequest(href, request.signal), match, fetchMatches, router.basename);
2208
2210
  })]);
2209
2211
  let loaderResults = results.slice(0, matchesToLoad.length);
2210
2212
  let fetcherResults = results.slice(matchesToLoad.length);
@@ -2615,7 +2617,7 @@ function unstable_createStaticHandler(routes, opts) {
2615
2617
  if (!actionMatch.route.action) {
2616
2618
  let error = getInternalRouterError(405, {
2617
2619
  method: request.method,
2618
- pathname: createURL(request.url).pathname,
2620
+ pathname: new URL(request.url).pathname,
2619
2621
  routeId: actionMatch.route.id
2620
2622
  });
2621
2623
 
@@ -2690,9 +2692,13 @@ function unstable_createStaticHandler(routes, opts) {
2690
2692
  [actionMatch.route.id]: result.headers
2691
2693
  } : {})
2692
2694
  });
2693
- }
2695
+ } // Create a GET request for the loaders
2696
+
2694
2697
 
2695
- let context = await loadRouteData(request, matches);
2698
+ let loaderRequest = new Request(request.url, {
2699
+ signal: request.signal
2700
+ });
2701
+ let context = await loadRouteData(loaderRequest, matches);
2696
2702
  return _extends({}, context, result.statusCode ? {
2697
2703
  statusCode: result.statusCode
2698
2704
  } : {}, {
@@ -2711,7 +2717,7 @@ function unstable_createStaticHandler(routes, opts) {
2711
2717
  if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
2712
2718
  throw getInternalRouterError(400, {
2713
2719
  method: request.method,
2714
- pathname: createURL(request.url).pathname,
2720
+ pathname: new URL(request.url).pathname,
2715
2721
  routeId: routeMatch == null ? void 0 : routeMatch.route.id
2716
2722
  });
2717
2723
  }
@@ -2905,9 +2911,9 @@ function isNewRouteInstance(currentMatch, match) {
2905
2911
  }
2906
2912
 
2907
2913
  function shouldRevalidateLoader(currentLocation, currentMatch, submission, location, match, isRevalidationRequired, actionResult) {
2908
- let currentUrl = createURL(currentLocation);
2914
+ let currentUrl = createClientSideURL(currentLocation);
2909
2915
  let currentParams = currentMatch.params;
2910
- let nextUrl = createURL(location);
2916
+ let nextUrl = createClientSideURL(location);
2911
2917
  let nextParams = match.params; // This is the default implementation as to when we revalidate. If the route
2912
2918
  // provides it's own implementation, then we give them full control but
2913
2919
  // provide this value so they can leverage it if needed after they check
@@ -2984,13 +2990,15 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
2984
2990
  let location = result.headers.get("Location");
2985
2991
  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
2986
2992
 
2987
- let external = createURL(location).origin !== createURL("/").origin; // Support relative routing in internal redirects
2993
+ let currentUrl = new URL(request.url);
2994
+ let currentOrigin = currentUrl.origin;
2995
+ let newOrigin = new URL(location, currentOrigin).origin;
2996
+ let external = newOrigin !== currentOrigin; // Support relative routing in internal redirects
2988
2997
 
2989
2998
  if (!external) {
2990
2999
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
2991
3000
  let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
2992
- let requestPath = createURL(request.url).pathname;
2993
- let resolvedLocation = resolveTo(location, routePathnames, requestPath);
3001
+ let resolvedLocation = resolveTo(location, routePathnames, currentUrl.pathname);
2994
3002
  invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
2995
3003
 
2996
3004
  if (basename) {
@@ -3073,10 +3081,13 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
3073
3081
  type: ResultType.data,
3074
3082
  data: result
3075
3083
  };
3076
- }
3084
+ } // Utility method for creating the Request instances for loaders/actions during
3085
+ // client-side navigations and fetches. During SSR we will always have a
3086
+ // Request instance from the static handler (query/queryRoute)
3087
+
3077
3088
 
3078
- function createRequest(location, signal, submission) {
3079
- let url = createURL(stripHashFromPath(location)).toString();
3089
+ function createClientSideRequest(location, signal, submission) {
3090
+ let url = createClientSideURL(stripHashFromPath(location)).toString();
3080
3091
  let init = {
3081
3092
  signal
3082
3093
  };