kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 195 forks source link

replace previous state #551

Open gsabran opened 8 years ago

gsabran commented 8 years ago

It seems that withReplaceState doesn't replace state, but instead doesn't write new state to the history.

FlowRouter.route('/route', {
  name: 'route',
  action: function() {
    FlowRouter.withReplaceState(function() {
      FlowRouter.go('silentRoute');
    });
  }
});
FlowRouter.go('route');

Will go to silentRoute and the history will be [route];

Would it be possible to get the opposite, ie:

FlowRouter.route('/silentRoute', {
  name: 'silentRoute',
  action: function() {
    FlowRouter.withOverwriteState(function() {
      FlowRouter.go('route');
    });
  }
});
FlowRouter.go('silentRoute');

Where the history will be [route]

Use case is when silentRoute is a tracking route that I don't want to keep in the history. Sorry I could not understand the code well enough to do a PR

funkyeah commented 7 years ago

I too ran into this issue. Which happens to be the same issue as https://github.com/kadirahq/flow-router/issues/585. I couldn't figure out exactly what is supposed to happen but I think I tracked it down to this section of router.js:

  // Implementing idempotent routing
  // by overriding page.js`s "show" method.
  // Why?
  // It is impossible to bypass exit triggers,
  // because they execute before the handler and
  // can not know what the next path is, inside exit trigger.
  //
  // we need override both show, replace to make this work
  // since we use redirect when we are talking about withReplaceState
  _.each(['show', 'replace'], function(fnName) {
    var original = self._page[fnName];
    self._page[fnName] = function(path, state, dispatch, push) {
      var reload = self.env.reload.get();
      if (!reload && self._current.path === path) {
        return;
      }

      original.call(this, path, state, dispatch, push);
    };
  });

self._current.path === path is not true when the problem arises. self._current.path is the correct withReplaceState URL but path is the previous URL

but path is always passed to original.call

funkyeah commented 7 years ago

@arunoda, I'd be happy to try and submit a pull request for this if you find a moment to weigh in on whether I might be on the right path