I am currently building a site using React and GatsbyJS and trying to conditionally add a navigation window.history.back(-1);
if the previous page was part of the same domain.
An example is
navigateBack = () => {
if (window !== undefined) {
window.history.back(-1);
}
};
note that if (window !== undefined)
allows window
to be used without server side rendering.
How can I check the previous page before running this function.
For example is the user navigated from a external link I want to use navigate('/stories/')
, else if the user came from an internal page, i.e. '/stories/2/'
I want them to go back using window.history.back(-1);