sveltejs/kit · error · Error
Cannot call `${name}${parens}` on the server
Error message
Cannot call `${name}${parens}` on the server What it means
Several $app/navigation APIs (goto, invalidate, invalidateAll, refreshAll, applyAction, disableScrollHandling) only make sense in the browser. When SvelteKit's module is loaded on the server, those functions are stubs that throw this error, so developers get a clear message instead of confusing server-side behavior.
Source
Thrown at packages/kit/src/utils/functions.js:26
let done = false;
/** @type T */
let result;
return () => {
if (done) return result;
done = true;
return (result = fn());
};
}
/**
* @param {string} name
* @param {string} [parens]
*/
export function disallow_on_server(name, parens = '(...)') {
return () => {
throw new Error(`Cannot call \`${name}${parens}\` on the server`);
};
}
View on GitHub (pinned to 03f1687fe6)
Solutions
- Wrap the call in `if (browser)` from '$app/environment'
- Move the call into onMount or an event handler that only runs client-side
- For actions, use redirect/flash/state instead of calling goto during SSR
- In load, use `depends`/await parent patterns instead of invalidate on the server
Example fix
// before
import { goto } from '$app/navigation';
export function load() { goto('/dashboard'); }
// after
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
export function load() {
if (browser) goto('/dashboard');
return redirect(302, '/dashboard'); // server-safe alternative
} Defensive patterns
Strategy: try-catch
Validate before calling
import { browser } from '$app/environment';
// call navigation APIs only when browser === true Type guard
const canNavigate = () => typeof window !== 'undefined' && typeof document !== 'undefined';
Try / catch
import { goto } from '$app/navigation';
try {
goto('/path');
} catch (e) {
if (/Cannot call `goto` on the server/.test(e.message)) {
// SSR context — defer to client
} else throw e;
} Prevention
- Guard navigation calls with `if (browser)` from '$app/environment'
- Prefer redirect()/error() in load functions over goto during SSR
- Call navigation APIs only from event handlers, effects, or onMount
- Never import navigation helpers into .server files or shared server code
When it happens
Trigger: Calling goto/invalidate/invalidateAll/refreshAll/applyAction/disableScrollHandling inside code that runs during SSR: top-level of +page.js/+layout.js load during server render, in +page.server.js, in onModuleInit-style server code, or in a shared module executed during server render.
Common situations: Calling `goto` in a load function without guarding for browser; using `invalidateAll` inside a shared store's init; importing navigation helpers into server-only code; running component code during prerender/SSR.
Related errors
- DEV: Cannot use `${caller}` with an external URL. Use `windo
- DEV: Cannot use `${caller}` with a URL that does not resolve
- The `goto(..., { noScroll: true, keepFocus: true })` options
- Loading ${url} using `window.fetch`. For best results, use t
- The form action returned a value, but it isn't available in
AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02).
Data as JSON: /api/errors/81e809e1e8ce7f66.
Report an issue: GitHub.