Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/runtime/client-hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,12 +697,20 @@ const addSlottedNodes = (

// Loop through the next siblings of the slot node, looking for nodes that match this slot's name
do {
const slotAttribute = slottedNode?.['getAttribute'] ? slottedNode.getAttribute('slot') : null;
const isSlotFallback = slottedNode?.nodeName === 'SLOT-FB';
const ssrSlotName =
slottedNode && slottedNode.nodeType === NODE_TYPE.ElementNode
? (slottedNode as Element).getAttribute('s-sn')
: null;
if (
slottedNode &&
(((slottedNode['getAttribute'] && slottedNode.getAttribute('slot')) || slottedNode['s-sn']) === slotName ||
!isSlotFallback &&
((slotAttribute ?? slottedNode['s-sn']) === slotName ||
(slotAttribute === null && ssrSlotName === slotName) ||
(slotName === '' &&
!slottedNode['s-sn'] &&
(!slottedNode['getAttribute'] || !slottedNode.getAttribute('slot')) &&
slotAttribute === null &&
(slottedNode.nodeType === NODE_TYPE.CommentNode || slottedNode.nodeType === NODE_TYPE.TextNode)))
) {
// Looking for nodes that match this slot's name,
Expand Down
82 changes: 82 additions & 0 deletions src/runtime/test/client-hydrate-to-vdom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { newSpecPage } from '@stencil/core/testing';

import type * as d from '../../declarations';
import { initializeClientHydrate } from '../client-hydrate';
import { renderVdom } from '../vdom/vdom-render';

describe('initializeClientHydrate', () => {
it('functional', async () => {
Expand Down Expand Up @@ -83,4 +84,85 @@ describe('initializeClientHydrate', () => {
expect(cmpAvnode.$children$).toHaveLength(1);
expect(cmpAvnode.$children$[0].$text$.trim()).toBe('88mph');
});

// Regression test for https://github.com/stenciljs/core/issues/6934.
it('collects SSR content in a nested non-shadow default slot before the owning host hydrates', async () => {
@Component({
tag: 'cmp-wrap',
shadow: false,
})
class CmpWrap {
render() {
return (
<Host>
<slot />
</Host>
);
}
}

@Component({
tag: 'cmp-page',
shadow: false,
})
class CmpPage {
render() {
return (
<cmp-wrap>
<div class="inner">Rendered by cmp-page</div>
</cmp-wrap>
);
}
}

const serverHydrated = await newSpecPage({
components: [CmpPage, CmpWrap],
html: `<cmp-page></cmp-page>`,
hydrateServerSide: true,
});
const container = document.createElement('div');
container.innerHTML = serverHydrated.root.outerHTML;

const wrapElm = container.querySelector('cmp-wrap') as d.HostElement;
const hostRef: d.HostRef = {
$flags$: 0,
$cmpMeta$: CmpWrap.COMPILER_META,
$hostElement$: wrapElm,
};
initializeClientHydrate(wrapElm, 'cmp-wrap', '2', hostRef);
renderVdom(hostRef, h('slot', null), true);

const inner = wrapElm.querySelector('.inner');
expect(inner?.hasAttribute('hidden')).toBe(false);
});

it('does not collect a serialized slot fallback from a foreign host', () => {
const hostElm = document.createElement('cmp-host') as d.HostElement;
hostElm.innerHTML = `
<!--r.1-->
<!--s.1.0.0.0.named-->
<slot-fb c-id="2.0.0.0" s-sn="named"></slot-fb>
`;
const foreignFallback = hostElm.querySelector('slot-fb') as d.RenderNode;
const hostRef: d.HostRef = { $flags$: 0 };

initializeClientHydrate(hostElm, 'cmp-host', '1', hostRef);

expect(foreignFallback['s-sn']).toBeUndefined();
});

it('does not let a serialized slot name override an explicit slot attribute', () => {
const hostElm = document.createElement('cmp-host') as d.HostElement;
hostElm.innerHTML = `
<!--r.1-->
<!--s.1.0.0.0.named-->
<div c-id="2.0.0.0" s-sn="named" slot="explicit"></div>
`;
const explicitlySlotted = hostElm.querySelector('div') as d.RenderNode;
const hostRef: d.HostRef = { $flags$: 0 };

initializeClientHydrate(hostElm, 'cmp-host', '1', hostRef);

expect(explicitlySlotted['s-sn']).toBeUndefined();
});
});