Skip to content

Commit d99c883

Browse files
committed
fix: Ensure node-html-parser doesn't strip comments
1 parent db39db0 commit d99c883

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

src/plugins/prerender-plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
234234
const tpl = /** @type {string} */ (
235235
/** @type {OutputAsset} */ (bundle['index.html']).source
236236
);
237-
let htmlDoc = htmlParse(tpl);
237+
let htmlDoc = htmlParse(tpl, { comment: true });
238238

239239
// Create a tmp dir to allow importing & consuming the built modules,
240240
// before Rollup writes them to the disk
@@ -377,7 +377,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
377377
}
378378

379379
// Reset HTML doc & head data
380-
htmlDoc = htmlParse(tpl);
380+
htmlDoc = htmlParse(tpl, { comment: true });
381381
head = { lang: '', title: '', elements: new Set() };
382382

383383
// Add any discovered links to the list of routes to pre-render:

tests/fixtures/comments/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
</head>
6+
<body>
7+
<!-- With Input HTML Comment -->
8+
<script prerender type="module" src="/src/index.js"></script>
9+
</body>
10+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function prerender() {
2+
return '<h1>Simple Test Result <!-- With Output HTML Comment --></h1>';
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from 'vite';
2+
import { vitePrerenderPlugin } from 'vite-prerender-plugin';
3+
4+
export default defineConfig({
5+
plugins: [vitePrerenderPlugin()],
6+
});

tests/index.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,13 @@ test('Should bail on merging preload & entry chunks if user configures `manualCh
6464
assert.equal((await fs.readdir(outDirAssets)).length, 2);
6565
});
6666

67+
test('Should support comment nodes in returned HTML', async () => {
68+
await loadFixture('comments', env);
69+
await viteBuild(env.tmp.path);
70+
71+
const prerenderedHtml = await getOutputFile(env.tmp.path, 'index.html');
72+
assert.match(prerenderedHtml, '<h1>Simple Test Result <!-- With Output HTML Comment --></h1>');
73+
assert.match(prerenderedHtml, '<!-- With Input HTML Comment -->');
74+
});
75+
6776
test.run();

0 commit comments

Comments
 (0)