Search code examples
javascriptsessionmeteornavigationiron-router

How navigate back to search page?


What is the Meteor/Iron Router way to jump back to a search page after visiting the single view result? I'm using history.back() at the moment, but it sometimes messes up. And it won't work for visitors that land directly on the single view page. I'm trying out Session but I'm not sure what parameters are good practice to use. Or maybe there's another approach? Thanks! ~ Kai


Solution

  • Actually iron:router have a method called Location.back()

    Also seems like history.back() its a good option too.

    Check this Add 'previousPage' and 'backMethods' or this feature request

    If you only want to do this on the search template try with this code.

    // onStop offshoot is executed whenever we LEAVE a lane
    Router.onStop(function(){
      // register the previous route into a Session
      Session.set("previousLocation",this.location.path);
    });
    
    // onBeforeAction is executed before indeed going to a new lane
    Router.onBeforeAction(function(){
      // getting the previous route
      var previousLocationPath=Session.get("previousLocation");
      // now lets back to search route.
      if(previousLocation ==="/search"){
        this.redirect("search");
      }
      this.next();
    });
    

    Take this code from this Previous Page location on ironRouter tutorial