@@ -2394,6 +2394,11 @@ export function createStaticHandler(
2394
2394
request : Request ,
2395
2395
{ requestContext, middlewareContext } : StaticHandlerQueryOpts = { }
2396
2396
) : Promise < StaticHandlerContext | Response > {
2397
+ invariant (
2398
+ request . signal ,
2399
+ "query()/queryRoute() requests must contain an AbortController signal"
2400
+ ) ;
2401
+
2397
2402
let url = new URL ( request . url ) ;
2398
2403
let method = request . method . toLowerCase ( ) ;
2399
2404
let location = createLocation ( "" , createPath ( url ) , null , "default" ) ;
@@ -2438,15 +2443,33 @@ export function createStaticHandler(
2438
2443
} ;
2439
2444
}
2440
2445
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 ) ;
2450
2473
}
2451
2474
2452
2475
// When returning StaticHandlerContext, we patch back in the location here
@@ -2483,6 +2506,11 @@ export function createStaticHandler(
2483
2506
middlewareContext,
2484
2507
} : StaticHandlerQueryRouteOpts = { }
2485
2508
) : Promise < any > {
2509
+ invariant (
2510
+ request . signal ,
2511
+ "query()/queryRoute() requests must contain an AbortController signal"
2512
+ ) ;
2513
+
2486
2514
let url = new URL ( request . url ) ;
2487
2515
let method = request . method . toLowerCase ( ) ;
2488
2516
let location = createLocation ( "" , createPath ( url ) , null , "default" ) ;
@@ -2509,16 +2537,35 @@ export function createStaticHandler(
2509
2537
throw getInternalRouterError ( 404 , { pathname : location . pathname } ) ;
2510
2538
}
2511
2539
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 ) ;
2522
2569
}
2523
2570
2524
2571
let error = result . errors ? Object . values ( result . errors ) [ 0 ] : undefined ;
@@ -2546,73 +2593,14 @@ export function createStaticHandler(
2546
2593
return undefined ;
2547
2594
}
2548
2595
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
-
2608
2596
async function submit (
2609
2597
request : Request ,
2610
2598
matches : AgnosticDataRouteMatch [ ] ,
2611
2599
actionMatch : AgnosticDataRouteMatch ,
2612
2600
requestContext : unknown ,
2613
2601
middlewareContext : MiddlewareContext | undefined ,
2614
2602
isRouteRequest : boolean
2615
- ) : Promise < Omit < StaticHandlerContext , "location" | "basename" > | Response > {
2603
+ ) : Promise < Omit < StaticHandlerContext , "location" | "basename" > > {
2616
2604
let result : DataResult ;
2617
2605
2618
2606
if ( ! actionMatch . route . action ) {
@@ -2755,11 +2743,10 @@ export function createStaticHandler(
2755
2743
routeMatch ?: AgnosticDataRouteMatch ,
2756
2744
pendingActionError ?: RouteData
2757
2745
) : Promise <
2758
- | Omit <
2759
- StaticHandlerContext ,
2760
- "location" | "basename" | "actionData" | "actionHeaders"
2761
- >
2762
- | Response
2746
+ Omit <
2747
+ StaticHandlerContext ,
2748
+ "location" | "basename" | "actionData" | "actionHeaders"
2749
+ >
2763
2750
> {
2764
2751
let isRouteRequest = routeMatch != null ;
2765
2752
@@ -2861,6 +2848,24 @@ export function createStaticHandler(
2861
2848
//#region Helpers
2862
2849
////////////////////////////////////////////////////////////////////////////////
2863
2850
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
+
2864
2869
/**
2865
2870
* Given an existing StaticHandlerContext and an error thrown at render time,
2866
2871
* provide an updated StaticHandlerContext suitable for a second SSR render
0 commit comments