@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.
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.1
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
@@ -2143,21 +2145,27 @@ function createRouter(init) {
2143
2145
 
2144
2146
 
2145
2147
  async function startRedirectNavigation(state, redirect, replace) {
2148
+ var _window;
2149
+
2146
2150
  if (redirect.revalidate) {
2147
2151
  isRevalidationRequired = true;
2148
2152
  }
2149
2153
 
2150
2154
  let redirectLocation = createLocation(state.location, redirect.location);
2151
- invariant(redirectLocation, "Expected a location on the redirect navigation");
2155
+ invariant(redirectLocation, "Expected a location on the redirect navigation"); // Check if this an external redirect that goes to a new origin
2152
2156
 
2153
- if (redirect.external && typeof window !== "undefined" && typeof window.location !== "undefined") {
2154
- if (replace) {
2155
- window.location.replace(redirect.location);
2156
- } else {
2157
- window.location.assign(redirect.location);
2158
- }
2157
+ if (typeof ((_window = window) == null ? void 0 : _window.location) !== "undefined") {
2158
+ let newOrigin = createClientSideURL(redirect.location).origin;
2159
2159
 
2160
- return;
2160
+ if (window.location.origin !== newOrigin) {
2161
+ if (replace) {
2162
+ window.location.replace(redirect.location);
2163
+ } else {
2164
+ window.location.assign(redirect.location);
2165
+ }
2166
+
2167
+ return;
2168
+ }
2161
2169
  } // There's no need to abort on redirects, since we don't detect the
2162
2170
  // redirect until the action/loaders have settled
2163
2171
 
@@ -2204,7 +2212,7 @@ function createRouter(init) {
2204
2212
  // accordingly
2205
2213
  let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, router.basename)), ...fetchersToLoad.map(_ref8 => {
2206
2214
  let [, href, match, fetchMatches] = _ref8;
2207
- return callLoaderOrAction("loader", createRequest(href, request.signal), match, fetchMatches, router.basename);
2215
+ return callLoaderOrAction("loader", createClientSideRequest(href, request.signal), match, fetchMatches, router.basename);
2208
2216
  })]);
2209
2217
  let loaderResults = results.slice(0, matchesToLoad.length);
2210
2218
  let fetcherResults = results.slice(matchesToLoad.length);
@@ -2488,7 +2496,7 @@ function unstable_createStaticHandler(routes, opts) {
2488
2496
 
2489
2497
  let result = await queryImpl(request, location, matches);
2490
2498
 
2491
- if (result instanceof Response) {
2499
+ if (isResponse(result)) {
2492
2500
  return result;
2493
2501
  } // When returning StaticHandlerContext, we patch back in the location here
2494
2502
  // since we need it for React Context. But this helps keep our submit and
@@ -2554,7 +2562,7 @@ function unstable_createStaticHandler(routes, opts) {
2554
2562
 
2555
2563
  let result = await queryImpl(request, location, matches, match);
2556
2564
 
2557
- if (result instanceof Response) {
2565
+ if (isResponse(result)) {
2558
2566
  return result;
2559
2567
  }
2560
2568
 
@@ -2583,7 +2591,7 @@ function unstable_createStaticHandler(routes, opts) {
2583
2591
  }
2584
2592
 
2585
2593
  let result = await loadRouteData(request, matches, routeMatch);
2586
- return result instanceof Response ? result : _extends({}, result, {
2594
+ return isResponse(result) ? result : _extends({}, result, {
2587
2595
  actionData: null,
2588
2596
  actionHeaders: {}
2589
2597
  });
@@ -2615,7 +2623,7 @@ function unstable_createStaticHandler(routes, opts) {
2615
2623
  if (!actionMatch.route.action) {
2616
2624
  let error = getInternalRouterError(405, {
2617
2625
  method: request.method,
2618
- pathname: createURL(request.url).pathname,
2626
+ pathname: new URL(request.url).pathname,
2619
2627
  routeId: actionMatch.route.id
2620
2628
  });
2621
2629
 
@@ -2690,9 +2698,13 @@ function unstable_createStaticHandler(routes, opts) {
2690
2698
  [actionMatch.route.id]: result.headers
2691
2699
  } : {})
2692
2700
  });
2693
- }
2701
+ } // Create a GET request for the loaders
2702
+
2694
2703
 
2695
- let context = await loadRouteData(request, matches);
2704
+ let loaderRequest = new Request(request.url, {
2705
+ signal: request.signal
2706
+ });
2707
+ let context = await loadRouteData(loaderRequest, matches);
2696
2708
  return _extends({}, context, result.statusCode ? {
2697
2709
  statusCode: result.statusCode
2698
2710
  } : {}, {
@@ -2711,7 +2723,7 @@ function unstable_createStaticHandler(routes, opts) {
2711
2723
  if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
2712
2724
  throw getInternalRouterError(400, {
2713
2725
  method: request.method,
2714
- pathname: createURL(request.url).pathname,
2726
+ pathname: new URL(request.url).pathname,
2715
2727
  routeId: routeMatch == null ? void 0 : routeMatch.route.id
2716
2728
  });
2717
2729
  }
@@ -2905,9 +2917,9 @@ function isNewRouteInstance(currentMatch, match) {
2905
2917
  }
2906
2918
 
2907
2919
  function shouldRevalidateLoader(currentLocation, currentMatch, submission, location, match, isRevalidationRequired, actionResult) {
2908
- let currentUrl = createURL(currentLocation);
2920
+ let currentUrl = createClientSideURL(currentLocation);
2909
2921
  let currentParams = currentMatch.params;
2910
- let nextUrl = createURL(location);
2922
+ let nextUrl = createClientSideURL(location);
2911
2923
  let nextParams = match.params; // This is the default implementation as to when we revalidate. If the route
2912
2924
  // provides it's own implementation, then we give them full control but
2913
2925
  // provide this value so they can leverage it if needed after they check
@@ -2977,20 +2989,18 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
2977
2989
  request.signal.removeEventListener("abort", onReject);
2978
2990
  }
2979
2991
 
2980
- if (result instanceof Response) {
2992
+ if (isResponse(result)) {
2981
2993
  let status = result.status; // Process redirects
2982
2994
 
2983
2995
  if (redirectStatusCodes.has(status)) {
2984
2996
  let location = result.headers.get("Location");
2985
- 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
-
2987
- let external = createURL(location).origin !== createURL("/").origin; // Support relative routing in internal redirects
2997
+ invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header");
2998
+ let isAbsolute = /^[a-z+]+:\/\//i.test(location) || location.startsWith("//"); // Support relative routing in internal redirects
2988
2999
 
2989
- if (!external) {
3000
+ if (!isAbsolute) {
2990
3001
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
2991
3002
  let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
2992
- let requestPath = createURL(request.url).pathname;
2993
- let resolvedLocation = resolveTo(location, routePathnames, requestPath);
3003
+ let resolvedLocation = resolveTo(location, routePathnames, new URL(request.url).pathname);
2994
3004
  invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
2995
3005
 
2996
3006
  if (basename) {
@@ -3014,8 +3024,7 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
3014
3024
  type: ResultType.redirect,
3015
3025
  status,
3016
3026
  location,
3017
- revalidate: result.headers.get("X-Remix-Revalidate") !== null,
3018
- external
3027
+ revalidate: result.headers.get("X-Remix-Revalidate") !== null
3019
3028
  };
3020
3029
  } // For SSR single-route requests, we want to hand Responses back directly
3021
3030
  // without unwrapping. We do this with the QueryRouteResponse wrapper
@@ -3073,10 +3082,13 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
3073
3082
  type: ResultType.data,
3074
3083
  data: result
3075
3084
  };
3076
- }
3085
+ } // Utility method for creating the Request instances for loaders/actions during
3086
+ // client-side navigations and fetches. During SSR we will always have a
3087
+ // Request instance from the static handler (query/queryRoute)
3088
+
3077
3089
 
3078
- function createRequest(location, signal, submission) {
3079
- let url = createURL(stripHashFromPath(location)).toString();
3090
+ function createClientSideRequest(location, signal, submission) {
3091
+ let url = createClientSideURL(stripHashFromPath(location)).toString();
3080
3092
  let init = {
3081
3093
  signal
3082
3094
  };
@@ -3330,8 +3342,12 @@ function isRedirectResult(result) {
3330
3342
  return (result && result.type) === ResultType.redirect;
3331
3343
  }
3332
3344
 
3345
+ function isResponse(value) {
3346
+ return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
3347
+ }
3348
+
3333
3349
  function isRedirectResponse(result) {
3334
- if (!(result instanceof Response)) {
3350
+ if (!isResponse(result)) {
3335
3351
  return false;
3336
3352
  }
3337
3353
 
@@ -3341,7 +3357,7 @@ function isRedirectResponse(result) {
3341
3357
  }
3342
3358
 
3343
3359
  function isQueryRouteResponse(obj) {
3344
- return obj && obj.response instanceof Response && (obj.type === ResultType.data || ResultType.error);
3360
+ return obj && isResponse(obj.response) && (obj.type === ResultType.data || ResultType.error);
3345
3361
  }
3346
3362
 
3347
3363
  function isValidMethod(method) {