0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/svelte/server-v5.js
Bjorn Lu a600c14837
Support Svelte 5 (experimental) (#9098)
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
2023-11-15 23:40:23 +08:00

42 lines
1.1 KiB
JavaScript

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;
for (const [key, value] of Object.entries(slotted)) {
if (key === 'default') {
children = () => `<${tagName}>${value}</${tagName}>`;
} else {
$$slots ??= {};
$$slots[key] = () => `<${tagName} name="${key}">${value}</${tagName}>`;
}
}
const { html } = render(Component, {
props: {
...props,
children,
$$slots,
},
});
return { html };
}
export default {
check,
renderToStaticMarkup,
supportsAstroStaticSlot: true,
};