A single-file Phoenix application which demonstrates a bug when using the Phoenix.router.forward/4
macro with a LiveView.
When forwarding a route to another router, values assigned to a socket in a live_session
on_mount
handler are discarded when the socket is connected.
The values are present when the LiveView is first mounted.
The repository contains two routers. The first router (Example.Router
) simply forwards all requests for the /forwarded
route to the second router (Example.ForwardedRouter
).
Example.ForwardedRouter
defines a single live
route, inside a live_session
block. The live_session
block sets the on_mount
option to {Example.MountHandler, :default}
.
The Example.MountHandler.on_mount/4
function sets the value of socket.assigns.test_key
to "Test value".
The Example.HomeLive.mount/3
function sets the value of socket.assigns.test_key
to "Fallback value" if it is not already assigned (using assign_new
).
When loading the /forwarded
route in the browser, you will briefly see the text "Test value". This is replaced by "Fallback value" once the socket is connected.
It is important to note that the bug described above only manifests under the following specific circumstances:
- The route is forwarded from the main router to a second router. If you use a single router, everything works.
- The forwarded route is not
/
. If you forward the/
route, everything works. - The second router defines the
on_mount
handler as alive_session
option. If the LiveView itself defines theon_mount
handler, everything works.