Handling Non-existent Dynamic Routes

Is there a way in Redwood to dynamically render a 404 page when a dynamic route does not match any existing data? For instance, if a user tries to access ‘/user/abc’ but there is no user with the username ‘abc’ in the database, instead of redirecting to another route to display a 404 page, can we directly render the 404 page at the current URL ‘/user/abc’?

1 Like

Hmm,
Not sure if it’s best practice but I’d nest a cell in the page (passing the username through to it), and then instead of rendering something in the Empty component call the navigate function pointing to your special 404 page.

That make sense?

Alternatively you could just use the empty component in the cell to render your special user does not exist content.

Hi @rbdwney 404’s are among the oldest RW issues and stem a bit from how SPA’s work: NotFoundPage incorrectly gives 200 status error instead of 404 · Issue #961 · redwoodjs/redwood · GitHub

But, in your case

For instance, if a user tries to access ‘/user/abc’ but there is no user with the username ‘abc’ in the database, instead of redirecting to another route to display a 404 page, can we directly render the 404 page at the current URL ‘/user/abc’?

The practice I have seen most is to use the Cell’s Failure component to do a navigate to a custom not found page etc … however, this would be a 404. And, sometimes a 404 for checking if a user exists or not is unwanted since this is a way to see if people have signed up or accounts exists.

Here, I think I’d have a nice component in the Failure (or maybe change your service to not error but return an empty Use r object) and rely on the Empty state to inform the user isn’t found.

In upcoming more serverful Redwood releases more control over the status codes for proper 404’s are possible.

Also - other approaches using Netlify or Vercel (or your deployment provider) to proxy redirect to a 404 with a status is possible.