Use Svelte components within Astro with server-side rendering and client-side hydration
npx @tessl/cli install tessl/npm-astrojs--svelte@7.1.0@astrojs/svelte is an Astro integration that enables server-side rendering and client-side hydration for Svelte components. It supports Svelte 3, 4, and 5 with automatic component detection, slot handling (both legacy $$slots and new @render syntax), and TypeScript editor support.
npm install @astrojs/svelte svelteimport svelteIntegration, { getContainerRenderer, vitePreprocess } from "@astrojs/svelte";For editor utilities:
import { toTSX } from "@astrojs/svelte/editor";// astro.config.mjs
import { defineConfig } from 'astro/config';
import svelte from '@astrojs/svelte';
export default defineConfig({
integrations: [
svelte({
// Optional Svelte plugin options
preprocess: [],
compilerOptions: {},
})
]
});@astrojs/svelte consists of several key components:
Core integration function that configures Astro to use Svelte components with Vite plugin setup and optimization configuration.
function svelteIntegration(options?: Options): AstroIntegration;
interface AstroIntegration {
name: string;
hooks: {
'astro:config:setup': (params: ConfigSetupParams) => Promise<void>;
};
}Server-side rendering engine that converts Svelte components to static HTML with support for slots, props, and metadata.
interface NamedSSRLoadedRendererValue {
name: string;
check: (Component: any) => boolean;
renderToStaticMarkup: (
this: RendererContext,
Component: any,
props: Record<string, any>,
slotted: Record<string, any>,
metadata?: AstroComponentMetadata
) => Promise<{ html: string }>;
supportsAstroStaticSlot: boolean;
}Client-side hydration system for making server-rendered components interactive with state management and event handling.
declare const clientRenderer: (element: HTMLElement) => (
Component: any,
props: Record<string, any>,
slotted: Record<string, any>,
options: Record<string, string>
) => Promise<void>;
export default clientRenderer;Container renderer for server-side rendering in isolated contexts like Astro's experimental container API.
function getContainerRenderer(): ContainerRenderer;
interface ContainerRenderer {
name: string;
serverEntrypoint: string;
}TypeScript transformation utilities for converting Svelte code to TypeScript JSX for editor tooling and language services.
function toTSX(code: string, className: string): string;Re-exported Vite preprocessor for Svelte files, providing build-time transformations.
const vitePreprocess: Preprocessor;
type Preprocessor = import('@sveltejs/vite-plugin-svelte').Preprocessor;interface RendererContext {
result: SSRResult;
}
interface AstroComponentMetadata {
hydrate?: string;
astroStaticSlot?: boolean;
}
interface ConfigSetupParams {
updateConfig: (config: AstroUserConfig) => void;
addRenderer: (renderer: AstroRenderer) => void;
}
interface AstroRenderer {
name: string;
clientEntrypoint: string;
serverEntrypoint: string;
}
type Options = import('@sveltejs/vite-plugin-svelte').Options;
type AstroUserConfig = import('astro').AstroUserConfig;
type SSRResult = import('astro').SSRResult;
type Preprocessor = import('@sveltejs/vite-plugin-svelte').Preprocessor;