@@ -3490,12 +3490,6 @@ export function createStaticHandler(
3490
3490
"`requestContext` must bean instance of `unstable_RouterContextProvider`"
3491
3491
) ;
3492
3492
try {
3493
- // Run middleware as far deep as the deepest loader to be executed
3494
- let tailIdx = [ ...matches ]
3495
- . reverse ( )
3496
- . findIndex ( ( m ) => ! filterMatchesToLoad || filterMatchesToLoad ( m ) ) ;
3497
- let lowestLoadingIdx = tailIdx < 0 ? 0 : matches . length - 1 - tailIdx ;
3498
-
3499
3493
let renderedStaticContext : StaticHandlerContext | undefined ;
3500
3494
let response = await runMiddlewarePipeline (
3501
3495
{
@@ -3506,7 +3500,6 @@ export function createStaticHandler(
3506
3500
// this to the proper type knowing it's not an `AppLoadContext`
3507
3501
context : requestContext as unstable_RouterContextProvider ,
3508
3502
} ,
3509
- lowestLoadingIdx ,
3510
3503
true ,
3511
3504
async ( ) => {
3512
3505
let result = await queryImpl (
@@ -3700,7 +3693,6 @@ export function createStaticHandler(
3700
3693
// this to the proper type knowing it's not an `AppLoadContext`
3701
3694
context : requestContext as unstable_RouterContextProvider ,
3702
3695
} ,
3703
- matches . length - 1 ,
3704
3696
true ,
3705
3697
async ( ) => {
3706
3698
let result = await queryImpl (
@@ -4940,91 +4932,65 @@ async function defaultDataStrategyWithMiddleware(
4940
4932
return defaultDataStrategy ( args ) ;
4941
4933
}
4942
4934
4943
- // Determine how far down we'll be loading so we only run middleware to that
4944
- // point. This prevents us from calling middleware below an action error
4945
- // boundary below which we don't run loaders
4946
- let lastIndex = args . matches . length - 1 ;
4947
- for ( let i = lastIndex ; i >= 0 ; i -- ) {
4948
- if ( args . matches [ i ] . shouldLoad ) {
4949
- lastIndex = i ;
4950
- break ;
4951
- }
4952
- }
4953
-
4954
- let results = await runMiddlewarePipeline (
4935
+ return runMiddlewarePipeline (
4955
4936
args ,
4956
- lastIndex ,
4957
4937
false ,
4958
- async ( keyedResults : Record < string , DataStrategyResult > ) => {
4959
- Object . assign ( keyedResults , await defaultDataStrategy ( args ) ) ;
4960
- } ,
4961
- ( e , keyedResults ) => {
4962
- // we caught an error running the middleware, copy that overtop any
4963
- // non-error result for the route
4964
- Object . assign ( keyedResults , {
4965
- [ e . routeId ] : { type : "error" , result : e . error } ,
4966
- } ) ;
4967
- }
4968
- ) ;
4969
- return results as Record < string , DataStrategyResult > ;
4938
+ ( ) => defaultDataStrategy ( args ) ,
4939
+ ( e ) => ( { [ e . routeId ] : { type : "error" , result : e . error } } )
4940
+ ) as Promise < Record < string , DataStrategyResult > > ;
4970
4941
}
4971
4942
4972
4943
type MutableMiddlewareState = {
4973
- keyedResults : Record < string , DataStrategyResult > ;
4944
+ handlerResult : unknown ;
4974
4945
propagateResult : boolean ;
4975
4946
} ;
4976
4947
4977
- export async function runMiddlewarePipeline (
4978
- {
4979
- request,
4980
- params,
4981
- context,
4982
- matches,
4983
- } : (
4948
+ export async function runMiddlewarePipeline < T extends boolean > (
4949
+ args : (
4984
4950
| LoaderFunctionArgs < unstable_RouterContextProvider >
4985
4951
| ActionFunctionArgs < unstable_RouterContextProvider >
4986
4952
) & {
4953
+ // Don't use `DataStrategyFunctionArgs` directly so we can we reduce these
4954
+ // back from `DataStrategyMatch` to regular matches for use in the staticHandler
4987
4955
matches : AgnosticDataRouteMatch [ ] ;
4988
4956
} ,
4989
- lastIndex : number ,
4990
- propagateResult : boolean ,
4991
- handler : ( results : Record < string , DataStrategyResult > ) => unknown ,
4992
- errorHandler : (
4993
- error : MiddlewareError ,
4994
- results : Record < string , DataStrategyResult >
4995
- ) => unknown
4957
+ propagateResult : T ,
4958
+ handler : ( ) => T extends true
4959
+ ? MaybePromise < Response >
4960
+ : MaybePromise < Record < string , DataStrategyResult > > ,
4961
+ errorHandler : ( error : MiddlewareError ) => unknown
4996
4962
) : Promise < unknown > {
4963
+ let { matches, request, params, context } = args ;
4997
4964
let middlewareState : MutableMiddlewareState = {
4998
- keyedResults : { } ,
4965
+ handlerResult : undefined ,
4999
4966
propagateResult,
5000
4967
} ;
5001
4968
try {
4969
+ let tuples = matches . flatMap ( ( m ) =>
4970
+ m . route . unstable_middleware
4971
+ ? m . route . unstable_middleware . map ( ( fn ) => [ m . route . id , fn ] )
4972
+ : [ ]
4973
+ ) as [ string , unstable_MiddlewareFunction ] [ ] ;
5002
4974
let result = await callRouteMiddleware (
5003
- matches
5004
- . slice ( 0 , lastIndex + 1 )
5005
- . flatMap ( ( m ) =>
5006
- m . route . unstable_middleware
5007
- ? m . route . unstable_middleware . map ( ( fn ) => [ m . route . id , fn ] )
5008
- : [ ]
5009
- ) as [ string , unstable_MiddlewareFunction ] [ ] ,
5010
- 0 ,
5011
4975
{ request, params, context } ,
4976
+ tuples ,
5012
4977
middlewareState ,
5013
4978
handler
5014
4979
) ;
5015
4980
return middlewareState . propagateResult
5016
4981
? result
5017
- : middlewareState . keyedResults ;
4982
+ : middlewareState . handlerResult ;
5018
4983
} catch ( e ) {
5019
4984
if ( ! ( e instanceof MiddlewareError ) ) {
5020
4985
// This shouldn't happen? This would have to come from a bug in our
5021
4986
// library code...
5022
4987
throw e ;
5023
4988
}
5024
- let result = await errorHandler ( e , middlewareState . keyedResults ) ;
5025
- return middlewareState . propagateResult
5026
- ? result
5027
- : middlewareState . keyedResults ;
4989
+ let result = await errorHandler ( e ) ;
4990
+ if ( propagateResult || ! middlewareState . handlerResult ) {
4991
+ return result ;
4992
+ }
4993
+ return Object . assign ( middlewareState . handlerResult , result ) ;
5028
4994
}
5029
4995
}
5030
4996
@@ -5038,13 +5004,13 @@ export class MiddlewareError {
5038
5004
}
5039
5005
5040
5006
async function callRouteMiddleware (
5041
- middlewares : [ string , unstable_MiddlewareFunction ] [ ] ,
5042
- idx : number ,
5043
5007
args :
5044
5008
| LoaderFunctionArgs < unstable_RouterContextProvider >
5045
5009
| ActionFunctionArgs < unstable_RouterContextProvider > ,
5010
+ middlewares : [ string , unstable_MiddlewareFunction ] [ ] ,
5046
5011
middlewareState : MutableMiddlewareState ,
5047
- handler : ( r : Record < string , DataStrategyResult > ) => void
5012
+ handler : ( ) => void ,
5013
+ idx = 0
5048
5014
) : Promise < unknown > {
5049
5015
let { request } = args ;
5050
5016
if ( request . signal . aborted ) {
@@ -5059,8 +5025,8 @@ async function callRouteMiddleware(
5059
5025
let tuple = middlewares [ idx ] ;
5060
5026
if ( ! tuple ) {
5061
5027
// We reached the end of our middlewares, call the handler
5062
- let result = await handler ( middlewareState . keyedResults ) ;
5063
- return result ;
5028
+ middlewareState . handlerResult = await handler ( ) ;
5029
+ return middlewareState . handlerResult ;
5064
5030
}
5065
5031
5066
5032
let [ routeId , middleware ] = tuple ;
@@ -5072,11 +5038,11 @@ async function callRouteMiddleware(
5072
5038
}
5073
5039
nextCalled = true ;
5074
5040
let result = await callRouteMiddleware (
5075
- middlewares ,
5076
- idx + 1 ,
5077
5041
args ,
5042
+ middlewares ,
5078
5043
middlewareState ,
5079
- handler
5044
+ handler ,
5045
+ idx + 1
5080
5046
) ;
5081
5047
if ( middlewareState . propagateResult ) {
5082
5048
nextResult = result ;
0 commit comments