SvelteKit "500" error thrown during prerendering in CI
During vite build with a static adapter, SvelteKit visits each prerenderable route and renders it. If a load function or component throws, the prerenderer reports a 500 for that path and fails the build.
What this error means
The build prints "500 /some/path" with an error message and stack during the prerendering phase, then exits non-zero before the static output is written.
> Building...
500 /blog/post-1
Error: Failed to fetch data: 500
at load (src/routes/blog/[slug]/+page.server.js:8:9)Common causes
A load function fails at build time
A load that calls an API works at runtime but the endpoint is unreachable or returns an error during the prerender, throwing a 500.
Code assumes a request context that prerender lacks
Reading cookies, headers, or a database during prerender (which has no real request) throws because that context does not exist.
How to fix it
Make load functions prerender-safe
- Read the failing path and the thrown error in the build log.
- Ensure data sources are reachable at build time, or skip prerendering for dynamic routes.
- Disable prerender for pages that need live request context.
export const prerender = false; // for routes needing a live requestHandle expected errors instead of throwing 500
Return a controlled error so the prerenderer records a handled status rather than crashing the build.
import { error } from '@sveltejs/kit';
if (!res.ok) throw error(404, 'Not found');How to prevent it
- Keep prerendered routes self-contained or use reachable build-time data.
- Set
prerender = falsefor routes that need live request context. - Test the build, not just the dev server, in CI to catch prerender 500s.