|
3 | 3 | Action as HistoryAction, |
4 | 4 | createLocation, |
5 | 5 | createPath, |
6 | | - createURL, |
| 6 | + createClientSideURL, |
7 | 7 | parsePath, |
8 | 8 | } from "./history"; |
9 | 9 | import type { |
@@ -913,7 +913,7 @@ export function createRouter(init: RouterInit): Router { |
913 | 913 |
|
914 | 914 | // Create a controller/Request for this navigation |
915 | 915 | pendingNavigationController = new AbortController(); |
916 | | - let request = createRequest( |
| 916 | + let request = createClientSideRequest( |
917 | 917 | location, |
918 | 918 | pendingNavigationController.signal, |
919 | 919 | opts && opts.submission |
@@ -954,7 +954,7 @@ export function createRouter(init: RouterInit): Router { |
954 | 954 | loadingNavigation = navigation; |
955 | 955 |
|
956 | 956 | // Create a GET request for the loaders |
957 | | - request = createRequest(request.url, request.signal); |
| 957 | + request = new Request(request.url, { signal: request.signal }); |
958 | 958 | } |
959 | 959 |
|
960 | 960 | // Call loaders |
@@ -1299,7 +1299,11 @@ export function createRouter(init: RouterInit): Router { |
1299 | 1299 |
|
1300 | 1300 | // Call the action for the fetcher |
1301 | 1301 | let abortController = new AbortController(); |
1302 | | - let fetchRequest = createRequest(path, abortController.signal, submission); |
| 1302 | + let fetchRequest = createClientSideRequest( |
| 1303 | + path, |
| 1304 | + abortController.signal, |
| 1305 | + submission |
| 1306 | + ); |
1303 | 1307 | fetchControllers.set(key, abortController); |
1304 | 1308 |
|
1305 | 1309 | let actionResult = await callLoaderOrAction( |
@@ -1346,7 +1350,7 @@ export function createRouter(init: RouterInit): Router { |
1346 | 1350 | // Start the data load for current matches, or the next location if we're |
1347 | 1351 | // in the middle of a navigation |
1348 | 1352 | let nextLocation = state.navigation.location || state.location; |
1349 | | - let revalidationRequest = createRequest( |
| 1353 | + let revalidationRequest = createClientSideRequest( |
1350 | 1354 | nextLocation, |
1351 | 1355 | abortController.signal |
1352 | 1356 | ); |
@@ -1501,7 +1505,7 @@ export function createRouter(init: RouterInit): Router { |
1501 | 1505 |
|
1502 | 1506 | // Call the loader for this fetcher route match |
1503 | 1507 | let abortController = new AbortController(); |
1504 | | - let fetchRequest = createRequest(path, abortController.signal); |
| 1508 | + let fetchRequest = createClientSideRequest(path, abortController.signal); |
1505 | 1509 | fetchControllers.set(key, abortController); |
1506 | 1510 | let result: DataResult = await callLoaderOrAction( |
1507 | 1511 | "loader", |
@@ -1675,7 +1679,7 @@ export function createRouter(init: RouterInit): Router { |
1675 | 1679 | ...fetchersToLoad.map(([, href, match, fetchMatches]) => |
1676 | 1680 | callLoaderOrAction( |
1677 | 1681 | "loader", |
1678 | | - createRequest(href, request.signal), |
| 1682 | + createClientSideRequest(href, request.signal), |
1679 | 1683 | match, |
1680 | 1684 | fetchMatches, |
1681 | 1685 | router.basename |
@@ -2120,7 +2124,7 @@ export function unstable_createStaticHandler( |
2120 | 2124 | if (!actionMatch.route.action) { |
2121 | 2125 | let error = getInternalRouterError(405, { |
2122 | 2126 | method: request.method, |
2123 | | - pathname: createURL(request.url).pathname, |
| 2127 | + pathname: new URL(request.url).pathname, |
2124 | 2128 | routeId: actionMatch.route.id, |
2125 | 2129 | }); |
2126 | 2130 | if (isRouteRequest) { |
@@ -2206,7 +2210,7 @@ export function unstable_createStaticHandler( |
2206 | 2210 | } |
2207 | 2211 |
|
2208 | 2212 | // Create a GET request for the loaders |
2209 | | - let loaderRequest = createRequest(request.url, request.signal); |
| 2213 | + let loaderRequest = new Request(request.url, { signal: request.signal }); |
2210 | 2214 | let context = await loadRouteData(loaderRequest, matches); |
2211 | 2215 |
|
2212 | 2216 | return { |
@@ -2240,7 +2244,7 @@ export function unstable_createStaticHandler( |
2240 | 2244 | if (isRouteRequest && !routeMatch?.route.loader) { |
2241 | 2245 | throw getInternalRouterError(400, { |
2242 | 2246 | method: request.method, |
2243 | | - pathname: createURL(request.url).pathname, |
| 2247 | + pathname: new URL(request.url).pathname, |
2244 | 2248 | routeId: routeMatch?.route.id, |
2245 | 2249 | }); |
2246 | 2250 | } |
@@ -2531,9 +2535,9 @@ function shouldRevalidateLoader( |
2531 | 2535 | isRevalidationRequired: boolean, |
2532 | 2536 | actionResult: DataResult | undefined |
2533 | 2537 | ) { |
2534 | | - let currentUrl = createURL(currentLocation); |
| 2538 | + let currentUrl = createClientSideURL(currentLocation); |
2535 | 2539 | let currentParams = currentMatch.params; |
2536 | | - let nextUrl = createURL(location); |
| 2540 | + let nextUrl = createClientSideURL(location); |
2537 | 2541 | let nextParams = match.params; |
2538 | 2542 |
|
2539 | 2543 | // This is the default implementation as to when we revalidate. If the route |
@@ -2624,16 +2628,22 @@ async function callLoaderOrAction( |
2624 | 2628 | ); |
2625 | 2629 |
|
2626 | 2630 | // Check if this an external redirect that goes to a new origin |
2627 | | - let external = createURL(location).origin !== createURL("/").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; |
2628 | 2635 |
|
2629 | 2636 | // Support relative routing in internal redirects |
2630 | 2637 | if (!external) { |
2631 | 2638 | let activeMatches = matches.slice(0, matches.indexOf(match) + 1); |
2632 | 2639 | let routePathnames = getPathContributingMatches(activeMatches).map( |
2633 | 2640 | (match) => match.pathnameBase |
2634 | 2641 | ); |
2635 | | - let requestPath = createURL(request.url).pathname; |
2636 | | - let resolvedLocation = resolveTo(location, routePathnames, requestPath); |
| 2642 | + let resolvedLocation = resolveTo( |
| 2643 | + location, |
| 2644 | + routePathnames, |
| 2645 | + currentUrl.pathname |
| 2646 | + ); |
2637 | 2647 | invariant( |
2638 | 2648 | createPath(resolvedLocation), |
2639 | 2649 | `Unable to resolve redirect location: ${location}` |
@@ -2713,12 +2723,15 @@ async function callLoaderOrAction( |
2713 | 2723 | return { type: ResultType.data, data: result }; |
2714 | 2724 | } |
2715 | 2725 |
|
2716 | | -function createRequest( |
| 2726 | +// Utility method for creating the Request instances for loaders/actions during |
| 2727 | +// client-side navigations and fetches. During SSR we will always have a |
| 2728 | +// Request instance from the static handler (query/queryRoute) |
| 2729 | +function createClientSideRequest( |
2717 | 2730 | location: string | Location, |
2718 | 2731 | signal: AbortSignal, |
2719 | 2732 | submission?: Submission |
2720 | 2733 | ): Request { |
2721 | | - let url = createURL(stripHashFromPath(location)).toString(); |
| 2734 | + let url = createClientSideURL(stripHashFromPath(location)).toString(); |
2722 | 2735 | let init: RequestInit = { signal }; |
2723 | 2736 |
|
2724 | 2737 | if (submission) { |
|
0 commit comments