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?
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
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?
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
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.
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.