Skip to content

Commit

Permalink
Fix hoisted scripts propagation (#11084)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored May 17, 2024
1 parent 4d32a80 commit 9637014
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/hungry-phones-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes regression when handling hoisted scripts from content collections
7 changes: 6 additions & 1 deletion packages/astro/src/core/build/plugins/plugin-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ export function vitePluginAnalyzer(
if (hoistedScripts.size) {
for (const parentInfo of getParentModuleInfos(from, this, isPropagatedAsset)) {
if (isPropagatedAsset(parentInfo.id)) {
internals.propagatedScriptsMap.set(parentInfo.id, hoistedScripts);
for (const hid of hoistedScripts) {
if (!internals.propagatedScriptsMap.has(parentInfo.id)) {
internals.propagatedScriptsMap.set(parentInfo.id, new Set());
}
internals.propagatedScriptsMap.get(parentInfo.id)?.add(hid);
}
} else if (moduleIsTopLevelPage(parentInfo)) {
for (const hid of hoistedScripts) {
if (!pageScripts.has(parentInfo.id)) {
Expand Down
19 changes: 19 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ describe('Content Collections', () => {
});
});
});

describe('Hoisted scripts', () => {
it('Contains all the scripts imported by components', async () => {
const html = await fixture.readFile('/with-scripts/one/index.html');
const $ = cheerio.load(html);
// NOTE: Hoisted scripts have two tags currently but could be optimized as one. However, we're moving towards
// `experimental.directRenderScript` so this optimization isn't a priority at the moment.
assert.equal($('script').length, 2);
// Read the scripts' content
const scripts = $('script')
.map((_, el) => $(el).attr('src'))
.toArray();
const scriptsCode = (
await Promise.all(scripts.map(async (src) => await fixture.readFile(src)))
).join('\n');
assert.match(scriptsCode, /ScriptCompA/);
assert.match(scriptsCode, /ScriptCompB/);
});
});
});

const blogSlugToContents = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script>console.log('ScriptCompA')</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script>console.log('ScriptCompB')</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ScriptCompA from '../../components/ScriptCompA.astro'
import ScriptCompB from '../../components/ScriptCompB.astro'

Both scripts should exist.

<ScriptCompA />
<ScriptCompB />
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
import { getCollection } from 'astro:content';
export async function getStaticPaths() {
const blogEntries = await getCollection('with-scripts');
return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry },
}));
}
const { entry } = Astro.props;
const { Content } = await entry.render();
const { title } = entry.data;
---

<article>
<h1>This is a content collection post</h1>
<h2>{title}</h2>
<Content />
</article>

0 comments on commit 9637014

Please sign in to comment.