Skip to content

Commit d4ecfaf

Browse files
committed
Lift queryImpl 👋
1 parent 0a853c5 commit d4ecfaf

File tree

1 file changed

+89
-84
lines changed

1 file changed

+89
-84
lines changed

‎packages/router/router.ts‎

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,6 +2394,11 @@ export function createStaticHandler(
23942394
request: Request,
23952395
{ requestContext, middlewareContext }: StaticHandlerQueryOpts = {}
23962396
): Promise<StaticHandlerContext | Response> {
2397+
invariant(
2398+
request.signal,
2399+
"query()/queryRoute() requests must contain an AbortController signal"
2400+
);
2401+
23972402
let url = new URL(request.url);
23982403
let method = request.method.toLowerCase();
23992404
let location = createLocation("", createPath(url), null, "default");
@@ -2438,15 +2443,33 @@ export function createStaticHandler(
24382443
};
24392444
}
24402445

2441-
let result = await queryImpl(
2442-
request,
2443-
location,
2444-
matches,
2445-
requestContext,
2446-
middlewareContext
2447-
);
2448-
if (isResponse(result)) {
2449-
return result;
2446+
let result: Omit<StaticHandlerContext, "location" | "basename">;
2447+
2448+
try {
2449+
if (isMutationMethod(request.method.toLowerCase())) {
2450+
result = await submit(
2451+
request,
2452+
matches,
2453+
getTargetMatch(matches, location),
2454+
requestContext,
2455+
middlewareContext,
2456+
false
2457+
);
2458+
} else {
2459+
let loaderResult = await loadRouteData(
2460+
request,
2461+
matches,
2462+
requestContext,
2463+
middlewareContext
2464+
);
2465+
result = {
2466+
...loaderResult,
2467+
actionData: null,
2468+
actionHeaders: {},
2469+
};
2470+
}
2471+
} catch (e) {
2472+
return handleStaticError(e);
24502473
}
24512474

24522475
// When returning StaticHandlerContext, we patch back in the location here
@@ -2483,6 +2506,11 @@ export function createStaticHandler(
24832506
middlewareContext,
24842507
}: StaticHandlerQueryRouteOpts = {}
24852508
): Promise<any> {
2509+
invariant(
2510+
request.signal,
2511+
"query()/queryRoute() requests must contain an AbortController signal"
2512+
);
2513+
24862514
let url = new URL(request.url);
24872515
let method = request.method.toLowerCase();
24882516
let location = createLocation("", createPath(url), null, "default");
@@ -2509,16 +2537,35 @@ export function createStaticHandler(
25092537
throw getInternalRouterError(404, { pathname: location.pathname });
25102538
}
25112539

2512-
let result = await queryImpl(
2513-
request,
2514-
location,
2515-
matches,
2516-
requestContext,
2517-
middlewareContext,
2518-
match
2519-
);
2520-
if (isResponse(result)) {
2521-
return result;
2540+
let result: Omit<StaticHandlerContext, "location" | "basename">;
2541+
2542+
try {
2543+
if (isMutationMethod(request.method.toLowerCase())) {
2544+
result = await submit(
2545+
request,
2546+
matches,
2547+
match,
2548+
requestContext,
2549+
middlewareContext,
2550+
true
2551+
);
2552+
} else {
2553+
let loadersResult = await loadRouteData(
2554+
request,
2555+
matches,
2556+
requestContext,
2557+
middlewareContext,
2558+
match
2559+
);
2560+
2561+
result = {
2562+
...loadersResult,
2563+
actionData: null,
2564+
actionHeaders: {},
2565+
};
2566+
}
2567+
} catch (e) {
2568+
return handleStaticError(e);
25222569
}
25232570

25242571
let error = result.errors ? Object.values(result.errors)[0] : undefined;
@@ -2546,73 +2593,14 @@ export function createStaticHandler(
25462593
return undefined;
25472594
}
25482595

2549-
async function queryImpl(
2550-
request: Request,
2551-
location: Location,
2552-
matches: AgnosticDataRouteMatch[],
2553-
requestContext: unknown,
2554-
middlewareContext?: MiddlewareContext,
2555-
routeMatch?: AgnosticDataRouteMatch
2556-
): Promise<Omit<StaticHandlerContext, "location" | "basename"> | Response> {
2557-
invariant(
2558-
request.signal,
2559-
"query()/queryRoute() requests must contain an AbortController signal"
2560-
);
2561-
2562-
try {
2563-
if (isMutationMethod(request.method.toLowerCase())) {
2564-
let result = await submit(
2565-
request,
2566-
matches,
2567-
routeMatch || getTargetMatch(matches, location),
2568-
requestContext,
2569-
middlewareContext,
2570-
routeMatch != null
2571-
);
2572-
return result;
2573-
}
2574-
2575-
let result = await loadRouteData(
2576-
request,
2577-
matches,
2578-
requestContext,
2579-
middlewareContext,
2580-
routeMatch
2581-
);
2582-
return isResponse(result)
2583-
? result
2584-
: {
2585-
...result,
2586-
actionData: null,
2587-
actionHeaders: {},
2588-
};
2589-
} catch (e) {
2590-
// If the user threw/returned a Response in callLoaderOrAction, we throw
2591-
// it to bail out and then return or throw here based on whether the user
2592-
// returned or threw
2593-
if (isQueryRouteResponse(e)) {
2594-
if (e.type === ResultType.error && !isRedirectResponse(e.response)) {
2595-
throw e.response;
2596-
}
2597-
return e.response;
2598-
}
2599-
// Redirects are always returned since they don't propagate to catch
2600-
// boundaries
2601-
if (isRedirectResponse(e)) {
2602-
return e;
2603-
}
2604-
throw e;
2605-
}
2606-
}
2607-
26082596
async function submit(
26092597
request: Request,
26102598
matches: AgnosticDataRouteMatch[],
26112599
actionMatch: AgnosticDataRouteMatch,
26122600
requestContext: unknown,
26132601
middlewareContext: MiddlewareContext | undefined,
26142602
isRouteRequest: boolean
2615-
): Promise<Omit<StaticHandlerContext, "location" | "basename"> | Response> {
2603+
): Promise<Omit<StaticHandlerContext, "location" | "basename">> {
26162604
let result: DataResult;
26172605

26182606
if (!actionMatch.route.action) {
@@ -2755,11 +2743,10 @@ export function createStaticHandler(
27552743
routeMatch?: AgnosticDataRouteMatch,
27562744
pendingActionError?: RouteData
27572745
): Promise<
2758-
| Omit<
2759-
StaticHandlerContext,
2760-
"location" | "basename" | "actionData" | "actionHeaders"
2761-
>
2762-
| Response
2746+
Omit<
2747+
StaticHandlerContext,
2748+
"location" | "basename" | "actionData" | "actionHeaders"
2749+
>
27632750
> {
27642751
let isRouteRequest = routeMatch != null;
27652752

@@ -2861,6 +2848,24 @@ export function createStaticHandler(
28612848
//#region Helpers
28622849
////////////////////////////////////////////////////////////////////////////////
28632850

2851+
function handleStaticError(e: unknown) {
2852+
// If the user threw/returned a Response in callLoaderOrAction, we throw
2853+
// it to bail out and then return or throw here based on whether the user
2854+
// returned or threw
2855+
if (isQueryRouteResponse(e)) {
2856+
if (e.type === ResultType.error && !isRedirectResponse(e.response)) {
2857+
throw e.response;
2858+
}
2859+
return e.response;
2860+
}
2861+
// Redirects are always returned since they don't propagate to catch
2862+
// boundaries
2863+
if (isRedirectResponse(e)) {
2864+
return e;
2865+
}
2866+
throw e;
2867+
}
2868+
28642869
/**
28652870
* Given an existing StaticHandlerContext and an error thrown at render time,
28662871
* provide an updated StaticHandlerContext suitable for a second SSR render

0 commit comments

Comments
 (0)