@@ -959,8 +959,10 @@ export function createRouter(init: RouterInit): Router {
959
959
// All initialMatches need to be loaded before we're ready. If we have lazy
960
960
// functions around still then we'll need to run them in initialize()
961
961
initialized = false ;
962
- } else if ( ! initialMatches . some ( ( m ) => m . route . loader ) ) {
963
- // If we've got no loaders to run, then we're good to go
962
+ } else if (
963
+ ! initialMatches . some ( ( m ) => routeHasLoaderOrMiddleware ( m . route ) )
964
+ ) {
965
+ // If we've got no loaders or middleware to run, then we're good to go
964
966
initialized = true ;
965
967
} else {
966
968
// With "partial hydration", we're initialized so long as we were
@@ -2092,7 +2094,9 @@ export function createRouter(init: RouterInit): Router {
2092
2094
if (
2093
2095
! init . dataStrategy &&
2094
2096
! dsMatches . some ( ( m ) => m . shouldLoad ) &&
2095
- ! dsMatches . some ( ( m ) => m . route . middleware ) &&
2097
+ ! dsMatches . some (
2098
+ ( m ) => m . route . middleware && m . route . middleware . length > 0 ,
2099
+ ) &&
2096
2100
revalidatingFetchers . length === 0
2097
2101
) {
2098
2102
let updatedFetchers = markFetchRedirectsDone ( ) ;
@@ -4763,7 +4767,7 @@ function getMatchesToLoad(
4763
4767
} else if ( route . lazy ) {
4764
4768
// We haven't loaded this route yet so we don't know if it's got a loader!
4765
4769
forceShouldLoad = true ;
4766
- } else if ( route . loader == null ) {
4770
+ } else if ( ! routeHasLoaderOrMiddleware ( route ) ) {
4767
4771
// Nothing to load!
4768
4772
forceShouldLoad = false ;
4769
4773
} else if ( initialHydration ) {
@@ -4950,6 +4954,13 @@ function getMatchesToLoad(
4950
4954
return { dsMatches, revalidatingFetchers } ;
4951
4955
}
4952
4956
4957
+ function routeHasLoaderOrMiddleware ( route : RouteObject ) {
4958
+ return (
4959
+ route . loader != null ||
4960
+ ( route . middleware != null && route . middleware . length > 0 )
4961
+ ) ;
4962
+ }
4963
+
4953
4964
function shouldLoadRouteOnHydration (
4954
4965
route : AgnosticDataRouteObject ,
4955
4966
loaderData : RouteData | null | undefined ,
@@ -4960,8 +4971,8 @@ function shouldLoadRouteOnHydration(
4960
4971
return true ;
4961
4972
}
4962
4973
4963
- // No loader, nothing to initialize
4964
- if ( ! route . loader ) {
4974
+ // No loader or middleware , nothing to run
4975
+ if ( ! routeHasLoaderOrMiddleware ( route ) ) {
4965
4976
return false ;
4966
4977
}
4967
4978
@@ -5519,9 +5530,15 @@ function runClientMiddlewarePipeline(
5519
5530
let { matches } = args ;
5520
5531
let maxBoundaryIdx = Math . min (
5521
5532
// Throwing route
5522
- matches . findIndex ( ( m ) => m . route . id === routeId ) || 0 ,
5533
+ Math . max (
5534
+ matches . findIndex ( ( m ) => m . route . id === routeId ) ,
5535
+ 0 ,
5536
+ ) ,
5523
5537
// or the shallowest route that needs to load data
5524
- matches . findIndex ( ( m ) => m . unstable_shouldCallHandler ( ) ) || 0 ,
5538
+ Math . max (
5539
+ matches . findIndex ( ( m ) => m . unstable_shouldCallHandler ( ) ) ,
5540
+ 0 ,
5541
+ ) ,
5525
5542
) ;
5526
5543
let boundaryRouteId = findNearestBoundary (
5527
5544
matches ,
0 commit comments