Here is the situation: I have a big Svelte component with a lot of html and static code. And inside of it, inside a section for example, I have some code that needs to be hydrated. In order to keep the code where it should be while only hydrating it and not the whole component, can I just make a separate small component with just what needs to be hydrated, then use <slot /> inside the big component where the small hydrated one should go?
#I want to hydrate a small section of a big component
5 messages · Page 1 of 1 (latest)
Hi @winged phoenix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
I want to hydrate a small section of a big component
Yes, your approach is correct and aligns with Astro’s recommended patterns.
To hydrate only a small section inside a larger (mostly static) Svelte component, you should:
- Extract the interactive part into its own small Svelte component.
- In your Astro page or parent component, place the big static Svelte component as usual, but use a
<slot />or a named slot in the big component’s template where you want the interactive part to appear. - Pass the small, interactive Svelte component as a child to the big component, using a client directive (like
client:load,client:idle, etc.) to hydrate only that part.
However, it’s important to note that Astro’s slot system works in .astro components, not inside Svelte components. You cannot use <slot /> inside a .svelte file to receive children from Astro; Svelte’s <slot /> is only for use within Svelte’s own component system.
Recommended Astro pattern:
- Use an
.astrocomponent as the parent layout, and place your big static Svelte component and the small hydrated Svelte component inside it. For example:
---
// In a .astro file
import BigStaticSection from '../components/BigStaticSection.svelte';
import SmallHydratedWidget from '../components/SmallHydratedWidget.svelte';
---
<BigStaticSection>
<SmallHydratedWidget client:load slot="interactive-section" />
</BigStaticSection>
- In your
BigStaticSection.svelte, use Svelte’s<slot name="interactive-section" />where you want the interactive part to appear.
This way, only the SmallHydratedWidget will be hydrated, and the rest of your big component remains static. This approach is supported and recommended in Astro for mixing static and interactive content efficiently. For more details, see the section on passing children to framework components and islands architecture.