kit icon indicating copy to clipboard operation
kit copied to clipboard

+error.svelte doesn't work in directories other than src/routes

Open len0xx opened this issue 3 years ago • 3 comments

Describe the bug

I was trying to create a custom error page for route /tee. But creating the +error.svelte file inside this directory doesn't solve the problem even though the documentation says it should've done so: SvelteKit will 'walk up the tree' looking for the closest error boundary — if the file above didn't exist it would try src/routes/blog/+error.svelte and src/routes/+error.svelte before rendering the default error page. https://kit.svelte.dev/docs/routing#error

My file hierarchy:

  • src/routes/
    • +page.svelte
    • +error.svelte
    • tee
      • +page.svelte
      • +error.svelte

When I go to / I see the contents of src/routes/+page.svelte When I go to /tee I see the contents of src/routes/tee/+page.svelte And when I go to /error (non-existent route) I see the contents of src/routes/+error.svelte But when I go to /tee/error (non-existent route) I still see the contents of src/routes/+error.svelte. But I expect to see the contents of src/routes/tee/+error.svelte here

Reproduction

https://github.com/len0xx/sveltekit-error-issue

To reproduce an issue, just clone the project and run:

npm install
npm run dev

Then go to http://localhost:5173/tee/error. What you will see is the contents of src/routes/+error.svelte. But I expect to see the contents of src/routes/tee/+error.svelte here

Logs

No response

System Info

System:
    OS: Linux 5.19 Ubuntu 22.10 22.10 (Kinetic Kudu)
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-12400F
    Memory: 8.45 GB / 15.47 GB
    Container: Yes
    Shell: 5.2.2 - /bin/bash
Binaries:
    Node: 18.7.0 - /usr/bin/node
    npm: 9.1.1 - /usr/local/bin/npm
Browsers:
    Brave Browser: 107.1.45.133
    Chrome: 107.0.5304.121
    Firefox: 107.0
npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.90 
    @sveltejs/kit: next => 1.0.0-next.567 
    svelte: ^3.53.1 => 3.53.1 
    vite: ^3.2.4 => 3.2.4

Severity

serious, but I can work around it

Additional Information

No response

len0xx avatar Nov 29 '22 10:11 len0xx

I believe this is the intended behavior, but the docs could probably be improved. This walking-up only happens from an existing route. You can achieve what you're looking for here with a catch-all src/routes/tee/[...whatever]/+page.js (which matches anything under /tee/...) with a load function that always throws a 404 error.

Conduitry avatar Nov 29 '22 12:11 Conduitry

See https://kit.svelte.dev/docs/advanced-routing#rest-parameters-404-pages

Rich-Harris avatar Nov 29 '22 13:11 Rich-Harris

Yeah, it works. Thank you! Seems like the info on this page is a little misleading. Would've been really great if you improved it by highlighting the correct way to achieve what I wanted to.

len0xx avatar Nov 29 '22 13:11 len0xx