axefrog / cycle-router5

A router driver for Cycle.js, wrapping the router5 library
29 stars 4 forks source link

Apply current route at the start #6

Open ivan-kleshnin opened 9 years ago

ivan-kleshnin commented 9 years ago

Direct browser access to /about results in defaultRoute applied. Only JS navigation works.

So router basically is not aware that it's alredy "other" page and navigation event had to be sent...

Following http://router5.github.io/docs/api-reference.html It seems that startPathOrState from router5.start([startPathOrState], [done]) should be used.

However

function main({DOM, router}) {
  ...
  return {
    DOM: vtree$,
    router: Observable.from([["start", window.location.pathname + "?" + window.location.search]])
  }
}

seems to has no effect.

I can surely debug this but maybe you'll answer quicker.

axefrog commented 9 years ago

I had this problem also; I had to use startWith to issue an initial event. This is router5's behaviour, but I might flag this as something to add in myself. I'm in the middle of completely rewriting the driver to properly support router5 features and to allow for scoping support (as per cycle/core issue 167).

Here's what I'm exposing in my own code in my router.js file:

function getRouterActions(router) {
  return {
    route$: router.addListener()
      .startWith({ toState: router.matchPath(location.pathname) })
      .map(change => {
        log.trace(`Route changed to ${change.toState.name}`);
        return {
          name: change.toState.name,
        };
      }),
    router$: Rx.Observable.just('start').delay(100)
  };
}