@@ -199,6 +199,28 @@ const isRouteVirtualModule = (id: string): boolean => {
199199 return isRouteEntryModuleId ( id ) || isRouteChunkModuleId ( id ) ;
200200} ;
201201
202+ const isServerBuildVirtualModuleId = ( id : string ) : boolean => {
203+ return id . split ( "?" ) [ 0 ] === virtual . serverBuild . id ;
204+ } ;
205+
206+ const getServerBuildFile = ( viteManifest : Vite . Manifest ) : string => {
207+ let serverBuildIds = Object . keys ( viteManifest ) . filter (
208+ isServerBuildVirtualModuleId
209+ ) ;
210+
211+ invariant (
212+ serverBuildIds . length <= 1 ,
213+ "Multiple server build files found in manifest"
214+ ) ;
215+
216+ invariant (
217+ serverBuildIds . length === 1 ,
218+ "Server build file not found in manifest"
219+ ) ;
220+
221+ return viteManifest [ serverBuildIds [ 0 ] ] . file ;
222+ } ;
223+
202224export type ServerBundleBuildConfig = {
203225 routes : RouteManifest ;
204226 serverBundleId : string ;
@@ -1518,7 +1540,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
15181540 viteConfig ,
15191541 ctx . reactRouterConfig ,
15201542 serverBuildDirectory ,
1521- ssrViteManifest [ virtual . serverBuild . id ] . file ,
1543+ getServerBuildFile ( ssrViteManifest ) ,
15221544 clientBuildDirectory
15231545 ) ;
15241546 }
@@ -1531,7 +1553,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
15311553 viteConfig ,
15321554 ctx . reactRouterConfig ,
15331555 serverBuildDirectory ,
1534- ssrViteManifest [ virtual . serverBuild . id ] . file ,
1556+ getServerBuildFile ( ssrViteManifest ) ,
15351557 clientBuildDirectory
15361558 ) ;
15371559 }
@@ -2406,19 +2428,28 @@ async function handlePrerender(
24062428 serverBuildPath
24072429 ) ;
24082430
2409- let routes = createPrerenderRoutes ( build . routes ) ;
2431+ let routes = createPrerenderRoutes ( reactRouterConfig . routes ) ;
2432+ for ( let path of build . prerender ) {
2433+ let matches = matchRoutes ( routes , `/${ path } /` . replace ( / ^ \/ \/ + / , "/" ) ) ;
2434+ if ( ! matches ) {
2435+ throw new Error (
2436+ `Unable to prerender path because it does not match any routes: ${ path } `
2437+ ) ;
2438+ }
2439+ }
2440+
2441+ let buildRoutes = createPrerenderRoutes ( build . routes ) ;
24102442 let headers = {
24112443 // Header that can be used in the loader to know if you're running at
24122444 // build time or runtime
24132445 "X-React-Router-Prerender" : "yes" ,
24142446 } ;
24152447 for ( let path of build . prerender ) {
24162448 // Ensure we have a leading slash for matching
2417- let matches = matchRoutes ( routes , `/${ path } /` . replace ( / ^ \/ \/ + / , "/" ) ) ;
2418- invariant (
2419- matches ,
2420- `Unable to prerender path because it does not match any routes: ${ path } `
2421- ) ;
2449+ let matches = matchRoutes ( buildRoutes , `/${ path } /` . replace ( / ^ \/ \/ + / , "/" ) ) ;
2450+ if ( ! matches ) {
2451+ continue ;
2452+ }
24222453 // When prerendering a resource route, we don't want to pass along the
24232454 // `.data` file since we want to prerender the raw Response returned from
24242455 // the loader. Presumably this is for routes where a file extension is
0 commit comments