My controller has a function that changes the browser's URL, but I'm struggling to maintain the original previous page when the user navigates back.
Here's an example:
Let's say a user is on /home and then moves to /article1. As they scroll through the page, I update the URL to /article2 using:
$location.path('article2');
However, when the user hits the back button while on /article2, the URL switches back to /article1, when I actually want to redirect them to /home.
I've tried the following code:
var previousPage = document.referrer; // Get the previous location on page load
history.pushState({}, '', previousPage); // Alter the history by adding the previous page
But this method doesn't work because the history state is automatically changed when updating the URL using $location.
Is there a way to preserve the navigation as intended?