Repository navigation
Replies: 7 comments
redirect to Or you want to handle the error gracefully, then consider https://svelte.dev/docs/svelte/svelte-boundary |
|
@sacrosanctic I'm still confused. |
|
Example: <script>
const post = await getPost() // not a remote function
if(!post) {
// What now?
}
</script>
<h1>{post.title}</h1> |
|
In SvelteKit 3 you should be able to throw your own |
<script>
const getName = async () => {
throw new Error('bleh')
return 'hi'
}
</script>
<svelte:boundary>
{#snippet failed(error)}
{error}
{/snippet}
{await getName()}
</svelte:boundary>Alongside what teemingc said, you'll get better hooks in sk3. It the pattern is more or less the same now. |
|
On SvelteKit 2.x (2.70.3 is still 1. Enable the flag // svelte.config.js
export default {
compilerOptions: { experimental: { async: true } },
kit: {
// adapter: ...,
experimental: { handleRenderingErrors: true }
}
};2. Throw <script>
import { error } from '@sveltejs/kit';
import { page } from '$app/state';
import { getPost } from '$lib/api'; // your own client
async function getPostOr404(slug) {
const post = await getPost(slug);
if (!post) error(404, 'Post not found');
return post;
}
const post = await getPostOr404(page.params.slug);
</script>
<h1>{post.title}</h1>3. Key the root layout (workaround for a 2.x bug, see below) <!-- src/routes/+layout.svelte -->
<script>
import { page } from '$app/state';
let { children } = $props();
</script>
{#key page.url.pathname}
{@render children()}
{/key}What I saw
Why
Tested
Not tested: remote functions, or errors thrown from a layout. If the full remount from |
|
I tested this on SvelteKit 2.70.3 with Throwing I can also reproduce the stale error boundary issue after navigating back to a valid route. Wrapping the root layout with Btw tks for documenting the workaround, this helped clarify the difference between throwing during async rendering and handling the missing resource directly in the component. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the problem
We have our own api client. Not using remote functions.
The reason is caching, and calling our API inside a remote function would just add an extra network call to every api request.
But it's unclear how to throw a 404 inside our own client when a resource is not found.
So that SvelteKit knows to render the 404 page.
I did find a way to do this with remote functions.
But how can we do this using only "async svelte" and our own API client?
Describe the proposed solution
I can't find any docs on how to do this.
Would be nice if we had some docs on how to solve this problem.
Alternatives considered
No response
Importance
i cannot use SvelteKit without it
Additional Information
No response
All reactions