kit: Add a straightforward way to export error pages with `adapter-static`
Is your feature request related to a problem? Please describe.
There doesn’t seem to be a proper way to export a 404 for static sites. In Sapper, adding /404
as a second entry point would export an additional /404/index.html
; in SvelteKit, there is currently no similar option.
Related to #1172, but with the specific goal of handling non-SPA exports.
Describe the solution you’d like
Assuming this has no value outside of static export, it probably should be a separate option for adapter-static
. Error routes should not be required to explicitly exist and should be exempt from the default ‘fail on error’ policy.
(A nice touch would be the ability to trigger specific errors from here, e.g. by passing error codes for prerendering. This way, websites that need more than a simple 404 (e.g. sites operating within larger systems may also want 502 and 504) could be catered for. I wonder, however, if this is common enough to mind.)
Describe alternatives you’ve considered In theory, offering a way to force prerendering no matter the status code would allow simply adding separate entries in the config, effectively replicating Sapper’s behaviour. This would be undesirable, as I believe SvelteKit’s ‘all errors are build errors’ behaviour is excellent and should remain otherwise intact.
A workaround currently requires error routes to exist as normal pages. This works, but leads to some duplication between $error.svelte
and 404.svelte
, is not very portable (a 404.svelte
that in fact returns 200
makes no sense with, say, adapter-node
), and generally ticks all boxes to qualify as a hack.
How important is this feature to you? A 404 page is important in static export, and relying on hacks in performing such a common task shouldn’t be necessary.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 13
- Comments: 16 (5 by maintainers)
Links to this issue
Commits related to this issue
- Manually create a 404 page. Fixes #12 Svelte Kit doesn't offer a good way to generate single HTML pages like 404.html, so I have to generate a regular page and then move it. Solution suggested here: ... — committed to cuibonobo/cuibonobo.com by cuibonobo 3 years ago
- Create static 404 page This involves using a really dumb `postbuild` script to make sure the generated `404.html` is actually in the right place. See https://github.com/sveltejs/kit/issues/1209#issue... — committed to united-computations/GunnHacks8.0 by ky28059 2 years ago
- Add 404.html See https://github.com/sveltejs/kit/issues/1209 The "prerender" instruction on the `kit` config from that thread didn't seem necessary, probably because my `+layout.server.ts` had that ... — committed to jeroenheijmans/tierdom by jeroenheijmans a year ago
What I’m doing now is
src/routes/404.svelte
– which builds to/build/404/index.html
(currently using static adapter).So then I add a
postbuild
npm script to move that file to the correct location:Summary for future readers
You can have your cake and eat it too. 🍰 i.e. :
Step 1: create error page
Step 2: update
sveltekit.config.js
Step 3: let your static host service know how to handle not found resources. As an example, I’m using the Azure Static Websites config
Oh, right, thanks, I missed this one. An option would be nice indeed.
However, the resulting fallback doesn’t render a 404 using
$error.svelte
, it simply renders a blank page withspa: true
, which ultimately solves a different problem.What am I still missing?
@vwkd Right now, the best way is to build a
routes/404.svelte
file and the include apostbuild
script that moves the built404/index.html
into the the build directory’s root as404.html
Before the recent output dir changes, this would look like:
I’ve run into the same issue. A static error page is missing. I’d like to see an option to export
__error.svelte
intomypath/myname.html
. Maybe something likeThis is needed for Cloudflare Pages to show an error page. By default, Cloudflare Pages redirects all invalid URLs back to the root, because it assumes SPA mode if there is no
404.html
present (see Not Found behavior).Currently, it’s even impossible to export such
404.html
manually, because a404.svelte
generates a404/index.html
instead of a404.html
.The
fallback
option is designed for thisI agree with @Raicuparta, this results in a blank page if JS is disabled in the browser. It seems like the fallback page is not prerendered along with the other routes, and instead relies on CSR.
I ended up reaching the exact same conclusion as @jeroenheijmans, but it not working with javascript off is a bit of a bummer. It’s the one thing missing here. Hoping there’s a solution for this
The
fallback
option appears to disable SSR altogether, exporting a single HTML document that I assume is an SPA entry point. What I’m trying to do is export all routes as usual, plus a/404
. Where am I wrong?fallback
and the newprerender.default: true
option make it straightforward to render a fallback 404.html (or 200.html, in the SPA case) page, so I’ll close this issue