summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2023-02-24 17:28:15 -0500
committerGravatar GitHub <noreply@github.com> 2023-02-24 17:28:15 -0500
commitd94aae77656f14f56898d33c6d3f83c59112212e (patch)
tree6499d59de9074ab00b929d616e604e4bbc2dbf3f
parentc87c16cfaddea3a05af87c3258d57ef1a31516f7 (diff)
downloadastro-d94aae77656f14f56898d33c6d3f83c59112212e.tar.gz
astro-d94aae77656f14f56898d33c6d3f83c59112212e.tar.zst
astro-d94aae77656f14f56898d33c6d3f83c59112212e.zip
Only apply head propagation in trees that need it (#6363)
-rw-r--r--.changeset/orange-cheetahs-hide.md5
-rw-r--r--packages/astro/src/vite-plugin-head-propagation/index.ts8
-rw-r--r--packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/inner.astro10
-rw-r--r--packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/slots-render-outer.astro5
-rw-r--r--packages/astro/test/fixtures/head-injection/src/pages/with-slot-render2.astro19
-rw-r--r--packages/astro/test/head-injection.test.js8
6 files changed, 51 insertions, 4 deletions
diff --git a/.changeset/orange-cheetahs-hide.md b/.changeset/orange-cheetahs-hide.md
new file mode 100644
index 000000000..29d21173a
--- /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 93f5f5e79..b17d5f382 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 000000000..9af3df31d
--- /dev/null
+++ b/packages/astro/test/fixtures/head-injection/src/components/with-slot-render2/inner.astro
@@ -0,0 +1,10 @@
+---
+---
+
+<p>View link tag position</p>
+
+<style>
+ p {
+ background: red;
+ }
+</style>
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 000000000..391b360cf
--- /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')
+---
+
+<Fragment set:html={content} />
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 000000000..316416a0c
--- /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'
+---
+
+<html lang='en'>
+ <head>
+ <meta charset='utf-8' />
+ <link rel='icon' type='image/svg+xml' href='/favicon.svg' />
+ <meta name='viewport' content='width=device-width' />
+ <meta name='generator' content={Astro.generator} />
+ <title>Astro</title>
+ </head>
+ <body>
+ <SlotsRenderOuter>
+ <Inner />
+ </SlotsRenderOuter>
+ </body>
+</html>
diff --git a/packages/astro/test/head-injection.test.js b/packages/astro/test/head-injection.test.js
index d86272712..a2c0389df 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);
+ });
});
});
});