Only apply head propagation in trees that need it (#6363)

This commit is contained in:
Matthew Phillips 2023-02-24 17:28:15 -05:00 committed by GitHub
parent c87c16cfad
commit d94aae7765
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes cases where head is injected in body when using Astro.slots.render()

View file

@ -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);
}
}
}
}

View file

@ -0,0 +1,10 @@
---
---
<p>View link tag position</p>
<style>
p {
background: red;
}
</style>

View file

@ -0,0 +1,5 @@
---
const content = await Astro.slots.render('default')
---
<Fragment set:html={content} />

View file

@ -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>

View file

@ -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);
});
});
});
});