Skip to content

Commit d9dfd3c

Browse files
authored
fix: Ensure prerender script isn't merged into entry (#40)
1 parent bcb028f commit d9dfd3c

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/plugins/prerender-plugin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function serializeElement(element) {
7272
* @returns {import('vite').Plugin}
7373
*/
7474
export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrerenderRoutes } = {}) {
75+
const originalPrerenderScript = prerenderScript;
7576
let viteConfig = {};
7677
let userEnabledSourceMaps;
7778
let ssrBuild = false;
@@ -195,7 +196,12 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
195196

196197
config.build.rollupOptions.output ??= {};
197198
config.build.rollupOptions.output.manualChunks = (id) => {
198-
if (id.includes(prerenderScript) || id.includes(preloadPolyfillId)) {
199+
// If the user has specified a prerender script via the plugin options, we don't
200+
// want to merge it into the index, but if they're using `<script prerender>` in their
201+
// HTML, then we do.
202+
//
203+
// As such, we grab & store the original value & check against it here.
204+
if ((!originalPrerenderScript && id.includes(prerenderScript)) || id.includes(preloadPolyfillId)) {
199205
return 'index';
200206
}
201207
};

tests/config.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { test } from 'uvu';
22
import * as assert from 'uvu/assert';
3+
import path from 'node:path';
4+
import { promises as fs } from 'node:fs';
35

46
import { setupTest, teardownTest, loadFixture, viteBuild } from './lib/lifecycle.js';
57
import { getOutputFile, outputFileExists, writeFixtureFile } from './lib/utils.js';
@@ -30,6 +32,12 @@ test('Should support the `prerenderScript` plugin option', async () => {
3032

3133
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
3234
assert.match(prerenderedHtml, '<body><h1>Hello, World!</h1>');
35+
36+
const outDirAssets = await fs.readdir(path.join(env.tmp.path, 'dist', 'assets'));
37+
38+
assert.equal(outDirAssets.length, 2);
39+
assert.ok(outDirAssets.some(asset => /^index-/.test(asset)))
40+
assert.ok(outDirAssets.some(asset => /^prerender-/.test(asset)))
3341
});
3442

3543
test('Should throw if no `prerenderScript` is specified or can be found', async () => {

0 commit comments

Comments
 (0)