How do I return 404? 🛑

With a page that shows data based on a slug, if the slug has no match in the database, I’d like to return a 404 error to the browser.

Now I’m redirecting to a page with an error message via a Navigate To action but that’s not the desired goal. Anyone has experience with this with a workaround?

Hi @davhad :wave:

Does this help: Create 404 page | WeWeb Documentation

Hey @Joyce that is what I already have (had stumbled on that doc and applied it).

But if you redirect users to a 404 page using the navigate to action, that’s not returning a 404 error to the browser. Or am I wrong?

I’m looking for a way to return a 404 error.

You don’t need to use the navigate to action. The user will be automatically redirected to the 404 page if the slug doesn’t exist

Can you help me understand how Weweb deals with that under the hood?

In this app, if the slug doesn’t exist, that means that the onpageload collection query to get an item based on the slug (filter: item.slug = page slug param) will return 0 items (length=0).

Right now, when this happens, all in-page components bound to the item’s properties are showing as undefined and the page is rendering normally with lots of undefined’s everywhere.

He wants to explicitly throw a 404 if something happens, e.g if the parameter is not present/is not found in the DB, he would like to throw a 404 error and show the view. This is currently not possible as far as I understand, as it would require routing middleware? Only static collections work like this. @davhad

Yes precisely @Broberto

You could have a page workflow that navigates the user to the 404 page is the returned items are empty. It may take a half a second to get the backend API response though. Any reason that wouldn’t work?

1 Like

Its not a best practice

1 Like

Hey Jane, thanks. That’s what I have in place which I’m trying to improve upon :slight_smile:

The issue with this approach is mostly that you get redirected, which doesn’t mark the page itself as a 404 (for example for Google Search, or other crawler bots) but it simply redirects you to a 404. The URL will return 200 - OK and then bring you to a 404 - Not Found

1 Like

Yes. And I agree that’s a huge issue!

Hey @davhad :wave: I’m sorry I don’t have the answer of how it works under the hood. Let me try to get a dev on the topic to help you with this :slight_smile:

1 Like

Hi, it’s not possible to force a 404 redirection for now.

If I understand correctly, you want a page like this to return a 404 error ?

https://www.mysite.com/products/non-existing-product/

The WeWeb server that returns this page has no way of knowing if this product exists or not in your data so it will just return the page and let you do the logic.
If the product does not exists then you can show an error message or redirect to the 404 page but there will not be a 404 HTTP code.
Even if you manage to redirect to the 404 page with a 404 HTTP code, the origin page (/non-existing-product/) has already returned a 200 HTTP code and this is what Google will index.

As of today, you cannot really solve your issue. In the future we may add a feature to return 404 when a product does not exist, but it is not planned for now.

3 Likes

Thanks @Joyce and @Marc. With this alley being a dead end, off to exploring other ways to solve this issue :sweat_smile:

So it turns out eventhough technically a redirect to an error page (with http 200) doesn’t produce a 404, google as I have just come to learn has a special classification that called a “soft 404”. Just sharing in case it’s helpful to anyone.

3 Likes