From 8f88198287c9618768bbe3b98496223147572de6 Mon Sep 17 00:00:00 2001 From: cyphercodes Date: Wed, 29 Apr 2026 01:56:51 +0300 Subject: [PATCH] fix(slots): unwrap conditional slot callbacks --- .changeset/tender-slots-unwrap.md | 5 +++++ packages/astro/src/core/render/slots.ts | 11 +++++++++-- packages/astro/test/astro-slots.test.ts | 7 +++++++ .../src/components/ConditionalSlottedCallback.astro | 8 ++++++++ .../src/pages/conditional-slotted-callback.astro | 13 +++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 .changeset/tender-slots-unwrap.md create mode 100644 packages/astro/test/fixtures/astro-slots/src/components/ConditionalSlottedCallback.astro create mode 100644 packages/astro/test/fixtures/astro-slots/src/pages/conditional-slotted-callback.astro diff --git a/.changeset/tender-slots-unwrap.md b/.changeset/tender-slots-unwrap.md new file mode 100644 index 000000000000..24bb45545a12 --- /dev/null +++ b/.changeset/tender-slots-unwrap.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix conditional named slot callbacks receiving arguments from `Astro.slots.render()`. diff --git a/packages/astro/src/core/render/slots.ts b/packages/astro/src/core/render/slots.ts index 1d8abf67d66c..4234e6fb78fc 100644 --- a/packages/astro/src/core/render/slots.ts +++ b/packages/astro/src/core/render/slots.ts @@ -1,5 +1,6 @@ import { type ComponentSlots, renderSlotToString } from '../../runtime/server/index.js'; import { renderJSX } from '../../runtime/server/jsx.js'; +import { isRenderTemplateResult } from '../../runtime/server/render/astro/index.js'; import { chunkToString } from '../../runtime/server/render/index.js'; import { isRenderInstruction } from '../../runtime/server/render/instruction.js'; import type { SSRResult } from '../../types/public/internal.js'; @@ -8,9 +9,15 @@ import type { AstroLogger } from '../logger/core.js'; function getFunctionExpression(slot: any) { if (!slot) return; - const expressions = slot?.expressions?.filter((e: unknown) => isRenderInstruction(e) === false); + const expressions = slot?.expressions?.filter( + (e: unknown) => isRenderInstruction(e) === false || isRenderTemplateResult(e), + ); if (expressions?.length !== 1) return; - return expressions[0] as (...args: any[]) => any; + const expression = expressions[0]; + if (isRenderTemplateResult(expression)) { + return getFunctionExpression(expression); + } + return expression as (...args: any[]) => any; } export class Slots { diff --git a/packages/astro/test/astro-slots.test.ts b/packages/astro/test/astro-slots.test.ts index b50fb3ce9a66..2c140ae66804 100644 --- a/packages/astro/test/astro-slots.test.ts +++ b/packages/astro/test/astro-slots.test.ts @@ -182,6 +182,13 @@ describe('Slots', () => { assert.deepEqual(afterDiv.text(), 'Test Content AFTER'); }); + it('Arguments can be passed to conditional named slots with Astro.slots.render()', async () => { + const html = await fixture.readFile('/conditional-slotted-callback/index.html'); + const $ = cheerio.load(html); + + assert.equal($('#conditional-block').text(), 'Block: block'); + }); + it('Unused slot builds without error', async () => { const html = await fixture.readFile('/unused-slot/index.html'); const $ = cheerio.load(html); diff --git a/packages/astro/test/fixtures/astro-slots/src/components/ConditionalSlottedCallback.astro b/packages/astro/test/fixtures/astro-slots/src/components/ConditionalSlottedCallback.astro new file mode 100644 index 000000000000..acc631cca546 --- /dev/null +++ b/packages/astro/test/fixtures/astro-slots/src/components/ConditionalSlottedCallback.astro @@ -0,0 +1,8 @@ +--- +let html = ''; +if (Astro.slots.has('block')) { + html = await Astro.slots.render('block', [{ test: 'block' }]); +} +--- + + diff --git a/packages/astro/test/fixtures/astro-slots/src/pages/conditional-slotted-callback.astro b/packages/astro/test/fixtures/astro-slots/src/pages/conditional-slotted-callback.astro new file mode 100644 index 000000000000..5dc1bc158638 --- /dev/null +++ b/packages/astro/test/fixtures/astro-slots/src/pages/conditional-slotted-callback.astro @@ -0,0 +1,13 @@ +--- +import ConditionalSlottedCallback from '../components/ConditionalSlottedCallback.astro'; +--- + + + { + true && ( + + {({ test }) =>
Block: {test}
} +
+ ) + } +