sveltejs / sveltejs/kit

Calling `error` in browser layout `load` function does not work

Open
#14,678 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

error handling load / layout
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

I am trying to trigger an error page if the user-agent is not supported.

if (browser && !checkBrowserFeatures()) error(400);

However, calling error() in a browser layout load function does not work:

// src\routes\+layout.ts

import { browser } from '$app/environment';
import { error } from '@sveltejs/kit';

export const load = () => {
  if (browser) error(400);
};
Uncaught (in promise) Object { status: 400, body: {…} }

Renaming +layout.ts to +page.ts fixes the issue, rendering a 400 error page.

Reproduction

https://github.com/hyunbinseo/svelte-kit-14678

Logs

System Info
System:
  OS: Windows 11 10.0.26100
  CPU: (8) x64 Intel(R) Core(TM) Ultra 7 258V
  Memory: 5.31 GB / 31.48 GB
Binaries:
  Node: 24.10.0 - C:\Users\hyunb\AppData\Local\fnm_multishells\2436_1760085385841\node.EXE
  Yarn: 1.22.22 - C:\Users\hyunb\AppData\Local\fnm_multishells\2436_1760085385841\yarn.CMD
  npm: 11.6.1 - C:\Users\hyunb\AppData\Local\fnm_multishells\2436_1760085385841\npm.CMD
  pnpm: 10.18.1 - C:\Users\hyunb\AppData\Local\fnm_multishells\2436_1760085385841\pnpm.CMD
  Deno: 2.4.0 - C:\Users\hyunb\.deno\bin\deno.EXE
Browsers:
  Chrome: 141.0.7390.66
  Edge: Chromium (140.0.3485.54)
  Firefox: 143.0.4 - C:\Program Files\Mozilla Firefox\firefox.exe
npmPackages:
  @sveltejs/adapter-auto: ^6.1.0 => 6.1.1
  @sveltejs/kit: ^2.43.2 => 2.46.4
  @sveltejs/vite-plugin-svelte: ^6.2.0 => 6.2.1
  svelte: ^5.39.5 => 5.39.11
  vite: ^7.1.7 => 7.1.9
Severity

annoyance

Additional Information

I know this technically isn't an HTTP error, thus the status code is ambiguous.

Still there should be a valid way to programmatically trigger an error page on the client.

Imagine a client-side navigation to a page without 1) server load functions 2) HTTP requests. The JS file including the load function is simply loaded and executed on the browser, but you can still call error(400).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior from the linked repository, comparing src/routes/+layout.ts with the +page.ts case that renders the 400 page. Read the client-side load and error-handling paths involved in browser layout navigation; done means a supported way to trigger the intended error page during a client-side navigation, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.