The popstate event fires when the user uses the back/forward buttons.
window.addEventListener("popstate", (event) => {
console.log(event.state); // State from pushState
// Update your UI based on the new state
});
This is how single-page apps handle the back button.