@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/utils.d.ts CHANGED
@@ -35,7 +35,6 @@ export interface RedirectResult {
35
35
  status: number;
36
36
  location: string;
37
37
  revalidate: boolean;
38
- external: boolean;
39
38
  }
40
39
  /**
41
40
  * Unsuccessful result from a loader or action
@@ -71,6 +70,7 @@ export interface Submission {
71
70
  interface DataFunctionArgs {
72
71
  request: Request;
73
72
  params: Params;
73
+ context?: any;
74
74
  }
75
75
  /**
76
76
  * Arguments passed to loader functions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix-run/router",
3
- "version": "1.0.5-pre.0",
3
+ "version": "1.0.5-pre.2",
4
4
  "description": "Nested/Data-driven/Framework-agnostic Routing",
5
5
  "keywords": [
6
6
  "remix",
package/router.ts CHANGED
@@ -316,8 +316,14 @@ export interface StaticHandlerContext {
316
316
  */
317
317
  export interface StaticHandler {
318
318
  dataRoutes: AgnosticDataRouteObject[];
319
- query(request: Request): Promise<StaticHandlerContext | Response>;
320
- queryRoute(request: Request, routeId?: string): Promise<any>;
319
+ query(
320
+ request: Request,
321
+ opts?: { requestContext?: unknown }
322
+ ): Promise<StaticHandlerContext | Response>;
323
+ queryRoute(
324
+ request: Request,
325
+ opts?: { routeId?: string; requestContext?: unknown }
326
+ ): Promise<any>;
321
327
  }
322
328
 
323
329
  /**
@@ -1606,17 +1612,17 @@ export function createRouter(init: RouterInit): Router {
1606
1612
  "Expected a location on the redirect navigation"
1607
1613
  );
1608
1614
 
1609
- if (
1610
- redirect.external &&
1611
- typeof window !== "undefined" &&
1612
- typeof window.location !== "undefined"
1613
- ) {
1614
- if (replace) {
1615
- window.location.replace(redirect.location);
1616
- } else {
1617
- window.location.assign(redirect.location);
1615
+ // Check if this an external redirect that goes to a new origin
1616
+ if (typeof window?.location !== "undefined") {
1617
+ let newOrigin = createClientSideURL(redirect.location).origin;
1618
+ if (window.location.origin !== newOrigin) {
1619
+ if (replace) {
1620
+ window.location.replace(redirect.location);
1621
+ } else {
1622
+ window.location.assign(redirect.location);
1623
+ }
1624
+ return;
1618
1625
  }
1619
- return;
1620
1626
  }
1621
1627
 
1622
1628
  // There's no need to abort on redirects, since we don't detect the
@@ -1943,7 +1949,8 @@ export function unstable_createStaticHandler(
1943
1949
  * return it directly.
1944
1950
  */
1945
1951
  async function query(
1946
- request: Request
1952
+ request: Request,
1953
+ { requestContext }: { requestContext?: unknown } = {}
1947
1954
  ): Promise<StaticHandlerContext | Response> {
1948
1955
  let url = new URL(request.url);
1949
1956
  let method = request.method.toLowerCase();
@@ -1987,8 +1994,8 @@ export function unstable_createStaticHandler(
1987
1994
  };
1988
1995
  }
1989
1996
 
1990
- let result = await queryImpl(request, location, matches);
1991
- if (result instanceof Response) {
1997
+ let result = await queryImpl(request, location, matches, requestContext);
1998
+ if (isResponse(result)) {
1992
1999
  return result;
1993
2000
  }
1994
2001
 
@@ -2018,7 +2025,13 @@ export function unstable_createStaticHandler(
2018
2025
  * code. Examples here are 404 and 405 errors that occur prior to reaching
2019
2026
  * any user-defined loaders.
2020
2027
  */
2021
- async function queryRoute(request: Request, routeId?: string): Promise<any> {
2028
+ async function queryRoute(
2029
+ request: Request,
2030
+ {
2031
+ routeId,
2032
+ requestContext,
2033
+ }: { requestContext?: unknown; routeId?: string } = {}
2034
+ ): Promise<any> {
2022
2035
  let url = new URL(request.url);
2023
2036
  let method = request.method.toLowerCase();
2024
2037
  let location = createLocation("", createPath(url), null, "default");
@@ -2045,8 +2058,14 @@ export function unstable_createStaticHandler(
2045
2058
  throw getInternalRouterError(404, { pathname: location.pathname });
2046
2059
  }
2047
2060
 
2048
- let result = await queryImpl(request, location, matches, match);
2049
- if (result instanceof Response) {
2061
+ let result = await queryImpl(
2062
+ request,
2063
+ location,
2064
+ matches,
2065
+ requestContext,
2066
+ match
2067
+ );
2068
+ if (isResponse(result)) {
2050
2069
  return result;
2051
2070
  }
2052
2071
 
@@ -2068,6 +2087,7 @@ export function unstable_createStaticHandler(
2068
2087
  request: Request,
2069
2088
  location: Location,
2070
2089
  matches: AgnosticDataRouteMatch[],
2090
+ requestContext: unknown,
2071
2091
  routeMatch?: AgnosticDataRouteMatch
2072
2092
  ): Promise<Omit<StaticHandlerContext, "location" | "basename"> | Response> {
2073
2093
  invariant(
@@ -2081,13 +2101,19 @@ export function unstable_createStaticHandler(
2081
2101
  request,
2082
2102
  matches,
2083
2103
  routeMatch || getTargetMatch(matches, location),
2104
+ requestContext,
2084
2105
  routeMatch != null
2085
2106
  );
2086
2107
  return result;
2087
2108
  }
2088
2109
 
2089
- let result = await loadRouteData(request, matches, routeMatch);
2090
- return result instanceof Response
2110
+ let result = await loadRouteData(
2111
+ request,
2112
+ matches,
2113
+ requestContext,
2114
+ routeMatch
2115
+ );
2116
+ return isResponse(result)
2091
2117
  ? result
2092
2118
  : {
2093
2119
  ...result,
@@ -2117,6 +2143,7 @@ export function unstable_createStaticHandler(
2117
2143
  request: Request,
2118
2144
  matches: AgnosticDataRouteMatch[],
2119
2145
  actionMatch: AgnosticDataRouteMatch,
2146
+ requestContext: unknown,
2120
2147
  isRouteRequest: boolean
2121
2148
  ): Promise<Omit<StaticHandlerContext, "location" | "basename"> | Response> {
2122
2149
  let result: DataResult;
@@ -2142,7 +2169,8 @@ export function unstable_createStaticHandler(
2142
2169
  matches,
2143
2170
  basename,
2144
2171
  true,
2145
- isRouteRequest
2172
+ isRouteRequest,
2173
+ requestContext
2146
2174
  );
2147
2175
 
2148
2176
  if (request.signal.aborted) {
@@ -2192,9 +2220,15 @@ export function unstable_createStaticHandler(
2192
2220
  // Store off the pending error - we use it to determine which loaders
2193
2221
  // to call and will commit it when we complete the navigation
2194
2222
  let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
2195
- let context = await loadRouteData(request, matches, undefined, {
2196
- [boundaryMatch.route.id]: result.error,
2197
- });
2223
+ let context = await loadRouteData(
2224
+ request,
2225
+ matches,
2226
+ requestContext,
2227
+ undefined,
2228
+ {
2229
+ [boundaryMatch.route.id]: result.error,
2230
+ }
2231
+ );
2198
2232
 
2199
2233
  // action status codes take precedence over loader status codes
2200
2234
  return {
@@ -2211,7 +2245,7 @@ export function unstable_createStaticHandler(
2211
2245
 
2212
2246
  // Create a GET request for the loaders
2213
2247
  let loaderRequest = new Request(request.url, { signal: request.signal });
2214
- let context = await loadRouteData(loaderRequest, matches);
2248
+ let context = await loadRouteData(loaderRequest, matches, requestContext);
2215
2249
 
2216
2250
  return {
2217
2251
  ...context,
@@ -2229,6 +2263,7 @@ export function unstable_createStaticHandler(
2229
2263
  async function loadRouteData(
2230
2264
  request: Request,
2231
2265
  matches: AgnosticDataRouteMatch[],
2266
+ requestContext: unknown,
2232
2267
  routeMatch?: AgnosticDataRouteMatch,
2233
2268
  pendingActionError?: RouteData
2234
2269
  ): Promise<
@@ -2277,7 +2312,8 @@ export function unstable_createStaticHandler(
2277
2312
  matches,
2278
2313
  basename,
2279
2314
  true,
2280
- isRouteRequest
2315
+ isRouteRequest,
2316
+ requestContext
2281
2317
  )
2282
2318
  ),
2283
2319
  ]);
@@ -2580,7 +2616,8 @@ async function callLoaderOrAction(
2580
2616
  matches: AgnosticDataRouteMatch[],
2581
2617
  basename = "/",
2582
2618
  isStaticRequest: boolean = false,
2583
- isRouteRequest: boolean = false
2619
+ isRouteRequest: boolean = false,
2620
+ requestContext?: unknown
2584
2621
  ): Promise<DataResult> {
2585
2622
  let resultType;
2586
2623
  let result;
@@ -2599,7 +2636,7 @@ async function callLoaderOrAction(
2599
2636
  );
2600
2637
 
2601
2638
  result = await Promise.race([
2602
- handler({ request, params: match.params }),
2639
+ handler({ request, params: match.params, context: requestContext }),
2603
2640
  abortPromise,
2604
2641
  ]);
2605
2642
 
@@ -2616,7 +2653,7 @@ async function callLoaderOrAction(
2616
2653
  request.signal.removeEventListener("abort", onReject);
2617
2654
  }
2618
2655
 
2619
- if (result instanceof Response) {
2656
+ if (isResponse(result)) {
2620
2657
  let status = result.status;
2621
2658
 
2622
2659
  // Process redirects
@@ -2627,14 +2664,11 @@ async function callLoaderOrAction(
2627
2664
  "Redirects returned/thrown from loaders/actions must have a Location header"
2628
2665
  );
2629
2666
 
2630
- // Check if this an external redirect that goes to a new origin
2631
- let currentUrl = new URL(request.url);
2632
- let currentOrigin = currentUrl.origin;
2633
- let newOrigin = new URL(location, currentOrigin).origin;
2634
- let external = newOrigin !== currentOrigin;
2667
+ let isAbsolute =
2668
+ /^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
2635
2669
 
2636
2670
  // Support relative routing in internal redirects
2637
- if (!external) {
2671
+ if (!isAbsolute) {
2638
2672
  let activeMatches = matches.slice(0, matches.indexOf(match) + 1);
2639
2673
  let routePathnames = getPathContributingMatches(activeMatches).map(
2640
2674
  (match) => match.pathnameBase
@@ -2642,7 +2676,7 @@ async function callLoaderOrAction(
2642
2676
  let resolvedLocation = resolveTo(
2643
2677
  location,
2644
2678
  routePathnames,
2645
- currentUrl.pathname
2679
+ new URL(request.url).pathname
2646
2680
  );
2647
2681
  invariant(
2648
2682
  createPath(resolvedLocation),
@@ -2673,7 +2707,6 @@ async function callLoaderOrAction(
2673
2707
  status,
2674
2708
  location,
2675
2709
  revalidate: result.headers.get("X-Remix-Revalidate") !== null,
2676
- external,
2677
2710
  };
2678
2711
  }
2679
2712
 
@@ -2972,12 +3005,10 @@ function getInternalRouterError(
2972
3005
  pathname,
2973
3006
  routeId,
2974
3007
  method,
2975
- message,
2976
3008
  }: {
2977
3009
  pathname?: string;
2978
3010
  routeId?: string;
2979
3011
  method?: string;
2980
- message?: string;
2981
3012
  } = {}
2982
3013
  ) {
2983
3014
  let statusText = "Unknown Server Error";
@@ -3052,8 +3083,18 @@ function isRedirectResult(result?: DataResult): result is RedirectResult {
3052
3083
  return (result && result.type) === ResultType.redirect;
3053
3084
  }
3054
3085
 
3086
+ function isResponse(value: any): value is Response {
3087
+ return (
3088
+ value != null &&
3089
+ typeof value.status === "number" &&
3090
+ typeof value.statusText === "string" &&
3091
+ typeof value.headers === "object" &&
3092
+ typeof value.body !== "undefined"
3093
+ );
3094
+ }
3095
+
3055
3096
  function isRedirectResponse(result: any): result is Response {
3056
- if (!(result instanceof Response)) {
3097
+ if (!isResponse(result)) {
3057
3098
  return false;
3058
3099
  }
3059
3100
 
@@ -3065,7 +3106,7 @@ function isRedirectResponse(result: any): result is Response {
3065
3106
  function isQueryRouteResponse(obj: any): obj is QueryRouteResponse {
3066
3107
  return (
3067
3108
  obj &&
3068
- obj.response instanceof Response &&
3109
+ isResponse(obj.response) &&
3069
3110
  (obj.type === ResultType.data || ResultType.error)
3070
3111
  );
3071
3112
  }
package/utils.ts CHANGED
@@ -41,7 +41,6 @@ export interface RedirectResult {
41
41
  status: number;
42
42
  location: string;
43
43
  revalidate: boolean;
44
- external: boolean;
45
44
  }
46
45
 
47
46
  /**
@@ -89,6 +88,7 @@ export interface Submission {
89
88
  interface DataFunctionArgs {
90
89
  request: Request;
91
90
  params: Params;
91
+ context?: any;
92
92
  }
93
93
 
94
94
  /**