Reload (fetch from db) upon hitting back button

This may be dumb, but when I navigate e.g. from show page back to index page (hitting back button in browser), it doesn’t trigger a database fetch. Is that phoenix, or some browser caching? In any case, can I force the fetch? I do some changes in associated records that are then not reflected on previous page.

It is the browser. You will have to resort to some javascript wizardry to force the fetch.

Hmm, actually another way around it would be LiveView, wouldn’t it? Even the cached page would establish soviet connection and start receiving changes in real time, right?

1 Like

:003:

You likely are better off using LiveView for your project as we chatted a little while ago anyway, so yep.

3 Likes

Lol wtf autocorrect :see_no_evil::grinning_face_with_smiling_eyes: Yes, I guess LV is making sense for more and more pieces here.

2 Likes

Yes. The javascript wizardry I was referring to is simply:

window.addEventListener("popstate", function(e) 
{   location.reload();
});
2 Likes

Hmm, actually another way around it would be LiveView, wouldn’t it? Even the cached page would establish soviet connection and start receiving changes in real time, right?

You are correct, if you are using LV and are jumping between views using live_redirect there’s no need for any additional JavaScript. When you hit the browser’s back button your LV will perform a stateful mount and call mount and handle_params. Where did you place the logic that fetches data from the DB? As long as it’s triggered from these 2 callbacks, you should be fine. Maybe put some debug code in mount and handle_params to convince yourself that they are being called.

1 Like