nuxt: allow manually redirecting to 404 route within a dynamic slug
Environment
------------------------------
- Operating System: `Linux`
- Node Version: `v16.14.2`
- Nuxt Version: `3.0.0-rc.1`
- Package Manager: `npm@7.17.0`
- Builder: `vite`
- User Config: `-`
- Runtime Modules: `-`
- Build Modules: `-`
------------------------------
Reproduction
https://stackblitz.com/edit/github-yyumyt?file=pages/404.vue
Describe the bug
I have defined three pages (routes):
car.vue[slug].vue404.vue
If I go to http://.../hi => it loads [slug].vue => OK
If I go to http://.../car => it loads car.vue => OK
If I go to http://.../404 => it loads [slug].vue => FAIL
Additional context
No response
Logs
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 20 (15 by maintainers)
I am positive the broad idea of this issue is to ask “how to handle 404 on dynamic path”.
There is currently no documentation on how to properly handle API’s 404 response in the docs. From my experience it is also extremely hard, as all of the 4 navigation types listed in comments above require different logic each.
Here is the repo reproduction in case you want to bootstrap the project: data-fetching
Thanks for that link @Aareksio.
I’ve submitted a related PR to fix an issue, add some basic docs and improve DX (by avoiding import of
createError), but check out https://stackblitz.com/edit/github-jwfbey. Does that answer your questions?If I have logic in setup inside [slug].vue where I call useFetch and the api returns 404, I would like to redirect to the /404 route which is not possible now.
Created two issues to follow:
Feel free to open a new one if you think there’s another one to track.
Minimal reproduction for the above, using script setup for readability: https://stackblitz.com/edit/github-5jhffm
I’m having a related problem rendering a 404 error:
The console shows:
So, instead of 404, a 500 error is rendered, because the template is trying to access a property of
undefined(data.id). It works (404 rendered) if I removedata.idfrom the template. Is this the intended behavior?I’d like to avoid to wrap my whole template into a
<div v-if="data"></div>as the template should not be rendered at all (I’m throwing an exception even before thereturn) and I’m usinguseAsyncDatainstead ofuseLazyAsyncDatato not have to handle the empty data situation in the template.@danielroe the throwError seems to be marked as deprecated now.
Swapping for throw createError seems to output fails in the server console "[nitro] [dev] [unhandledRejection] Error: nuxt instance unavailable
@danielroe Example: I have a bike shop. A customer accesses the route: http://bike.com => index.vue. The customer wants to view the bike categories http://bike.com/blue-bikes => [slug].vue. The customer selects one of the bikes from the category, http://bike.com/best-blue-bike.html => [slug].html.vue. Everything works great, but how do I catch the condition where I want to redirect to a 404 page?
Should I open a separate issue for that?
Edit: Created https://github.com/nuxt/nuxt.js/issues/14695.
We deliberately don’t throw a full page error in this case by default (so you can handle it within the UI with more granular error handlers). But if you want the full error page, you can add
fatal: trueto the options ofcreateError.@danielroe Even with your fix, when using SSR the backend still throws internal an error (and flashes it until the hidration takes over):
It’s visible in the logs of the example you provided, but here’s a minimal example nontheless: https://stackblitz.com/edit/nuxt-starter-nlacpe
https://user-images.githubusercontent.com/5151543/169717529-9f1a377b-30d9-4466-b576-bab9ffa550ff.mp4
The error is thrown whenever
throwErroris used after anawaitin the setup function