2.4 KiB
2.4 KiB
Research: SvelteKit Integration
Decision: SvelteKit SPA with FastAPI Backend
Rationale
SvelteKit provides a robust file-based routing system and shared layout mechanism that fulfills the requirements (FR-001, FR-002, FR-004, FR-005). By using adapter-static in SPA mode, we can generate a set of static files that can be served by the existing FastAPI backend, satisfying the constraint of no Node.js server in production (FR-003, Assumptions).
Alternatives Considered
- Vanilla Svelte (Current): Lacks built-in routing and layout management, leading to manual implementation overhead.
- SvelteKit with Node.js Server: Rejected because the project requires the Python backend to be the primary server.
- Inertia.js: Requires more tight coupling between backend and frontend than desired for this project.
Technical Implementation Details
SvelteKit Configuration (SPA Mode)
- Adapter: Use
@sveltejs/adapter-static. - Fallback: Configure
fallback: 'index.html'insvelte.config.js. - Client-Side Rendering: Create
src/routes/+layout.tswith:This ensures the entire app is treated as a SPA.export const ssr = false; export const prerender = false;
FastAPI Backend Integration
- Static Files: Mount the
frontend/build(ordist) directory usingStaticFiles. - SPA Routing: Implement a catch-all route to serve
index.htmlfor any non-API request. This allows SvelteKit's client-side router to handle deep links like/settings.@app.get("/{full_path:path}") async def serve_spa(full_path: str): # Check if path exists in static files, else serve index.html ...
Migration Strategy
- Layout: Move shared UI (header, footer) from
App.sveltetosrc/routes/+layout.svelte. - Routes:
Dashboard.svelte->src/routes/+page.svelte(orsrc/routes/dashboard/+page.svelte)Settings.svelte->src/routes/settings/+page.svelte
- API Client: Reuse existing
frontend/src/lib/api.jsbut ensure it works within SvelteKit's load functions if needed (though for pure SPA, standardonMountor reactive statements also work).
Best Practices
- Use SvelteKit's
$libalias for shared components and utilities. - Leverage
+page.tsloadfunctions for data fetching to ensure data is ready before component mount (User Story 2). - Use SvelteKit's
gotofor programmatic navigation.