I want to take the user to the home page when a user clicks on the back button of the browser if the user is in my angular platform.
There is an edge case as well which is to be handled, if user has come to our platform via google search, Fb or direct entering link, then we cant this behavior but if he ia already roaming around in our platform then we don't want each back button to take user to home.
i.e.
User Enter the link and press button => take him to home.
User Enters the link and now clicks on a button or link and app state is changed, now user clicks browser back button => take user back to previos page (normal behaviour)
Demo: click on the link below and and then click on the 1st link on google search and then try hitting browser back button you will see it takes you to the home page.
https://www.google.com/search?ei=EnmkXJmYCIGLvQTinL_ICw&q=gap+men+shirts+ae&oq=gap+men+shirts+ae&gs_l=psy-ab.3...6313.6796..6983...0.0..0.141.399.0j3......0....1..gws-wiz.......0i71j0i22i30j0i22i10i30.To9hLinHj7I
I tried to implement it using pushState()
ngAfterViewInit() {
console.log('init called');
var x = document.referrer;
console.log('ref', x);
console.log(this.angLocation.path());
if (x) {
console.log('pushing state');
this.location.pushState({id:'page3'}, 'Page3', '/page3');
}
}
Click on the link below see it in action https://angular-tet5fy.stackblitz.io/page2?fbclid=IwAR1zP_UTnCQxKMp8o3ZXf-n4NR3ftu_4JoulLqItaONa-_wSPJ_DT95OgRU
Expected behavior: 1. Clicking on the link takes the user to page2. 2. While page2 is initialized it should push state '/page3' to the browser history. 3. When the user clicks the browser back now it should take to /page3 as it is was pushed to history in step2 above.
Current Behaviour: 1. Clicking on the link, takes me to page2 but it automatically redirects to /page3 while pushing state. 2. When clicked browser back, it takes me back to /page2.