I have a component for pagination. It works but there’s one issue: it won’t replace or update the URL in the browser dynamically. Namely, when I change a page – 2nd, 3rd, 4th page of the data in a table – I want the URL to get updated as well: updated in the browser URL bar and appended to the browser history.
my_domain.com/my/pigs?page=2&page_size=5
my_domain.com/my/pigs?page=3&page_size=5
my_domain.com/my/pigs?page=66&page_size=5
But at the moment only data will get fetched correctly, according to the current page, whereas the URL will remain static always - my_domain.com/my/pigs
:
@impl true
def handle_info({:update, opts}, socket) do
extra_params = merge_and_sanitize_params(socket, opts)
path = "/my/pigs"
to = path <> "?" <> URI.encode_query(extra_params)
# socket = push_patch(socket, to: to, replace: true)
socket = push_patch(socket, to: to)
{:noreply, socket}
end
I’ve tried replace
with true
, and without – the URL will stay “my/pigs” no matter what.
The _url
in handle_params(..)
will contain the correct one – with ?page=X&page_size=Y
which is correct:
@impl true
def handle_params(params, _url, socket) do
# _url will contain dynamic url, with the correct params
# .....
However, the the URL in browser the will still remain static.
What’s the matter?