Basically, what I wish to achieve is to trigger an event when the user navigates the page. I've seen many solutions out there that seem kind of hacky, so here's one of the solutions I have adopted (I'm open to better solutions as long as it works for the majority of apps, not just SPAs):
window.addEventListener("beforeunload", (event) => {
this.save() // <- This function is meant to save the state of the current page
});
The this.save() Method :
save = () => {
const currentUrl = window.location.href
this.flowState.push({ // <- Storing it in an array before saving the entire thing to sessionStorage
url: currentUrl,
});
sessionStorage.setItem('FLOWSTATE', JSON.stringify(this.flowState))
}
The problem is, this solution saves the target URL instead of the source URL, which introduces bugs to my program. Is there a good way to solve this in 2020?