question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow manually redirecting to 404 route within a dynamic slug

See original GitHub issue

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].vue
  • 404.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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

13reactions
Aareksiocommented, Apr 22, 2022

I am positive the broad idea of this issue is to ask “how to handle 404 on dynamic path”.

  const $route = useRoute()
  const { data } = await useAsyncData(`product:${$route.params.id}`, () => $fetch(`/api/products/${$route.params.id}`))

  // TODO: Display proper 404 when accessing unknown product id

  // Requirements:
  // - Using default error page - big "404" with small "Page not found: /product/42" - just like native nuxt 404
  // - On all types of navigation:
  //   a) Home -> Product/42 (client)
  //   b) Home -> Product/42 -> Home -> Product/42 (client + cache)
  //   c) Product/42 (server)
  //   d) Product/42 -> Home -> Product/42 (server into client + cache)
  // - Proper status code and status message when SSR (404, Not Found)

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

3reactions
danielroecommented, Apr 22, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic route prevents redirecting to 404 page in Next.js
js file inside /pages directory. If I delete my [pid].js file, 404 page works just fine. But, if I keep my [pid].
Read more >
How to redirect to 404 when slug is invalid? : r/Nuxt - Reddit
Can anyone tell me how to manually redirect to a 404 from a dynamic route component where the incoming slug is deemed (by...
Read more >
Empty page instead of 404 error when url contains channel slug
Whenever a wrong url is entered manually behind "example.com/news/" (or maybe a wrong filled in link) it won't go to the 404 page...
Read more >
Shallow Routing - Next.js
Shallow routing allows you to change the URL without running data fetching methods again ... Shallow routing only works for URL changes in...
Read more >
File System Routing - Nuxt
Thus 301 redirects should be in place and your internal linking has to be adapted correctly. If you set trailingSlash to true ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found