Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-fragment-named-slot-style-propagation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes scoped styles from `.astro` components being dropped when rendered inside MDX content (`<Content />` from `render(entry)`) passed through a named slot using `<Fragment slot="X">`. The Fragment component now eagerly evaluates its slot contents to ensure propagating components register their styles before head content is flushed.
12 changes: 9 additions & 3 deletions packages/astro/src/runtime/server/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { componentIsHTMLElement, renderHTMLElement } from './dom.js';
import { maybeRenderHead } from './head.js';
import { createRenderInstruction } from './instruction.js';
import { containsServerDirective, ServerIslandComponent } from './server-islands.js';
import { type ComponentSlots, renderSlot, renderSlots, renderSlotToString } from './slot.js';
import { renderChild } from './any.js';
import { type ComponentSlots, renderSlots, renderSlotToString } from './slot.js';
import { formatList, internalSpreadAttributes, renderElement, voidElementNames } from './util.js';

const needsHeadRenderingSymbol = Symbol.for('astro.needsHeadRendering');
Expand Down Expand Up @@ -416,10 +417,15 @@ function sanitizeElementName(tag: string) {

function renderFragmentComponent(result: SSRResult, slots: ComponentSlots = {}): RenderInstance {
const slot = slots?.default;
// Eagerly evaluate the slot to trigger creation of nested component instances
// (and their propagator registration for head content like styles/scripts).
// Without this, propagating components (e.g. Content from render()) nested inside
// a Fragment named slot won't register before bufferHeadContent() runs.
const preRendered = slot?.(result);
return {
render(destination) {
if (slot == null) return;
return renderSlot(result, slot).render(destination);
if (preRendered == null) return;
return renderChild(destination, preRendered);
},
};
}
Expand Down
47 changes: 47 additions & 0 deletions packages/astro/test/units/render/head-injection-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FetchState } from '../../../dist/core/fetch/fetch-state.js';
import {
createComponent,
createHeadAndContent,
Fragment,
maybeRenderHead,
render,
renderComponent,
Expand Down Expand Up @@ -178,4 +179,50 @@ describe('head injection app-level rendering', () => {
assert.equal($('body link[rel="stylesheet"]').length, 0);
assert.equal($('p').text(), 'Paragraph.');
});

it('propagates head from Content inside Fragment named slot (#16181)', async () => {
// Simulates: MDX Content component (propagation: 'self') rendered through
// <Wrapper><Fragment slot="content"><Content /></Fragment></Wrapper>
// This is the pattern from the johnnydecimal reproduction.
const ContentInner = createComponent(() => render`<div class="box">styled content</div>`);
const ContentEntry = createComponent({
factory(result: any) {
const style = '<style>.box{color:hotpink}</style>';
return createHeadAndContent(
unescapeHTML(style) as unknown as string,
render`${renderComponent(result, 'ContentInner', ContentInner, {})}`,
);
},
propagation: 'self',
});

const Wrapper = createComponent(
(result: any, _props: any, slots: any) =>
render`<div>${renderSlot(result, slots['content'])}</div>`,
);

// This mirrors what the Astro compiler generates for:
// <Wrapper><Fragment slot="content"><Content /></Fragment></Wrapper>
const Page = createComponent(
(result: any) =>
render`<html><head>${renderHead()}</head><body>${renderComponent(
result,
'Wrapper',
Wrapper,
{},
{
content: (result2: any) =>
render`${renderComponent(result2, 'Fragment', Fragment, { slot: 'content' }, {
default: (result3: any) =>
render` ${renderComponent(result3, 'ContentEntry', ContentEntry, {})} `,
})}`,
},
)}</body></html>`,
);

const $ = await renderPage(Page);
assert.equal($('head style').length, 1);
assert.equal($('head style').text(), '.box{color:hotpink}');
assert.equal($('.box').length, 1);
});
});