fix: show error.html when root layout load() throws in SPA mode#15798
fix: show error.html when root layout load() throws in SPA mode#15798Zelys-DFKH wants to merge 9 commits into
Conversation
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 detectedLatest commit: e4cc71b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
🔔 Gentle ping — this PR has been ready with green CI for several days. Happy to address feedback or answer questions about the approach! |
|
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>
|
Thanks for the nudge. My fault — the test I added was waiting for I also swapped out The other failures in the current/beta jobs are showing up on main today too, so they're not related to this PR. |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
|
||
| export { default as root } from '../root.${isSvelte5Plus() ? 'js' : 'svelte'}'; | ||
|
|
||
| export const error_template = ${s(error_page)}; |
There was a problem hiding this comment.
I wonder if it's worth loading the error template only when it's needed rather than serialising it here (we can write a module to the .svelte-kit/generated folder and dynamically import it when we need to display the error page)
|
Took a shot at this. Since |
closes #13721
In SPA mode (
ssr: falseon the root layout), whenload()throws, SvelteKit callsload_root_error_pageto show an error page. That function re-runs the same root layoutload()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.htmlat build time and bundles the content intoapp.jsaserror_template(viawrite_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 originalthrow erroris preserved as a fallback iferror_templateis empty.The regression test is in the existing
no-ssrtest app: a/root-layout-errorroute throws fromload(), and Playwright verifies thaterror.htmlrenders 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:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits