Skip to content

Commit

Permalink
Only apply head propagation in trees that need it (#6363)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Feb 24, 2023
1 parent c87c16c commit d94aae7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-cheetahs-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes cases where head is injected in body when using Astro.slots.render()
8 changes: 4 additions & 4 deletions packages/astro/src/vite-plugin-head-propagation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---

<p>View link tag position</p>

<style>
p {
background: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
const content = await Astro.slots.render('default')
---

<Fragment set:html={content} />
Original file line number Diff line number Diff line change
@@ -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>
8 changes: 8 additions & 0 deletions packages/astro/test/head-injection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit d94aae7

Please sign in to comment.