@@ -74,6 +74,7 @@ function serializeElement(element) {
7474export function prerenderPlugin ( { prerenderScript, renderTarget, additionalPrerenderRoutes } = { } ) {
7575 let viteConfig = { } ;
7676 let userEnabledSourceMaps ;
77+ let ssrBuild = false ;
7778
7879 /** @type {import('./types.d.ts').PrerenderedRoute[] } */
7980 let routes = [ ] ;
@@ -124,9 +125,19 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
124125 name : 'vite-prerender-plugin' ,
125126 apply : 'build' ,
126127 enforce : 'post' ,
128+ applyToEnvironment ( environment ) {
129+ return environment . name == 'client' ;
130+ } ,
127131 // Vite is pretty inconsistent with how it resolves config options, both
128132 // hooks are needed to set their respective options. ¯\_(ツ)_/¯
129133 config ( config ) {
134+ // Only required for Vite 5 and older. In 6+, this is handled by the
135+ // Environment API (`applyToEnvironment`)
136+ if ( config . build ?. ssr ) {
137+ ssrBuild = true
138+ return ;
139+ }
140+
130141 userEnabledSourceMaps = ! ! config . build ?. sourcemap ;
131142
132143 if ( ! config . customLogger ) {
@@ -161,6 +172,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
161172 config . build . sourcemap = true ;
162173 } ,
163174 configResolved ( config ) {
175+ if ( ssrBuild ) return ;
164176 // We're only going to alter the chunking behavior in the default cases, where the user and/or
165177 // other plugins haven't already configured this. It'd be impossible to avoid breakages otherwise.
166178 if (
@@ -181,7 +193,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
181193 viteConfig = config ;
182194 } ,
183195 async options ( opts ) {
184- if ( ! opts . input ) return ;
196+ if ( ssrBuild || ! opts . input ) return ;
185197 if ( ! prerenderScript ) {
186198 prerenderScript = await getPrerenderScriptFromHTML ( opts . input ) ;
187199 }
@@ -197,6 +209,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
197209 } ,
198210 // Injects window checks into Vite's preload helper & modulepreload polyfill
199211 transform ( code , id ) {
212+ if ( ssrBuild ) return ;
200213 if ( id . includes ( preloadHelperId ) ) {
201214 // Injects a window check into Vite's preload helper, instantly resolving
202215 // the module rather than attempting to add a <link> to the document.
@@ -238,6 +251,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
238251 }
239252 } ,
240253 async generateBundle ( _opts , bundle ) {
254+ if ( ssrBuild ) return ;
241255 // @ts -ignore
242256 globalThis . location = { } ;
243257 // @ts -ignore
0 commit comments