forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-v5.js
57 lines (50 loc) · 1.54 KB
/
server-v5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { createRawSnippet } from 'svelte';
import { render } from 'svelte/server';
function check(Component) {
// Svelte 5 generated components always accept these two props
const str = Component.toString();
return str.includes('$$payload') && str.includes('$$props');
}
function needsHydration(metadata) {
// Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
return metadata.astroStaticSlot ? !!metadata.hydrate : true;
}
async function renderToStaticMarkup(Component, props, slotted, metadata) {
const tagName = needsHydration(metadata) ? 'astro-slot' : 'astro-static-slot';
let children = undefined;
let $$slots = undefined;
const renderProps = {};
for (const [key, value] of Object.entries(slotted)) {
// Legacy slot support
$$slots ??= {};
if (key === 'default') {
$$slots.default = true;
children = createRawSnippet(() => ({
render: () => `<${tagName}>${value}</${tagName}>`,
}));
} else {
$$slots[key] = createRawSnippet(() => ({
render: () => `<${tagName} name="${key}">${value}</${tagName}>`,
}));
}
// @render support for Svelte ^5.0
const slotName = key === 'default' ? 'children' : key;
renderProps[slotName] = createRawSnippet(() => ({
render: () => `<${tagName}${key !== 'default' ? ` name="${key}"` : ''}>${value}</${tagName}>`,
}));
}
const result = render(Component, {
props: {
...props,
children,
$$slots,
...renderProps,
},
});
return { html: result.body };
}
export default {
check,
renderToStaticMarkup,
supportsAstroStaticSlot: true,
};