How to return custom status codes from live views?

I suppose the thread title says it all. We currently use live view for our consumer-facing website and would like to return 404 errors whenever web crawlers (or users) make requests to endpoints with no data. We would like the error page to be a live view so that the experience is no different than being on any other page.

Is there something we are missing here? How do you folks go about setting HTTP status codes for live views? I’ve tried using the error view but it ended up not being able to render the live view (as far as I’m aware at least).

Our motivation for this is to prevent those pages from being indexed. Currently we just set noindex, nofollow on that page so that crawlers leave it alone. Was hoping there was a better way though.

I don’t think there such system (returning a 404).
The only thing that pop in my head is : redirect the socket to your 494 page using ˋpush_patch/2` (Phoenix.LiveView — Phoenix LiveView v0.15.5) or a simple redirect.

Hope it help :wink:

Maybe you could make some sort of plug that will redirect to the 404 live view with the right status code. When you say no data do you mean the page doesn’t exist so it returns nothing or what they want is like an empty list?

I would think push_redirect/2 is more what they are looking for.

1 Like

Yeah right push_redirect is better :wink:

Maybe this thread could be of help?

We have an e-commerce site which is using live view. Essentially our use case is if a product in our database is not found when you visit a product page directly we want to return a 404. Right now we are returning a 200 status code but displaying an error page. This is fine for users but not so fine for web crawlers.

Right now our solution is to add noindex, nofollow but a 404 would be preferred.

I suppose we could create a route for the error, something like /error/404 which would render the live view error page and also set the status code properly. Does that make sense?

Searching the forum I found this. It should help with what you are trying to do.

You can also passed via a Phoenix controller. If there is the data continue the flow using live flows, otherwise return your error not found