@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.
package/dist/router.d.ts CHANGED
@@ -236,8 +236,13 @@ export interface StaticHandlerContext {
236
236
  */
237
237
  export interface StaticHandler {
238
238
  dataRoutes: AgnosticDataRouteObject[];
239
- query(request: Request): Promise<StaticHandlerContext | Response>;
240
- queryRoute(request: Request, routeId?: string): Promise<any>;
239
+ query(request: Request, opts?: {
240
+ requestContext?: unknown;
241
+ }): Promise<StaticHandlerContext | Response>;
242
+ queryRoute(request: Request, opts?: {
243
+ routeId?: string;
244
+ requestContext?: unknown;
245
+ }): Promise<any>;
241
246
  }
242
247
  /**
243
248
  * Subscriber function signature for changes to router state
package/dist/router.js CHANGED
@@ -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
  *
@@ -2145,21 +2145,27 @@ function createRouter(init) {
2145
2145
 
2146
2146
 
2147
2147
  async function startRedirectNavigation(state, redirect, replace) {
2148
+ var _window;
2149
+
2148
2150
  if (redirect.revalidate) {
2149
2151
  isRevalidationRequired = true;
2150
2152
  }
2151
2153
 
2152
2154
  let redirectLocation = createLocation(state.location, redirect.location);
2153
- 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
2154
2156
 
2155
- if (redirect.external && typeof window !== "undefined" && typeof window.location !== "undefined") {
2156
- if (replace) {
2157
- window.location.replace(redirect.location);
2158
- } else {
2159
- window.location.assign(redirect.location);
2160
- }
2157
+ if (typeof ((_window = window) == null ? void 0 : _window.location) !== "undefined") {
2158
+ let newOrigin = createClientSideURL(redirect.location).origin;
2161
2159
 
2162
- 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
+ }
2163
2169
  } // There's no need to abort on redirects, since we don't detect the
2164
2170
  // redirect until the action/loaders have settled
2165
2171
 
@@ -2438,7 +2444,10 @@ function unstable_createStaticHandler(routes, opts) {
2438
2444
  * return it directly.
2439
2445
  */
2440
2446
 
2441
- async function query(request) {
2447
+ async function query(request, _temp) {
2448
+ let {
2449
+ requestContext
2450
+ } = _temp === void 0 ? {} : _temp;
2442
2451
  let url = new URL(request.url);
2443
2452
  let method = request.method.toLowerCase();
2444
2453
  let location = createLocation("", createPath(url), null, "default");
@@ -2488,9 +2497,9 @@ function unstable_createStaticHandler(routes, opts) {
2488
2497
  };
2489
2498
  }
2490
2499
 
2491
- let result = await queryImpl(request, location, matches);
2500
+ let result = await queryImpl(request, location, matches, requestContext);
2492
2501
 
2493
- if (result instanceof Response) {
2502
+ if (isResponse(result)) {
2494
2503
  return result;
2495
2504
  } // When returning StaticHandlerContext, we patch back in the location here
2496
2505
  // since we need it for React Context. But this helps keep our submit and
@@ -2524,7 +2533,11 @@ function unstable_createStaticHandler(routes, opts) {
2524
2533
  */
2525
2534
 
2526
2535
 
2527
- async function queryRoute(request, routeId) {
2536
+ async function queryRoute(request, _temp2) {
2537
+ let {
2538
+ routeId,
2539
+ requestContext
2540
+ } = _temp2 === void 0 ? {} : _temp2;
2528
2541
  let url = new URL(request.url);
2529
2542
  let method = request.method.toLowerCase();
2530
2543
  let location = createLocation("", createPath(url), null, "default");
@@ -2554,9 +2567,9 @@ function unstable_createStaticHandler(routes, opts) {
2554
2567
  });
2555
2568
  }
2556
2569
 
2557
- let result = await queryImpl(request, location, matches, match);
2570
+ let result = await queryImpl(request, location, matches, requestContext, match);
2558
2571
 
2559
- if (result instanceof Response) {
2572
+ if (isResponse(result)) {
2560
2573
  return result;
2561
2574
  }
2562
2575
 
@@ -2575,17 +2588,17 @@ function unstable_createStaticHandler(routes, opts) {
2575
2588
  return Object.values(routeData || {})[0];
2576
2589
  }
2577
2590
 
2578
- async function queryImpl(request, location, matches, routeMatch) {
2591
+ async function queryImpl(request, location, matches, requestContext, routeMatch) {
2579
2592
  invariant(request.signal, "query()/queryRoute() requests must contain an AbortController signal");
2580
2593
 
2581
2594
  try {
2582
2595
  if (isSubmissionMethod(request.method.toLowerCase())) {
2583
- let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), routeMatch != null);
2596
+ let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), requestContext, routeMatch != null);
2584
2597
  return result;
2585
2598
  }
2586
2599
 
2587
- let result = await loadRouteData(request, matches, routeMatch);
2588
- return result instanceof Response ? result : _extends({}, result, {
2600
+ let result = await loadRouteData(request, matches, requestContext, routeMatch);
2601
+ return isResponse(result) ? result : _extends({}, result, {
2589
2602
  actionData: null,
2590
2603
  actionHeaders: {}
2591
2604
  });
@@ -2611,7 +2624,7 @@ function unstable_createStaticHandler(routes, opts) {
2611
2624
  }
2612
2625
  }
2613
2626
 
2614
- async function submit(request, matches, actionMatch, isRouteRequest) {
2627
+ async function submit(request, matches, actionMatch, requestContext, isRouteRequest) {
2615
2628
  let result;
2616
2629
 
2617
2630
  if (!actionMatch.route.action) {
@@ -2630,7 +2643,7 @@ function unstable_createStaticHandler(routes, opts) {
2630
2643
  error
2631
2644
  };
2632
2645
  } else {
2633
- result = await callLoaderOrAction("action", request, actionMatch, matches, basename, true, isRouteRequest);
2646
+ result = await callLoaderOrAction("action", request, actionMatch, matches, basename, true, isRouteRequest, requestContext);
2634
2647
 
2635
2648
  if (request.signal.aborted) {
2636
2649
  let method = isRouteRequest ? "queryRoute" : "query";
@@ -2681,7 +2694,7 @@ function unstable_createStaticHandler(routes, opts) {
2681
2694
  // Store off the pending error - we use it to determine which loaders
2682
2695
  // to call and will commit it when we complete the navigation
2683
2696
  let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
2684
- let context = await loadRouteData(request, matches, undefined, {
2697
+ let context = await loadRouteData(request, matches, requestContext, undefined, {
2685
2698
  [boundaryMatch.route.id]: result.error
2686
2699
  }); // action status codes take precedence over loader status codes
2687
2700
 
@@ -2698,7 +2711,7 @@ function unstable_createStaticHandler(routes, opts) {
2698
2711
  let loaderRequest = new Request(request.url, {
2699
2712
  signal: request.signal
2700
2713
  });
2701
- let context = await loadRouteData(loaderRequest, matches);
2714
+ let context = await loadRouteData(loaderRequest, matches, requestContext);
2702
2715
  return _extends({}, context, result.statusCode ? {
2703
2716
  statusCode: result.statusCode
2704
2717
  } : {}, {
@@ -2711,7 +2724,7 @@ function unstable_createStaticHandler(routes, opts) {
2711
2724
  });
2712
2725
  }
2713
2726
 
2714
- async function loadRouteData(request, matches, routeMatch, pendingActionError) {
2727
+ async function loadRouteData(request, matches, requestContext, routeMatch, pendingActionError) {
2715
2728
  let isRouteRequest = routeMatch != null; // Short circuit if we have no loaders to run (queryRoute())
2716
2729
 
2717
2730
  if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader)) {
@@ -2735,7 +2748,7 @@ function unstable_createStaticHandler(routes, opts) {
2735
2748
  };
2736
2749
  }
2737
2750
 
2738
- let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, basename, true, isRouteRequest))]);
2751
+ let results = await Promise.all([...matchesToLoad.map(match => callLoaderOrAction("loader", request, match, matches, basename, true, isRouteRequest, requestContext))]);
2739
2752
 
2740
2753
  if (request.signal.aborted) {
2741
2754
  let method = isRouteRequest ? "queryRoute" : "query";
@@ -2945,7 +2958,7 @@ function shouldRevalidateLoader(currentLocation, currentMatch, submission, locat
2945
2958
  return defaultShouldRevalidate;
2946
2959
  }
2947
2960
 
2948
- async function callLoaderOrAction(type, request, match, matches, basename, isStaticRequest, isRouteRequest) {
2961
+ async function callLoaderOrAction(type, request, match, matches, basename, isStaticRequest, isRouteRequest, requestContext) {
2949
2962
  if (basename === void 0) {
2950
2963
  basename = "/";
2951
2964
  }
@@ -2973,7 +2986,8 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
2973
2986
  invariant(handler, "Could not find the " + type + " to run on the \"" + match.route.id + "\" route");
2974
2987
  result = await Promise.race([handler({
2975
2988
  request,
2976
- params: match.params
2989
+ params: match.params,
2990
+ context: requestContext
2977
2991
  }), abortPromise]);
2978
2992
  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`.");
2979
2993
  } catch (e) {
@@ -2983,22 +2997,18 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
2983
2997
  request.signal.removeEventListener("abort", onReject);
2984
2998
  }
2985
2999
 
2986
- if (result instanceof Response) {
3000
+ if (isResponse(result)) {
2987
3001
  let status = result.status; // Process redirects
2988
3002
 
2989
3003
  if (redirectStatusCodes.has(status)) {
2990
3004
  let location = result.headers.get("Location");
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
3005
+ invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header");
3006
+ let isAbsolute = /^[a-z+]+:\/\//i.test(location) || location.startsWith("//"); // Support relative routing in internal redirects
2992
3007
 
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
2997
-
2998
- if (!external) {
3008
+ if (!isAbsolute) {
2999
3009
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
3000
3010
  let routePathnames = getPathContributingMatches(activeMatches).map(match => match.pathnameBase);
3001
- let resolvedLocation = resolveTo(location, routePathnames, currentUrl.pathname);
3011
+ let resolvedLocation = resolveTo(location, routePathnames, new URL(request.url).pathname);
3002
3012
  invariant(createPath(resolvedLocation), "Unable to resolve redirect location: " + location); // Prepend the basename to the redirect location if we have one
3003
3013
 
3004
3014
  if (basename) {
@@ -3022,8 +3032,7 @@ async function callLoaderOrAction(type, request, match, matches, basename, isSta
3022
3032
  type: ResultType.redirect,
3023
3033
  status,
3024
3034
  location,
3025
- revalidate: result.headers.get("X-Remix-Revalidate") !== null,
3026
- external
3035
+ revalidate: result.headers.get("X-Remix-Revalidate") !== null
3027
3036
  };
3028
3037
  } // For SSR single-route requests, we want to hand Responses back directly
3029
3038
  // without unwrapping. We do this with the QueryRouteResponse wrapper
@@ -3270,13 +3279,12 @@ function getShortCircuitMatches(routes) {
3270
3279
  };
3271
3280
  }
3272
3281
 
3273
- function getInternalRouterError(status, _temp) {
3282
+ function getInternalRouterError(status, _temp3) {
3274
3283
  let {
3275
3284
  pathname,
3276
3285
  routeId,
3277
- method,
3278
- message
3279
- } = _temp === void 0 ? {} : _temp;
3286
+ method
3287
+ } = _temp3 === void 0 ? {} : _temp3;
3280
3288
  let statusText = "Unknown Server Error";
3281
3289
  let errorMessage = "Unknown @remix-run/router error";
3282
3290
 
@@ -3341,8 +3349,12 @@ function isRedirectResult(result) {
3341
3349
  return (result && result.type) === ResultType.redirect;
3342
3350
  }
3343
3351
 
3352
+ function isResponse(value) {
3353
+ return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
3354
+ }
3355
+
3344
3356
  function isRedirectResponse(result) {
3345
- if (!(result instanceof Response)) {
3357
+ if (!isResponse(result)) {
3346
3358
  return false;
3347
3359
  }
3348
3360
 
@@ -3352,7 +3364,7 @@ function isRedirectResponse(result) {
3352
3364
  }
3353
3365
 
3354
3366
  function isQueryRouteResponse(obj) {
3355
- return obj && obj.response instanceof Response && (obj.type === ResultType.data || ResultType.error);
3367
+ return obj && isResponse(obj.response) && (obj.type === ResultType.data || ResultType.error);
3356
3368
  }
3357
3369
 
3358
3370
  function isValidMethod(method) {