Skip to content

How to throw a 404 inside an element? #16949

Description

@ivands

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

Activity

  1. sacrosanctic commented on Aug 27, 2026

    @sacrosanctic
    Contributor

    But how can we do this using only "async svelte" and our own API client?

    redirect to /404

    Or you want to handle the error gracefully, then consider https://svelte.dev/docs/svelte/svelte-boundary

  2. ivands commented on Aug 27, 2026

    @ivands
    Author

    @sacrosanctic I'm still confused.
    How can we redirect during rendering inside a component?
    Cause the problem is that rendering happens client & server side.
    Also, I don't really wanna redirect.
    I wanna stop the rendering and show the 404 error page.
    Just like when you throw a 404 error inside a remote function.

  3. ivands commented on Aug 27, 2026

    @ivands
    Author

    Example:

    <script>
    const post = await getPost() // not a remote function
    if(!post) {
     // What now?
    }
    </script>
    <h1>{post.title}</h1>
  4. teemingc commented on Aug 27, 2026

    @teemingc
    Member

    In SvelteKit 3 you should be able to throw your own Error object and handle that in a client-side handleError https://next.svelte.dev/docs/kit/errors#Unknown-errors
    SvelteKit 3 also uses +error.svelte pages for errors thrown during async rendering https://next.svelte.dev/docs/kit/errors#Error-boundaries

  5. sacrosanctic commented on Aug 27, 2026

    @sacrosanctic
    Contributor
    <script>
    	const getName = async () => {
    		throw new Error('bleh')
    		return 'hi'
    	}
    </script>
    
    <svelte:boundary>
    	{#snippet failed(error)}
    		{error}
    	{/snippet}
    
    	{await getName()}
    </svelte:boundary>

    REPL

    Alongside what teemingc said, you'll get better hooks in sk3. It the pattern is more or less the same now.

  6. locked and limited conversation to collaborators on Aug 27, 2026
  7. converted this issue into a discussion #16957 on Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions