server-v5.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { render } from 'svelte/server';
  2. import { add_snippet_symbol } from 'svelte/internal';
  3. // Allow a slot to be rendered as a snippet (dev validation only)
  4. const tagSlotAsSnippet = import.meta.env.DEV ? add_snippet_symbol : (s) => s;
  5. function check(Component) {
  6. // Svelte 5 generated components always accept these two props
  7. const str = Component.toString();
  8. return str.includes('$$payload') && str.includes('$$props');
  9. }
  10. function needsHydration(metadata) {
  11. // Adjust how this is hydrated only when the version of Astro supports `astroStaticSlot`
  12. return metadata.astroStaticSlot ? !!metadata.hydrate : true;
  13. }
  14. async function renderToStaticMarkup(Component, props, slotted, metadata) {
  15. const tagName = needsHydration(metadata) ? 'astro-slot' : 'astro-static-slot';
  16. let children = undefined;
  17. let $$slots = undefined;
  18. for (const [key, value] of Object.entries(slotted)) {
  19. if (key === 'default') {
  20. children = tagSlotAsSnippet(() => `<${tagName}>${value}</${tagName}>`);
  21. } else {
  22. $$slots ??= {};
  23. $$slots[key] = tagSlotAsSnippet(() => `<${tagName} name="${key}">${value}</${tagName}>`);
  24. }
  25. }
  26. const { html } = render(Component, {
  27. props: {
  28. ...props,
  29. children,
  30. $$slots,
  31. },
  32. });
  33. return { html };
  34. }
  35. export default {
  36. check,
  37. renderToStaticMarkup,
  38. supportsAstroStaticSlot: true,
  39. };