Reload a webpage when it's loaded from the back/forward cache in the browser (Safari)

Published 18 July 2023 13:24 (1-minute read)

Sometimes, your page will stay in the previous state in the Safari browser. A simple fix is to reload the page when your visitors enter the page.

The following event can be used:

        window.onpageshow = function(event) {
            // reload page if coming from back/forward cache (Safari)
            if (event.persisted) {
                // jQuery('body').html(''); // When needed, you can clear the current html from the page to prevent a swap feeling for the visitor
                window.location.reload(true);
            }
        };

Based on the `event.persisted` element, it will simply reload the current page.

Robin Dirksen

Follow me on Twitter, there I post web-related content, tips/tricks, and other interesting things.

On my blog, you can find articles that I've found useful or wanted to share with anyone else.

If you want to know more about this article or just want to talk to me, don't hesitate to reach out.