@@ -468,34 +468,54 @@ async function singleFetchLoaderNavigationStrategy(
468468
469469 await resolvePromise ;
470470
471- // If a middleware threw on the way down, we won't have data for our requested
472- // loaders and they'll resolve to `SingleFetchNoResultError` results. If this
473- // happens, take the highest error we find in our results (which is a middleware
474- // error if no loaders ever ran), and assign to these missing routes and let
475- // the router bubble accordingly
476- let middlewareError : unknown ;
477- let fetchedData = await singleFetchDfd . promise ;
478- if ( "routes" in fetchedData ) {
479- for ( let match of args . matches ) {
480- if ( match . route . id in fetchedData . routes ) {
481- let routeResult = fetchedData . routes [ match . route . id ] ;
482- if ( "error" in routeResult ) {
483- middlewareError = routeResult . error ;
484- break ;
471+ await bubbleMiddlewareErrors (
472+ singleFetchDfd . promise ,
473+ args . matches ,
474+ routesParams ,
475+ results
476+ ) ;
477+
478+ return results ;
479+ }
480+
481+ // If a middleware threw on the way down, we won't have data for our requested
482+ // loaders and they'll resolve to `SingleFetchNoResultError` results. If this
483+ // happens, take the highest error we find in our results (which is a middleware
484+ // error if no loaders ever ran), and assign to these missing routes and let
485+ // the router bubble accordingly
486+ async function bubbleMiddlewareErrors (
487+ singleFetchPromise : Promise < DecodedSingleFetchResults > ,
488+ matches : DataStrategyFunctionArgs [ "matches" ] ,
489+ routesParams : Set < string > ,
490+ results : Record < string , DataStrategyResult >
491+ ) {
492+ try {
493+ let middlewareError : unknown ;
494+ let fetchedData = await singleFetchPromise ;
495+
496+ if ( "routes" in fetchedData ) {
497+ for ( let match of matches ) {
498+ if ( match . route . id in fetchedData . routes ) {
499+ let routeResult = fetchedData . routes [ match . route . id ] ;
500+ if ( "error" in routeResult ) {
501+ middlewareError = routeResult . error ;
502+ break ;
503+ }
485504 }
486505 }
487506 }
488- }
489507
490- if ( middlewareError !== undefined ) {
491- Array . from ( routesParams . values ( ) ) . forEach ( ( routeId ) => {
492- if ( results [ routeId ] . result instanceof SingleFetchNoResultError ) {
493- results [ routeId ] . result = middlewareError ;
494- }
495- } ) ;
508+ if ( middlewareError !== undefined ) {
509+ Array . from ( routesParams . values ( ) ) . forEach ( ( routeId ) => {
510+ if ( results [ routeId ] . result instanceof SingleFetchNoResultError ) {
511+ results [ routeId ] . result = middlewareError ;
512+ }
513+ } ) ;
514+ }
515+ } catch ( e ) {
516+ // No-op - this logic is only intended to process successful responses
517+ // If the `.data` failed, the routes will handle those errors themselves
496518 }
497-
498- return results ;
499519}
500520
501521// Fetcher loader calls are much simpler than navigational loader calls
0 commit comments