Skip to content

Commit f404808

Browse files
committed
fix: Bail out & skip plugin during Vite's SSR builds
1 parent 14b1db1 commit f404808

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/plugins/prerender-plugin.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function serializeElement(element) {
7474
export 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 = [];
@@ -127,6 +128,11 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
127128
// Vite is pretty inconsistent with how it resolves config options, both
128129
// hooks are needed to set their respective options. ¯\_(ツ)_/¯
129130
config(config) {
131+
if (config.build?.ssr) {
132+
ssrBuild = true
133+
return;
134+
}
135+
130136
userEnabledSourceMaps = !!config.build?.sourcemap;
131137

132138
if (!config.customLogger) {
@@ -161,6 +167,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
161167
config.build.sourcemap = true;
162168
},
163169
configResolved(config) {
170+
if (ssrBuild) return;
164171
// We're only going to alter the chunking behavior in the default cases, where the user and/or
165172
// other plugins haven't already configured this. It'd be impossible to avoid breakages otherwise.
166173
if (
@@ -181,7 +188,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
181188
viteConfig = config;
182189
},
183190
async options(opts) {
184-
if (!opts.input) return;
191+
if (ssrBuild || !opts.input) return;
185192
if (!prerenderScript) {
186193
prerenderScript = await getPrerenderScriptFromHTML(opts.input);
187194
}
@@ -197,6 +204,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
197204
},
198205
// Injects window checks into Vite's preload helper & modulepreload polyfill
199206
transform(code, id) {
207+
if (ssrBuild) return;
200208
if (id.includes(preloadHelperId)) {
201209
// Injects a window check into Vite's preload helper, instantly resolving
202210
// the module rather than attempting to add a <link> to the document.
@@ -238,6 +246,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
238246
}
239247
},
240248
async generateBundle(_opts, bundle) {
249+
if (ssrBuild) return;
241250
// @ts-ignore
242251
globalThis.location = {};
243252
// @ts-ignore

tests/index.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,32 @@ test('Should support custom output filenames', async () => {
102102
assert.match(message, '');
103103
});
104104

105+
test('Should skip plugin during SSR build', async () => {
106+
await loadFixture('simple', env);
107+
await writeConfig(env.tmp.path, `
108+
import { defineConfig } from 'vite';
109+
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
110+
111+
export default defineConfig({
112+
build: {
113+
ssr: true,
114+
rollupOptions: {
115+
input: 'src/index.js'
116+
}
117+
},
118+
plugins: [vitePrerenderPlugin()],
119+
});
120+
`);
121+
await viteBuild(env.tmp.path);
122+
123+
let message = '';
124+
try {
125+
await viteBuild(env.tmp.path);
126+
} catch (error) {
127+
message = error.message;
128+
}
129+
130+
assert.match(message, '');
131+
});
132+
105133
test.run();

0 commit comments

Comments
 (0)