Skip to content

Commit

Permalink
Fix slot fallbacks unexpectedly showing up in some cases (#6819)
Browse files Browse the repository at this point in the history
* test: add test fixture

* test: add test case

* test: revert unneeded changes in fixture

* fix

* chore: changeset
  • Loading branch information
MoustaphaDev authored Apr 12, 2023
1 parent bdfbe00 commit 76dd53e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-experts-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix fallback content showing unexpectedly in some cases
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/render/slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function* renderSlot(
yield* iterator;
}

if (fallback) {
if (fallback && !slotted) {
yield* renderSlot(result, fallback);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/astro-slots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('Slots', () => {
const $ = cheerio.load(html);

expect($('#override')).to.have.lengthOf(1);
expect($('#fallback-2').text()).to.equal('Slotty slot.');
});

it('Slots work with multiple elements', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- note: to make testing easier this was inlined -->
<div id="fallback-2"><slot name="override-2">Fallback should only show when no slot has been provided.</slot></div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import Fallback from '../components/Fallback.astro';
import Fallback2 from '../components/Fallback2.astro';
---

<html>
Expand All @@ -11,6 +12,7 @@ import Fallback from '../components/Fallback.astro';
<Fallback>
<div id="override" />
</Fallback>
<Fallback2><div slot="override-2">Slotty slot.</div></Fallback2>
</div>
</body>
</html>

0 comments on commit 76dd53e

Please sign in to comment.