0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/packages/integrations/svelte/server-v5.js
Bjorn Lu 6ad02b5902
Fix Svelte 5 slots change (#11490)
* Fix Svelte 5 slots change

* Use alternative

* Fix Svelte 5 slots
2024-07-18 14:32:39 +01:00

48 lines
1.2 KiB
JavaScript

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