Hi everybody,
TL;DR: How to push_redirect
in mount without a full page reload?
I have an application that handle localization with Gettext and I have a plug that set the selected locale in a cookie and in the session.
Now, say I have the following routes:
live "/:locale", DashboardLive
live "/product", ProductLive
live "/cart", CartLive
Sometimes from the other live views I want to go back to the Dashboard, but I don’t want to track anymore the locale. So I have live_redirect links to "/"
instead of for example "/en"
:
# In LiveView, e.g. product_live.html.leex
live_redirect "Home", to: Routes.live_path(@socket, MyApp.DashboardLive, "")
In my Dashboard mount however I want to redirect back to "/en"
(or whatever is the current locale) but without refreshing the page.
Currently I have the following code in mount:
def mount(params, session, socket) do
locale = params["locale"] || socket.assigns[:locale] || session["locale"]
...
# In case of the current path being "/"
socket = if params["locale"] == nil do
push_redirect(socket, to: "/#{locale}")
else
socket
end
{:ok, socket}
end
Here the code might not be idiomatic, but in my current code I also handle the case where the locale is not in the known locale list but removed for brevity…
The problem is that, I’m getting a full page reload at one moment, but I don’t know where?
If in the views I have the links with the locale, e.g.
# In LiveView, e.g. product_live.html.leex
live_redirect "Home", to: Routes.live_path(@socket, MyApp.DashboardLive, "en")
I can go back and forth within the LiveViews without full reloads.
So I wonder what I’m doing wrong?
Also, the redirect seems to happen after a little delay.
I mean, when I look in the network panel of the devtools, at first it seems that the redirect is actually happening within live views without HTTP full reloads, but after a second or two I got a full page reload…
Edit: At first I tried to use push_patch
, but it seems that’s impossible to call push_patch
in mount. I got the error attempted to live patch while mounting