@@ -959,8 +959,10 @@ export function createRouter(init: RouterInit): Router {
959959 // All initialMatches need to be loaded before we're ready. If we have lazy
960960 // functions around still then we'll need to run them in initialize()
961961 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
964966 initialized = true ;
965967 } else {
966968 // With "partial hydration", we're initialized so long as we were
@@ -2092,7 +2094,9 @@ export function createRouter(init: RouterInit): Router {
20922094 if (
20932095 ! init . dataStrategy &&
20942096 ! 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+ ) &&
20962100 revalidatingFetchers . length === 0
20972101 ) {
20982102 let updatedFetchers = markFetchRedirectsDone ( ) ;
@@ -4763,7 +4767,7 @@ function getMatchesToLoad(
47634767 } else if ( route . lazy ) {
47644768 // We haven't loaded this route yet so we don't know if it's got a loader!
47654769 forceShouldLoad = true ;
4766- } else if ( route . loader == null ) {
4770+ } else if ( ! routeHasLoaderOrMiddleware ( route ) ) {
47674771 // Nothing to load!
47684772 forceShouldLoad = false ;
47694773 } else if ( initialHydration ) {
@@ -4950,6 +4954,13 @@ function getMatchesToLoad(
49504954 return { dsMatches, revalidatingFetchers } ;
49514955}
49524956
4957+ function routeHasLoaderOrMiddleware ( route : RouteObject ) {
4958+ return (
4959+ route . loader != null ||
4960+ ( route . middleware != null && route . middleware . length > 0 )
4961+ ) ;
4962+ }
4963+
49534964function shouldLoadRouteOnHydration (
49544965 route : AgnosticDataRouteObject ,
49554966 loaderData : RouteData | null | undefined ,
@@ -4960,8 +4971,8 @@ function shouldLoadRouteOnHydration(
49604971 return true ;
49614972 }
49624973
4963- // No loader, nothing to initialize
4964- if ( ! route . loader ) {
4974+ // No loader or middleware , nothing to run
4975+ if ( ! routeHasLoaderOrMiddleware ( route ) ) {
49654976 return false ;
49664977 }
49674978
@@ -5519,9 +5530,15 @@ function runClientMiddlewarePipeline(
55195530 let { matches } = args ;
55205531 let maxBoundaryIdx = Math . min (
55215532 // 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+ ) ,
55235537 // 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+ ) ,
55255542 ) ;
55265543 let boundaryRouteId = findNearestBoundary (
55275544 matches ,
0 commit comments