From ba20c718a4ccd1009bdf81f8265956bff1d19d05 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 4 Jun 2024 10:52:48 -0400 Subject: [PATCH] Retain client scripts in content cache (#11170) * Retain client scripts in content cache * Add test --- .changeset/wicked-dragons-draw.md | 5 +++++ .../src/core/build/plugins/plugin-content.ts | 1 + ...xperimental-content-collections-render.test.js | 15 +++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 .changeset/wicked-dragons-draw.md diff --git a/.changeset/wicked-dragons-draw.md b/.changeset/wicked-dragons-draw.md new file mode 100644 index 0000000000..d93036f11f --- /dev/null +++ b/.changeset/wicked-dragons-draw.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Retain client scripts in content cache diff --git a/packages/astro/src/core/build/plugins/plugin-content.ts b/packages/astro/src/core/build/plugins/plugin-content.ts index ae6fff8067..1698eb2a0c 100644 --- a/packages/astro/src/core/build/plugins/plugin-content.ts +++ b/packages/astro/src/core/build/plugins/plugin-content.ts @@ -255,6 +255,7 @@ function vitePluginContent( ...oldManifest.clientEntries, ...internals.discoveredHydratedComponents.keys(), ...internals.discoveredClientOnlyComponents.keys(), + ...internals.discoveredScripts ]); // Likewise, these are server modules that might not be referenced // once the cached items are excluded from the build process diff --git a/packages/astro/test/experimental-content-collections-render.test.js b/packages/astro/test/experimental-content-collections-render.test.js index ffe0a0290f..e5d152f410 100644 --- a/packages/astro/test/experimental-content-collections-render.test.js +++ b/packages/astro/test/experimental-content-collections-render.test.js @@ -136,6 +136,21 @@ if (!isWindows) { const files = await fixture.readdir(''); assert.equal(files.includes('chunks'), false, 'chunks folder removed'); }); + + it('hoisted script is built', async () => { + const html = await fixture.readFile('/launch-week-component-scripts/index.html'); + const $ = cheerio.load(html); + + const allScripts = $('head > script[type="module"]'); + assert.ok(allScripts.length > 0); + + // Includes hoisted script + assert.notEqual( + [...allScripts].find((script) => $(script).attr('src')?.includes('/_astro/WithScripts')), + undefined, + 'hoisted script missing from head.' + ); + }); }); }); });