Skip to content

Calling error in browser layout load function does not work #14678

@hyunbinseo

Description

@hyunbinseo

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions