Skip to content

fix: show error.html when root layout load() throws in SPA mode#15798

Merged
Rich-Harris merged 12 commits into
sveltejs:mainfrom
Zelys-DFKH:fix/spa-root-layout-error-page
Jun 17, 2026
Merged

fix: show error.html when root layout load() throws in SPA mode#15798
Rich-Harris merged 12 commits into
sveltejs:mainfrom
Zelys-DFKH:fix/spa-root-layout-error-page

Conversation

@Zelys-DFKH

Copy link
Copy Markdown
Contributor

closes #13721

In SPA mode (ssr: false on the root layout), when load() throws, SvelteKit calls load_root_error_page to show an error page. That function re-runs the same root layout load() to gather data for the error boundary, which throws again. The inner catch had a // TODO: SPA mode? comment and re-threw with no handler. Blank page.

The fix reads error.html at build time and bundles the content into app.js as error_template (via write_client_manifest). When the inner catch fires, it writes the template directly to the document, substituting %sveltekit.status% and %sveltekit.error.message%: the same substitution the server already does. The original throw error is preserved as a fallback if error_template is empty.

The regression test is in the existing no-ssr test app: a /root-layout-error route throws from load(), and Playwright verifies that error.html renders with the right status and message rather than a blank page.

(Thanks to @inq for independently identifying the root cause and the same approach in the issue comments.)


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

In SPA mode, if the root layout's load() throws, load_root_error_page
re-runs that same load() to find an error URL, which throws again. The
inner catch had a // TODO: SPA mode? comment and re-threw the error
with no handler. Result: blank page.

Fix: bundle src/error.html into app.js as error_template at build time
(via the existing load_error_page helper). When the inner catch fires
and error_template is set, document.write() replaces the page with the
error HTML. Status and message placeholders are substituted, and the
message is HTML-escaped before writing.

Closes sveltejs#13721
@changeset-bot

changeset-bot Bot commented May 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 77e5d2e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

Comment thread packages/kit/src/runtime/client/client.js Outdated
@Zelys-DFKH

Copy link
Copy Markdown
Contributor Author

🔔 Gentle ping — this PR has been ready with green CI for several days. Happy to address feedback or answer questions about the approach!

@teemingc

teemingc commented May 9, 2026

Copy link
Copy Markdown
Member

There are 6 CI checks currently failing

… false in test

document.open() fires executionContextsCleared on Windows, destroying
Playwright's CDP connection. DOMParser + adoptNode updates the DOM
in-place without resetting document state.

The test was waiting for body.started, which is structurally impossible
on this path since initialize() never runs — the error is thrown before
it. { wait_for_started: false } opts out of that wait.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Zelys-DFKH

Copy link
Copy Markdown
Contributor Author

Thanks for the nudge. My fault — the test I added was waiting for body.started, which is the CSS class SvelteKit sets after initialize() finishes. On this code path initialize() never runs (the error gets thrown before it gets there), so the test was always going to time out. Adding { wait_for_started: false } fixes it.

I also swapped out document.open() / document.write() / document.close() for DOMParser + adoptNode. The document.open() call fires executionContextsCleared on Windows, which destroys Playwright's connection to the page — and document.write() is deprecated anyway. The in-place DOM replacement avoids both problems.

The other failures in the current/beta jobs are showing up on main today too, so they're not related to this PR.

Zelys-DFKH and others added 2 commits May 9, 2026 11:53
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread packages/kit/src/core/sync/write_client_manifest.js Outdated
@Zelys-DFKH

Copy link
Copy Markdown
Contributor Author

Took a shot at this. Since client.js lives in the source tree rather than the generated folder, it can't resolve ./error-template.js directly. Went with a getter in app.js instead: it lives in the generated folder, so the relative path resolves correctly and Vite can follow the static string for chunking.

@Rich-Harris Rich-Harris force-pushed the fix/spa-root-layout-error-page branch from 26b6e55 to db95b53 Compare June 17, 2026 17:01

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@Rich-Harris Rich-Harris merged commit 8740132 into sveltejs:main Jun 17, 2026
22 of 23 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 17, 2026
Rich-Harris pushed a commit that referenced this pull request Jun 18, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @sveltejs/enhanced-img@0.11.0

### Minor Changes


- feat: export `EnhancedImgAttributes` type
([#15649](#15649))


### Patch Changes


- fix: exclude imports with `?` character from transformation
([#15617](#15617))
## @sveltejs/kit@2.66.0

### Minor Changes


- feat: precompress prerendered `.md` and `.mdx` files
([#15893](#15893))


- feat: warn the user when they forget to make boolean inputs optional
in their form schemas
([#15804](#15804))


### Patch Changes


- fix: blur active element before component update during navigation so
that blur/focusout handlers fire while old component data is still valid
([#15452](#15452))


- fix: ensure `base` is available from `$service-worker` during
development ([#15882](#15882))


- fix: use correct relative asset paths when rendering an error page for
a missing `__data.json` request
([#15884](#15884))


- fix: preserve active `for await` consumers across `query.live`
reconnects ([#16022](#16022))


- fix: settle `query.live` reconnect promise on all exit paths,
preventing `invalidateAll()` from deadlocking when a live query is
offline or interrupted
([#16022](#16022))


- fix: preserve last value when a `query.live` stream completes without
yielding on reconnect
([#16022](#16022))


- fix: remove `types: ['node']` from generated tsconfig to avoid errors
when `@types/node` is not installed
([#15709](#15709))


- fix: prefer pages over endpoints when prerendering
([#16076](#16076))


- fix: restore snapshots after afterNavigate callbacks
([#16066](#16066))


- fix: support `ws:`/`wss:` and `trusted-types-eval` for CSP sources
([#15938](#15938))


- fix: omit empty `file` inputs from remote form data
([#15898](#15898))


- fix: fail early if a route with `+page` and `+server` is marked as
prerenderable ([#16075](#16075))


- fix: wait a tick before resetting forms
([#15805](#15805))


- fix: `preflight` schemas apply correctly when chained before `for`
([#15863](#15863))


- fix: blank page in SPA mode when root layout `load()` throws
([#15798](#15798))


- fix: pass all unknown options from the `sveltekit` Vite plugin through
to `vite-plugin-svelte`
([#16010](#16010))
## @sveltejs/adapter-node@5.5.5

### Patch Changes


- fix: bundle entrypoints alongside app code
([#16069](#16069))


- fix: log the actual adapter-node listening address
([#15899](#15899))

- Updated dependencies
[[`63f1b0b`](63f1b0b),
[`1dbff3f`](1dbff3f),
[`961ba01`](961ba01),
[`d2e108c`](d2e108c),
[`d2e108c`](d2e108c),
[`d2e108c`](d2e108c),
[`860b3c7`](860b3c7),
[`f8c842c`](f8c842c),
[`d3aa5fe`](d3aa5fe),
[`0dd7659`](0dd7659),
[`03e9f66`](03e9f66),
[`57b7b7b`](57b7b7b),
[`4eabadc`](4eabadc),
[`6fbf2b6`](6fbf2b6),
[`276744d`](276744d),
[`8740132`](8740132),
[`f430a68`](f430a68),
[`1c7a8dc`](1c7a8dc)]:
  - @sveltejs/kit@2.66.0
## @sveltejs/adapter-vercel@6.3.4

### Patch Changes


- fix: prevent missing immutable assets from being cached as 404s for a
year ([#16077](#16077))

- Updated dependencies
[[`63f1b0b`](63f1b0b),
[`1dbff3f`](1dbff3f),
[`961ba01`](961ba01),
[`d2e108c`](d2e108c),
[`d2e108c`](d2e108c),
[`d2e108c`](d2e108c),
[`860b3c7`](860b3c7),
[`f8c842c`](f8c842c),
[`d3aa5fe`](d3aa5fe),
[`0dd7659`](0dd7659),
[`03e9f66`](03e9f66),
[`57b7b7b`](57b7b7b),
[`4eabadc`](4eabadc),
[`6fbf2b6`](6fbf2b6),
[`276744d`](276744d),
[`8740132`](8740132),
[`f430a68`](f430a68),
[`1c7a8dc`](1c7a8dc)]:
  - @sveltejs/kit@2.66.0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
huskas-2189 pushed a commit to huskas-2189/Bookmark that referenced this pull request Jun 20, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@sveltejs/kit](https://svelte.dev) ([source](https://github.com/sveltejs/kit/tree/HEAD/packages/kit)) | [`2.65.1` → `2.66.0`](https://renovatebot.com/diffs/npm/@sveltejs%2fkit/2.65.1/2.66.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@sveltejs%2fkit/2.66.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@sveltejs%2fkit/2.65.1/2.66.0?slim=true) |

---

### Release Notes

<details>
<summary>sveltejs/kit (@&#8203;sveltejs/kit)</summary>

### [`v2.66.0`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2660)

[Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/kit@2.65.2...@sveltejs/kit@2.66.0)

##### Minor Changes

- feat: precompress prerendered `.md` and `.mdx` files ([#&#8203;15893](sveltejs/kit#15893))

- feat: warn the user when they forget to make boolean inputs optional in their form schemas ([#&#8203;15804](sveltejs/kit#15804))

##### Patch Changes

- fix: blur active element before component update during navigation so that blur/focusout handlers fire while old component data is still valid ([#&#8203;15452](sveltejs/kit#15452))

- fix: ensure `base` is available from `$service-worker` during development ([#&#8203;15882](sveltejs/kit#15882))

- fix: use correct relative asset paths when rendering an error page for a missing `__data.json` request ([#&#8203;15884](sveltejs/kit#15884))

- fix: preserve active `for await` consumers across `query.live` reconnects ([#&#8203;16022](sveltejs/kit#16022))

- fix: settle `query.live` reconnect promise on all exit paths, preventing `invalidateAll()` from deadlocking when a live query is offline or interrupted ([#&#8203;16022](sveltejs/kit#16022))

- fix: preserve last value when a `query.live` stream completes without yielding on reconnect ([#&#8203;16022](sveltejs/kit#16022))

- fix: remove `types: ['node']` from generated tsconfig to avoid errors when `@types/node` is not installed ([#&#8203;15709](sveltejs/kit#15709))

- fix: prefer pages over endpoints when prerendering ([#&#8203;16076](sveltejs/kit#16076))

- fix: restore snapshots after afterNavigate callbacks ([#&#8203;16066](sveltejs/kit#16066))

- fix: support `ws:`/`wss:` and `trusted-types-eval` for CSP sources ([#&#8203;15938](sveltejs/kit#15938))

- fix: omit empty `file` inputs from remote form data ([#&#8203;15898](sveltejs/kit#15898))

- fix: fail early if a route with `+page` and `+server` is marked as prerenderable ([#&#8203;16075](sveltejs/kit#16075))

- fix: wait a tick before resetting forms ([#&#8203;15805](sveltejs/kit#15805))

- fix: `preflight` schemas apply correctly when chained before `for` ([#&#8203;15863](sveltejs/kit#15863))

- fix: blank page in SPA mode when root layout `load()` throws ([#&#8203;15798](sveltejs/kit#15798))

- fix: pass all unknown options from the `sveltekit` Vite plugin through to `vite-plugin-svelte` ([#&#8203;16010](sveltejs/kit#16010))

### [`v2.65.2`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2652)

[Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/kit@2.65.1...@sveltejs/kit@2.65.2)

##### Patch Changes

- fix: throw an error when prerendering a root +server.js that returns a non-HTML response ([#&#8203;15994](sveltejs/kit#15994))

- fix: decode base64-serialized fetch bodies before caching them for client-side replay ([#&#8203;16034](sveltejs/kit#16034))

- fix: correctly access explicit dynamic public environment variables from prerendered pages and service workers ([#&#8203;16024](sveltejs/kit#16024))

- fix: allow `preloadCode` to be called during initial page load ([#&#8203;16028](sveltejs/kit#16028))

- fix: send `cache-control: private, no-store` on remote function responses so personalized query results can never be cached by shared caches ([#&#8203;16020](sveltejs/kit#16020))

- fix: preserve the HTTP status and error body when a remote function request fails in transport (e.g. a 401/403 from a `handle` hook), instead of reporting a generic 500 ([#&#8203;16021](sveltejs/kit#16021))

- fix: avoid loading universal nodes during build analysis when the app uses a hash router ([#&#8203;16042](sveltejs/kit#16042))

- fix: correctly serve client entry during development when using the pnpm global virtual store ([#&#8203;16045](sveltejs/kit#16045))

- fix: normalize path separators when comparing config ([#&#8203;16037](sveltejs/kit#16037))

- fix: ensure `building` resolves correctly to allow avoiding build-time explicit environment variable validation ([#&#8203;16058](sveltejs/kit#16058))

- fix: prevent unhandled promise rejections when remote function failures are consumed via `current`/`error` instead of `await` ([#&#8203;16018](sveltejs/kit#16018))

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled because a matching PR was automerged previously.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yMjAuMCIsInVwZGF0ZWRJblZlciI6IjQzLjIyMC4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6W119-->

Reviewed-on: https://codeberg.org/huskas-2189/Bookmark/pulls/126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSR disabled incorrectly handles root layout exceptions

3 participants