admin管理员组

文章数量:1296392

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);

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);

Share Improve this question asked Apr 1, 2019 at 10:23 DarrenDarren 2,29010 gold badges48 silver badges104 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

I've had to address this by checking to make sure there's a history to go to, and then by verifying that the current host can be found in the referrer. If those are found then I know they came from within my site, so I go back; otherwise, I navigate them elsewhere.

if(window.history.length > 1 && 
document.referrer.indexOf(window.location.host) !== -1) {
  window.history.back();
}
else {
  <do something else>
}

Gatsby has an api that'll tell you the previous location. Perhaps you can use it together with document.referrer like @ATT suggested to check if previous url were from the same domain.

// gatsby-browser.js

exports.onRouteUpdate = ({ location, prevLocation }) => {
  if (prevLocation || document.referrer.includes(SITE_NAME)) {
    // same domain
  }
}

Alternatively, if you want to have previous location information in your React ponent, check out the answers to this other gatsby question.

Side note: document.referrer will be blank ("") instead of null or undefined if there were no referrer (site omit referrer, or direct)

You can get the URL of the previous page by document.referrer.

If you are using React Router, you can use the following snippet.

if(browserHistory.getCurrentLocation().key === null) {
   browserHistory.push(customRoute); // push to custom route
} else {
   browserHistory.go(-1); // go back normally
}

Answered here for a version that works also when the window is "new" and the referrer might not be reliable https://stackoverflow./a/68161306/1348517

本文标签: