@remix-run/router 1.0.5-pre.0 → 1.0.5-pre.2

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.5-pre.0
2
+ * @remix-run/router v1.0.5-pre.2
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -2195,21 +2195,27 @@
2195
2195
 
2196
2196
 
2197
2197
  async function startRedirectNavigation(state, redirect, replace) {
2198
+ var _window;
2199
+
2198
2200
  if (redirect.revalidate) {
2199
2201
  isRevalidationRequired = true;
2200
2202
  }
2201
2203
 
2202
2204
  let redirectLocation = createLocation(state.location, redirect.location);
2203
- 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
2204
2206
 
2205
- if (redirect.external && typeof window !== "undefined" && typeof window.location !== "undefined") {
2206
- if (replace) {
2207
- window.location.replace(redirect.location);
2208
- } else {
2209
- window.location.assign(redirect.location);
2210
- }
2207
+ if (typeof ((_window = window) == null ? void 0 : _window.location) !== "undefined") {
2208
+ let newOrigin = createClientSideURL(redirect.location).origin;
2211
2209
 
2212
- 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
+ }
2213
2219
  } // There's no need to abort on redirects, since we don't detect the
2214
2220
  // redirect until the action/loaders have settled
2215
2221
 
@@ -2488,7 +2494,10 @@
2488
2494
  * return it directly.
2489
2495
  */
2490
2496
 
2491
- async function query(request) {
2497
+ async function query(request, _temp) {
2498
+ let {
2499
+ requestContext
2500
+ } = _temp === void 0 ? {} : _temp;
2492
2501
  let url = new URL(request.url);
2493
2502
  let method = request.method.toLowerCase();
2494
2503
  let location = createLocation("", createPath(url), null, "default");
@@ -2538,9 +2547,9 @@
2538
2547
  };
2539
2548
  }
2540
2549
 
2541
- let result = await queryImpl(request, location, matches);
2550
+ let result = await queryImpl(request, location, matches, requestContext);
2542
2551
 
2543
- if (result instanceof Response) {
2552
+ if (isResponse(result)) {
2544
2553
  return result;
2545
2554
  } // When returning StaticHandlerContext, we patch back in the location here
2546
2555
  // since we need it for React Context. But this helps keep our submit and
@@ -2574,7 +2583,11 @@
2574
2583
  */
2575
2584
 
2576
2585
 
2577
- async function queryRoute(request, routeId) {
2586
+ async function queryRoute(request, _temp2) {
2587
+ let {
2588
+ routeId,
2589
+ requestContext
2590
+ } = _temp2 === void 0 ? {} : _temp2;
2578
2591
  let url = new URL(request.url);
2579
2592
  let method = request.method.toLowerCase();
2580
2593
  let location = createLocation("", createPath(url), null, "default");
@@ -2604,9 +2617,9 @@
2604
2617
  });
2605
2618
  }
2606
2619
 
2607
- let result = await queryImpl(request, location, matches, match);
2620
+ let result = await queryImpl(request, location, matches, requestContext, match);
2608
2621
 
2609
- if (result instanceof Response) {
2622
+ if (isResponse(result)) {
2610
2623
  return result;
2611
2624
  }
2612
2625
 
@@ -2625,17 +2638,17 @@
2625
2638
  return Object.values(routeData || {})[0];
2626
2639
  }
2627
2640
 
2628
- async function queryImpl(request, location, matches, routeMatch) {
2641
+ async function queryImpl(request, location, matches, requestContext, routeMatch) {
2629
2642
  invariant(request.signal, "query()/queryRoute() requests must contain an AbortController signal");
2630
2643
 
2631
2644
  try {
2632
2645
  if (isSubmissionMethod(request.method.toLowerCase())) {
2633
- let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), routeMatch != null);
2646
+ let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), requestContext, routeMatch != null);
2634
2647
  return result;
2635
2648
  }
2636
2649
 
2637
- let result = await loadRouteData(request, matches, routeMatch);
2638
- return result instanceof Response ? result : _extends({}, result, {
2650
+ let result = await loadRouteData(request, matches, requestContext, routeMatch);
2651
+ return isResponse(result) ? result : _extends({}, result, {
2639
2652
  actionData: null,
2640
2653
  actionHeaders: {}
2641
2654
  });
@@ -2661,7 +2674,7 @@
2661
2674
  }
2662
2675
  }
2663
2676
 
2664
- async function submit(request, matches, actionMatch, isRouteRequest) {
2677
+ async function submit(request, matches, actionMatch, requestContext, isRouteRequest) {
2665
2678
  let result;
2666
2679
 
2667
2680
  if (!actionMatch.route.action) {
@@ -2680,7 +2693,7 @@
2680
2693
  error
2681
2694
  };
2682
2695
  } else {
2683
- result = await callLoaderOrAction("action", request, actionMatch, matches, basename, true, isRouteRequest);
2696
+ result = await callLoaderOrAction("action", request, actionMatch, matches, basename, true, isRouteRequest, requestContext);
2684
2697
 
2685
2698
  if (request.signal.aborted) {
2686
2699
  let method = isRouteRequest ? "queryRoute" : "query";
@@ -2731,7 +2744,7 @@
2731
2744
  // Store off the pending error - we use it to determine which loaders
2732
2745
  // to call and will commit it when we complete the navigation
2733
2746
  let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
2734
- let context = await loadRouteData(request, matches, undefined, {
2747
+ let context = await loadRouteData(request, matches, requestContext, undefined, {
2735
2748
  [boundaryMatch.route.id]: result.error
2736
2749
  }); // action status codes take precedence over loader status codes
2737
2750
 
@@ -2748,7 +2761,7 @@
2748
2761
  let loaderRequest = new Request(request.url, {
2749
2762
  signal: request.signal
2750
2763
  });
2751
- let context = await loadRouteData(loaderRequest, matches);
2764
+ let context = await loadRouteData(loaderRequest, matches, requestContext);
2752
2765
  return _extends({}, context, result.statusCode ? {
2753
2766
  statusCode: result.statusCode
2754
2767
  } : {}, {
@@ -2761,7 +2774,7 @@
2761
2774
  });
2762
2775
  }
2763
2776
 
2764
- async function loadRouteData(request, matches, routeMatch, pendingActionError) {
2777
+ async function loadRouteData(request, matches, requestContext, routeMatch, pendingActionError) {
2765
2778
  let isRouteRequest = routeMatch != null; // Short circuit if we have no loaders to run (queryRoute())
2766
2779
 
2767
2780
  if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
@@ -2785,7 +2798,7 @@
2785
2798
  };
2786
2799
  }
2787
2800
 
2788
- let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, basename, true, isRouteRequest))]);
2801
+ let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, basename, true, isRouteRequest, requestContext))]);
2789
2802
 
2790
2803
  if (request.signal.aborted) {
2791
2804
  let method = isRouteRequest ? "queryRoute" : "query";
@@ -2995,7 +3008,7 @@
2995
3008
  return defaultShouldRevalidate;
2996
3009
  }
2997
3010
 
2998
- async function callLoaderOrAction(type, request, match, matches, basename, isStaticRequest, isRouteRequest) {
3011
+ async function callLoaderOrAction(type, request, match, matches, basename, isStaticRequest, isRouteRequest, requestContext) {
2999
3012
  if (basename === void 0) {
3000
3013
  basename = "/";
3001
3014
  }
@@ -3023,7 +3036,8 @@
3023
3036
  invariant(handler, "Could not find the " + type + " to run on the \"" + match.route.id + "\" route");
3024
3037
  result = await Promise.race([handler({
3025
3038
  request,
3026
- params: match.params
3039
+ params: match.params,
3040
+ context: requestContext
3027
3041
  }), abortPromise]);
3028
3042
  invariant(result !== undefined, "You defined " + (type === "action" ? "an action" : "a loader") + " for route " + ("\"" + match.route.id + "\" but didn't return anything from your `" + type + "` ") + "function. Please return a value or `null`.");
3029
3043
  } catch (e) {
@@ -3033,22 +3047,18 @@
3033
3047
  request.signal.removeEventListener("abort", onReject);
3034
3048
  }
3035
3049
 
3036
- if (result instanceof Response) {
3050
+ if (isResponse(result)) {
3037
3051
  let status = result.status; // Process redirects
3038
3052
 
3039
3053
  if (redirectStatusCodes.has(status)) {
3040
3054
  let location = result.headers.get("Location");
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
3055
+ invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header");
3056
+ let isAbsolute = /^[a-z+]+:\/\//i.test(location) || location.startsWith("//"); // Support relative routing in internal redirects
3042
3057
 
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
3047
-
3048
- if (!external) {
3058
+ if (!isAbsolute) {
3049
3059
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
3050
3060
  let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
3051
- let resolvedLocation = resolveTo(location, routePathnames, currentUrl.pathname);
3061
+ let resolvedLocation = resolveTo(location, routePathnames, new URL(request.url).pathname);
3052
3062
  invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
3053
3063
 
3054
3064
  if (basename) {
@@ -3072,8 +3082,7 @@
3072
3082
  type: ResultType.redirect,
3073
3083
  status,
3074
3084
  location,
3075
- revalidate: result.headers.get("X-Remix-Revalidate") !== null,
3076
- external
3085
+ revalidate: result.headers.get("X-Remix-Revalidate") !== null
3077
3086
  };
3078
3087
  } // For SSR single-route requests, we want to hand Responses back directly
3079
3088
  // without unwrapping. We do this with the QueryRouteResponse wrapper
@@ -3320,13 +3329,12 @@
3320
3329
  };
3321
3330
  }
3322
3331
 
3323
- function getInternalRouterError(status, _temp) {
3332
+ function getInternalRouterError(status, _temp3) {
3324
3333
  let {
3325
3334
  pathname,
3326
3335
  routeId,
3327
- method,
3328
- message
3329
- } = _temp === void 0 ? {} : _temp;
3336
+ method
3337
+ } = _temp3 === void 0 ? {} : _temp3;
3330
3338
  let statusText = "Unknown Server Error";
3331
3339
  let errorMessage = "Unknown @remix-run/router error";
3332
3340
 
@@ -3391,8 +3399,12 @@
3391
3399
  return (result && result.type) === ResultType.redirect;
3392
3400
  }
3393
3401
 
3402
+ function isResponse(value) {
3403
+ return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
3404
+ }
3405
+
3394
3406
  function isRedirectResponse(result) {
3395
- if (!(result instanceof Response)) {
3407
+ if (!isResponse(result)) {
3396
3408
  return false;
3397
3409
  }
3398
3410
 
@@ -3402,7 +3414,7 @@
3402
3414
  }
3403
3415
 
3404
3416
  function isQueryRouteResponse(obj) {
3405
- return obj && obj.response instanceof Response && (obj.type === ResultType.data || ResultType.error);
3417
+ return obj && isResponse(obj.response) && (obj.type === ResultType.data || ResultType.error);
3406
3418
  }
3407
3419
 
3408
3420
  function isValidMethod(method) {