I am working on a service where I want to serve multiple file formats from the same endpoint, depending on what content type user will negotiate. The goal there is to not duplicate endpoints where it is not needed. However I do not see a way to have for example live route on /
, but when non-HTML, non-browser client connects - for example if it will send request for Accept: application/atom+xml
I will handle that request in “plain old” controller, but when it will request Accept: text/html
it will be handled by LiveView. Is such scenario even supported? I tried to dig through the documentation, but it is not answered anywhere.
You could do it with live_render in a controller, but that means not using live/4
in the router and essentially not being able to use the features of LV, which require the LV to be “router mounted”.
Yeah, that is what I noticed, but my question is mainly if there is way to have router to decide whether it is live view or not.
No. The router routes based on method, path and host, nothing more. You can see that with Phoenix.Router — Phoenix v1.7.12, which asks for those exact values besides having the router itself passed as first parameter. Format handling is the job of the controller.
Maybe a way to achieve this is in liveview analize the headers and perform a redirect to a normal controller before rendering.
If the business logic is well organized then it can be shared across normal controllers and liveviews to render json/xml/html
It may be a solution, that I will do path rewrite before it hits router and then it will be possible to handle it differently depending on the format. Though it is still sad, that it is something that is not supported by Phoenix by default.
This is the only way I’ve managed to do what you want. You can even have a separate, private router.