========== SvelteKit ========== SvelteKit integration with Litestar Vite for full-stack Svelte applications. Quick Start ----------- .. code-block:: bash litestar assets init --template sveltekit This creates a SvelteKit project with TypeScript support and SSR capabilities. Project Structure ----------------- SvelteKit applications use a specialized Vite plugin: .. code-block:: text my-app/ ├── app.py # Litestar backend ├── package.json ├── vite.config.ts # Vite + SvelteKit plugin ├── svelte.config.js # SvelteKit configuration ├── tsconfig.json ├── src/ │ ├── app.html # HTML template │ ├── routes/ │ │ └── +page.svelte # Pages (file-based routing) │ └── lib/ │ └── api/ # Generated types from OpenAPI └── build/ # SvelteKit build output Backend Setup ------------- .. literalinclude:: /../examples/sveltekit/app.py :language: python :caption: examples/sveltekit/app.py Key points: - ``mode="framework"`` enables meta-framework integration mode (aliases: ``mode="ssr"`` / ``mode="ssg"``) - ``ExternalDevServer`` delegates dev server to SvelteKit - ``TypeGenConfig`` enables type generation for SvelteKit Vite Configuration ------------------ .. literalinclude:: /../examples/sveltekit/vite.config.ts :language: typescript :caption: vite.config.ts The ``litestarSvelteKit`` plugin must come **before** the ``sveltekit()`` plugin. Configuration options: - ``apiProxy`` - URL of Litestar backend (default: ``http://localhost:8000``) - ``apiPrefix`` - API route prefix to proxy (default: ``/api``) - ``types`` - Enable type generation (reads from ``.litestar.json`` or explicit config) API Integration --------------- Using Load Functions ~~~~~~~~~~~~~~~~~~~~ SvelteKit's ``load`` functions are perfect for SSR data fetching: .. code-block:: typescript // src/routes/users/+page.ts import type { PageLoad } from './$types' import type { User } from '$lib/api/types.gen' import { route } from '$lib/api/routes' export const load: PageLoad = async ({ fetch }) => { const response = await fetch(route('users:list')) const users: User[] = await response.json() return { users } } .. code-block:: svelte