Skip to content

Commit 497224a

Browse files
committed
Switch to unstable_path and add back loaderRequests
1 parent b77f08a commit 497224a

File tree

9 files changed

+82
-103
lines changed

9 files changed

+82
-103
lines changed

packages/react-router/lib/dom/ssr/routes.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export function createClientRoutes(
345345
params,
346346
context,
347347
unstable_pattern,
348-
unstable_url,
348+
unstable_path,
349349
}: LoaderFunctionArgs,
350350
singleFetch?: unknown,
351351
) => {
@@ -365,7 +365,7 @@ export function createClientRoutes(
365365
params,
366366
context,
367367
unstable_pattern,
368-
unstable_url,
368+
unstable_path,
369369
async serverLoader() {
370370
preventInvalidServerHandlerCall("loader", route);
371371

@@ -406,7 +406,7 @@ export function createClientRoutes(
406406
params,
407407
context,
408408
unstable_pattern,
409-
unstable_url,
409+
unstable_path,
410410
}: ActionFunctionArgs,
411411
singleFetch?: unknown,
412412
) => {
@@ -427,7 +427,7 @@ export function createClientRoutes(
427427
params,
428428
context,
429429
unstable_pattern,
430-
unstable_url,
430+
unstable_path,
431431
async serverAction() {
432432
preventInvalidServerHandlerCall("action", route);
433433
return fetchServerAction(singleFetch);

packages/react-router/lib/router/router.ts

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ export interface StaticHandler {
463463
},
464464
) => Promise<StaticHandlerContext | Response>,
465465
) => MaybePromise<Response>;
466-
unstable_normalizeUrl?: (r: Request) => URL;
466+
unstable_normalizeUrl?: (url: URL) => URL;
467467
},
468468
): Promise<StaticHandlerContext | Response>;
469469
queryRoute(
@@ -475,7 +475,7 @@ export interface StaticHandler {
475475
generateMiddlewareResponse?: (
476476
queryRoute: (r: Request) => Promise<Response>,
477477
) => MaybePromise<Response>;
478-
unstable_normalizeUrl?: (r: Request) => URL;
478+
unstable_normalizeUrl?: (url: URL) => URL;
479479
},
480480
): Promise<any>;
481481
}
@@ -1821,18 +1821,11 @@ export function createRouter(init: RouterInit): Router {
18211821
fogOfWar.active = false;
18221822

18231823
// Create a GET request for the loaders
1824-
if (future.unstable_passThroughRequests) {
1825-
// Don't let loaders consume any request bodies
1826-
if (!request.bodyUsed) {
1827-
request.body?.cancel();
1828-
}
1829-
} else {
1830-
request = createClientSideRequest(
1831-
init.history,
1832-
request.url,
1833-
request.signal,
1834-
);
1835-
}
1824+
request = createClientSideRequest(
1825+
init.history,
1826+
request.url,
1827+
request.signal,
1828+
);
18361829
}
18371830

18381831
// Call loaders
@@ -3118,13 +3111,13 @@ export function createRouter(init: RouterInit): Router {
31183111
matches: DataStrategyMatch[],
31193112
fetchersToLoad: RevalidatingFetcher[],
31203113
request: Request,
3121-
path: To,
3114+
location: Location,
31223115
scopedContext: RouterContextProvider,
31233116
) {
31243117
// Kick off loaders and fetchers in parallel
31253118
let loaderResultsPromise = callDataStrategy(
31263119
request,
3127-
path,
3120+
location,
31283121
matches,
31293122
scopedContext,
31303123
null,
@@ -3798,11 +3791,12 @@ export function createStaticHandler(
37983791
unstable_normalizeUrl,
37993792
}: Parameters<StaticHandler["query"]>[1] = {},
38003793
): Promise<StaticHandlerContext | Response> {
3801-
let normalizedUrl = unstable_normalizeUrl
3802-
? unstable_normalizeUrl(request)
3803-
: new URL(request.url);
3794+
let url = new URL(request.url);
3795+
if (unstable_normalizeUrl) {
3796+
url = unstable_normalizeUrl(url);
3797+
}
38043798
let method = request.method;
3805-
let location = createLocation("", normalizedUrl, null, "default");
3799+
let location = createLocation("", createPath(url), null, "default");
38063800
let matches = matchRoutes(dataRoutes, location, basename);
38073801
requestContext =
38083802
requestContext != null ? requestContext : new RouterContextProvider();
@@ -3881,7 +3875,7 @@ export function createStaticHandler(
38813875
let response = await runServerMiddlewarePipeline(
38823876
{
38833877
request,
3884-
unstable_url: createNormalizedUrlFromLocation(request, location),
3878+
unstable_path: stripIndexParam(location),
38853879
unstable_pattern: getRoutePattern(matches),
38863880
matches,
38873881
params: matches[0].params,
@@ -4077,11 +4071,12 @@ export function createStaticHandler(
40774071
unstable_normalizeUrl,
40784072
}: Parameters<StaticHandler["queryRoute"]>[1] = {},
40794073
): Promise<any> {
4080-
let normalizedUrl = unstable_normalizeUrl
4081-
? unstable_normalizeUrl(request)
4082-
: new URL(request.url);
4083-
let location = createLocation("", normalizedUrl, null, "default");
4074+
let url = new URL(request.url);
4075+
if (unstable_normalizeUrl) {
4076+
url = unstable_normalizeUrl(url);
4077+
}
40844078
let method = request.method;
4079+
let location = createLocation("", createPath(url), null, "default");
40854080
let matches = matchRoutes(dataRoutes, location, basename);
40864081
requestContext =
40874082
requestContext != null ? requestContext : new RouterContextProvider();
@@ -4117,7 +4112,7 @@ export function createStaticHandler(
41174112
let response = await runServerMiddlewarePipeline(
41184113
{
41194114
request,
4120-
unstable_url: normalizedUrl,
4115+
unstable_path: stripIndexParam(location),
41214116
unstable_pattern: getRoutePattern(matches),
41224117
matches,
41234118
params: matches[0].params,
@@ -4208,7 +4203,7 @@ export function createStaticHandler(
42084203

42094204
async function queryImpl(
42104205
request: Request,
4211-
path: To,
4206+
location: Location,
42124207
matches: AgnosticDataRouteMatch[],
42134208
requestContext: unknown,
42144209
dataStrategy: DataStrategyFunction<unknown> | null,
@@ -4226,9 +4221,9 @@ export function createStaticHandler(
42264221
if (isMutationMethod(request.method)) {
42274222
let result = await submit(
42284223
request,
4229-
path,
4224+
location,
42304225
matches,
4231-
routeMatch || getTargetMatch(matches, new URL(request.url)),
4226+
routeMatch || getTargetMatch(matches, location),
42324227
requestContext,
42334228
dataStrategy,
42344229
skipLoaderErrorBubbling,
@@ -4241,7 +4236,7 @@ export function createStaticHandler(
42414236

42424237
let result = await loadRouteData(
42434238
request,
4244-
path,
4239+
location,
42454240
matches,
42464241
requestContext,
42474242
dataStrategy,
@@ -4277,7 +4272,7 @@ export function createStaticHandler(
42774272

42784273
async function submit(
42794274
request: Request,
4280-
path: To,
4275+
location: Location,
42814276
matches: AgnosticDataRouteMatch[],
42824277
actionMatch: AgnosticDataRouteMatch,
42834278
requestContext: unknown,
@@ -4307,7 +4302,7 @@ export function createStaticHandler(
43074302
mapRouteProperties,
43084303
manifest,
43094304
request,
4310-
path,
4305+
location,
43114306
matches,
43124307
actionMatch,
43134308
[],
@@ -4316,7 +4311,7 @@ export function createStaticHandler(
43164311

43174312
let results = await callDataStrategy(
43184313
request,
4319-
path,
4314+
location,
43204315
dsMatches,
43214316
isRouteRequest,
43224317
requestContext,
@@ -4405,20 +4400,11 @@ export function createStaticHandler(
44054400
}
44064401

44074402
// Create a GET request for the loaders
4408-
let loaderRequest: Request;
4409-
if (future.unstable_passThroughRequests) {
4410-
// Don't permit loaders to read from POST request bodies
4411-
if (!request.bodyUsed) {
4412-
request.body?.cancel();
4413-
}
4414-
loaderRequest = request;
4415-
} else {
4416-
loaderRequest = new Request(request.url, {
4417-
headers: request.headers,
4418-
redirect: request.redirect,
4419-
signal: request.signal,
4420-
});
4421-
}
4403+
let loaderRequest = new Request(request.url, {
4404+
headers: request.headers,
4405+
redirect: request.redirect,
4406+
signal: request.signal,
4407+
});
44224408

44234409
if (isErrorResult(result)) {
44244410
// Store off the pending error - we use it to determine which loaders
@@ -4429,7 +4415,7 @@ export function createStaticHandler(
44294415

44304416
let handlerContext = await loadRouteData(
44314417
loaderRequest,
4432-
path,
4418+
location,
44334419
matches,
44344420
requestContext,
44354421
dataStrategy,
@@ -4456,7 +4442,7 @@ export function createStaticHandler(
44564442

44574443
let handlerContext = await loadRouteData(
44584444
loaderRequest,
4459-
path,
4445+
location,
44604446
matches,
44614447
requestContext,
44624448
dataStrategy,
@@ -4480,7 +4466,7 @@ export function createStaticHandler(
44804466

44814467
async function loadRouteData(
44824468
request: Request,
4483-
path: To,
4469+
location: Location,
44844470
matches: AgnosticDataRouteMatch[],
44854471
requestContext: unknown,
44864472
dataStrategy: DataStrategyFunction<unknown> | null,
@@ -4516,7 +4502,7 @@ export function createStaticHandler(
45164502
mapRouteProperties,
45174503
manifest,
45184504
request,
4519-
path,
4505+
location,
45204506
matches,
45214507
routeMatch,
45224508
[],
@@ -4536,7 +4522,7 @@ export function createStaticHandler(
45364522
mapRouteProperties,
45374523
manifest,
45384524
request,
4539-
path,
4525+
location,
45404526
pattern,
45414527
match,
45424528
[],
@@ -4549,7 +4535,7 @@ export function createStaticHandler(
45494535
mapRouteProperties,
45504536
manifest,
45514537
request,
4552-
path,
4538+
location,
45534539
pattern,
45544540
match,
45554541
[],
@@ -4579,7 +4565,7 @@ export function createStaticHandler(
45794565

45804566
let results = await callDataStrategy(
45814567
request,
4582-
path,
4568+
location,
45834569
dsMatches,
45844570
isRouteRequest,
45854571
requestContext,
@@ -4609,7 +4595,7 @@ export function createStaticHandler(
46094595
// pass around the manifest, mapRouteProperties, etc.
46104596
async function callDataStrategy(
46114597
request: Request,
4612-
path: To,
4598+
location: Location,
46134599
matches: DataStrategyMatch[],
46144600
isRouteRequest: boolean,
46154601
requestContext: unknown,
@@ -4618,7 +4604,7 @@ export function createStaticHandler(
46184604
let results = await callDataStrategyImpl(
46194605
dataStrategy || defaultDataStrategy,
46204606
request,
4621-
path,
4607+
location,
46224608
matches,
46234609
null,
46244610
requestContext,
@@ -6150,12 +6136,7 @@ async function callDataStrategyImpl(
61506136
"fetcherKey" | "runClientMiddleware"
61516137
> = {
61526138
request,
6153-
unstable_url: createNormalizedUrlFromLocation(request, {
6154-
pathname: "",
6155-
search: "",
6156-
hash: "",
6157-
...(typeof path === "string" ? parsePath(path) : path),
6158-
}),
6139+
unstable_path: stripIndexParam(path),
61596140
unstable_pattern: getRoutePattern(matches),
61606141
params: matches[0].params,
61616142
context: scopedContext,
@@ -6258,7 +6239,7 @@ async function callLoaderOrAction({
62586239
return handler(
62596240
{
62606241
request,
6261-
unstable_url: createNormalizedUrlFromLocation(request, path),
6242+
unstable_path: stripIndexParam(path),
62626243
unstable_pattern,
62636244
params: match.params,
62646245
context: scopedContext,
@@ -6558,20 +6539,23 @@ function createClientSideRequest(
65586539
return new Request(url, init);
65596540
}
65606541

6561-
function createNormalizedUrlFromLocation(request: Request, path: To): URL {
6562-
let url = new URL(
6563-
new URL(request.url).origin +
6564-
createPath(typeof path === "string" ? parsePath(path) : path),
6565-
);
6542+
function stripIndexParam(path: To): Path {
6543+
let parsed = typeof path === "string" ? parsePath(path) : path;
6544+
let searchParams = new URLSearchParams(parsed.search);
65666545

65676546
// Strip naked index param, preserve any other index params with values
6568-
let indexValues = url.searchParams.getAll("index");
6569-
url.searchParams.delete("index");
6547+
let indexValues = searchParams.getAll("index");
6548+
searchParams.delete("index");
65706549
for (let value of indexValues.filter(Boolean)) {
6571-
url.searchParams.append("index", value);
6550+
searchParams.append("index", value);
65726551
}
65736552

6574-
return url;
6553+
return {
6554+
pathname: "",
6555+
hash: "",
6556+
...parsed,
6557+
search: searchParams.size ? `?${searchParams.toString()}` : "",
6558+
};
65756559
}
65766560

65776561
function convertFormDataToSearchParams(formData: FormData): URLSearchParams {

packages/react-router/lib/router/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ interface DataFunctionArgs<Context> {
270270
/** A {@link https://developer.mozilla.org/en-US/docs/Web/API/Request Fetch Request instance} which you can use to read headers (like cookies, and {@link https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams URLSearchParams} from the request. */
271271
request: Request;
272272
/**
273-
* The URL of the application location being navigated to or fetched.
273+
* The application location being navigated to or fetched.
274274
* Without `future.unstable_passThroughRequests` enabled, this matches `request.url`.
275275
* With `future.unstable_passThroughRequests` enabled, this is a normalized
276276
* version of `request.url` with React-Router-specific implementation details
277277
* removed (`.data` pathnames, `index`/`_routes` search params)
278278
*/
279-
unstable_url: URL;
279+
unstable_path: Path;
280280
/**
281281
* Matched un-interpolated route pattern for the current path (i.e., /blog/:slug).
282282
* Mostly useful as a identifier to aggregate on for logging/tracing/etc.

0 commit comments

Comments
 (0)