From d94aae77656f14f56898d33c6d3f83c59112212e Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 24 Feb 2023 17:28:15 -0500 Subject: [PATCH] Only apply head propagation in trees that need it (#6363) --- .changeset/orange-cheetahs-hide.md | 5 +++++ .../src/vite-plugin-head-propagation/index.ts | 8 ++++---- .../components/with-slot-render2/inner.astro | 10 ++++++++++ .../slots-render-outer.astro | 5 +++++ .../src/pages/with-slot-render2.astro | 19 +++++++++++++++++++ packages/astro/test/head-injection.test.js | 8 ++++++++ 6 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 .changeset/orange-cheetahs-hide.md create mode 100644 packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/inner.astro create mode 100644 packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/slots-render-outer.astro create mode 100644 packages/astro/test/fixtures/head-injection/src/pages/with-slot-render2.astro diff --git a/.changeset/orange-cheetahs-hide.md b/.changeset/orange-cheetahs-hide.md new file mode 100644 index 000000000000..29d21173a8ac --- /dev/null +++ b/.changeset/orange-cheetahs-hide.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes cases where head is injected in body when using Astro.slots.render() diff --git a/packages/astro/src/vite-plugin-head-propagation/index.ts b/packages/astro/src/vite-plugin-head-propagation/index.ts index 93f5f5e79b4c..b17d5f382ef7 100644 --- a/packages/astro/src/vite-plugin-head-propagation/index.ts +++ b/packages/astro/src/vite-plugin-head-propagation/index.ts @@ -92,11 +92,11 @@ export function astroHeadPropagationBuildPlugin( for (const [info] of walkParentInfos(id, this)) { appendPropagation(info); } - } - const info = this.getModuleInfo(id); - if (info) { - appendPropagation(info); + const info = this.getModuleInfo(id); + if (info) { + appendPropagation(info); + } } } } diff --git a/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/inner.astro b/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/inner.astro new file mode 100644 index 000000000000..9af3df31d216 --- /dev/null +++ b/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/inner.astro @@ -0,0 +1,10 @@ +--- +--- + +

View link tag position

+ + diff --git a/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/slots-render-outer.astro b/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/slots-render-outer.astro new file mode 100644 index 000000000000..391b360cf6ca --- /dev/null +++ b/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/slots-render-outer.astro @@ -0,0 +1,5 @@ +--- +const content = await Astro.slots.render('default') +--- + + diff --git a/packages/astro/test/fixtures/head-injection/src/pages/with-slot-render2.astro b/packages/astro/test/fixtures/head-injection/src/pages/with-slot-render2.astro new file mode 100644 index 000000000000..316416a0c660 --- /dev/null +++ b/packages/astro/test/fixtures/head-injection/src/pages/with-slot-render2.astro @@ -0,0 +1,19 @@ +--- +import Inner from '../components/with-slot-render2/inner.astro' +import SlotsRenderOuter from '../components/with-slot-render2/slots-render-outer.astro' +--- + + + + + + + + Astro + + + + + + + diff --git a/packages/astro/test/head-injection.test.js b/packages/astro/test/head-injection.test.js index d86272712c89..a2c0389df165 100644 --- a/packages/astro/test/head-injection.test.js +++ b/packages/astro/test/head-injection.test.js @@ -58,6 +58,14 @@ describe('Head injection', () => { expect($('head link[rel=stylesheet]')).to.have.a.lengthOf(2); expect($('body link[rel=stylesheet]')).to.have.a.lengthOf(0); }); + + it('Using slots with Astro.slots.render() (layout)', async () => { + const html = await fixture.readFile('/with-slot-render2/index.html'); + const $ = cheerio.load(html); + + expect($('head link[rel=stylesheet]')).to.have.a.lengthOf(1); + expect($('body link[rel=stylesheet]')).to.have.a.lengthOf(0); + }); }); }); });