Skip to content
Latchkey

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.

svelte-kit
> 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

  1. Read the failing path and the thrown error in the build log.
  2. Ensure data sources are reachable at build time, or skip prerendering for dynamic routes.
  3. Disable prerender for pages that need live request context.
+page.server.js
export const prerender = false; // for routes needing a live request

Handle expected errors instead of throwing 500

Return a controlled error so the prerenderer records a handled status rather than crashing the build.

+page.server.js
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 = false for routes that need live request context.
  • Test the build, not just the dev server, in CI to catch prerender 500s.

Frequently asked questions

What causes ""500" during prerendering"?
A load that calls an API works at runtime but the endpoint is unreachable or returns an error during the prerender, throwing a 500.
How do I fix "500" during prerendering?
Make load functions prerender-safe

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card