Checking page refresh In JavaScript (Code Snippet)

Checking page refresh In JavaScript (Code Snippet)
const pageWasAccessedViaReload = (
    (window.performance.navigation 
    && window.performance.navigation.type === 1)
    || window.performance
        .getEntriesByType("navigation")
        .map(nav => nav.type)
        .includes("reload")
);

Recently I needed to know in an application if the user had got to the current page by performing a browser refresh. Here is a snippet of code you can use in JavaScript to check this. It uses the user timing api and as you can see the coverage is well apart from Opera Mini as always.

Subscribe to Making sense of the world around me, one blog post at a time

Donโ€™t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe