Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial route not firing #199

Closed
rymohr opened this issue Jun 6, 2013 · 12 comments
Closed

Initial route not firing #199

rymohr opened this issue Jun 6, 2013 · 12 comments

Comments

@rymohr
Copy link

@rymohr rymohr commented Jun 6, 2013

It seems if the initial page load (in browser) already contains the local route then the route doesn't fire.

routes =
  '/test': -> debugger

router = new Router(routes)
router.init()

The only way I've found to get the initial route to trigger is with this little hack:

initialRoute = window.location.hash.replace(/^#/, '')
router.setRoute('/')
router.setRoute(initialRoute)

Anyone else experiencing this?

@Couto
Copy link

@Couto Couto commented Jun 17, 2013

I can confirm that I suffer from the same bug as you do.
That solution works fine except the fact that #/ gets hanged, and you have to remove it with pushState to get a clean initial URL

    var initialRoute = window.location.hash.replace(/^#/, '');
    router.setRoute('/');
    router.setRoute(initialRoute);
    if (!location.hash.replace(/^#\/*/, '') && (history && history.pushState)) {
        history.pushState("", document.title, window.location.pathname + window.location.search);
    };

Obviously this only works on browsers with pushState

@Ngorror
Copy link

@Ngorror Ngorror commented Jun 21, 2013

try this brutal hack in https://github.com/flatiron/director/blob/master/build/director.js

change row 251:

        listener.setHash(url.join('/'));

with next rows:

        var currentUrl = dloc.hash;
        listener.setHash(url.join('/'));
        if (currentUrl == dloc.hash) {
            listener.fire();
        }
@indexzero
Copy link
Member

@indexzero indexzero commented Jun 22, 2013

Isn't this the expected behavior (although it still sucks)? i.e. if you navigate to /#foo the hash hasn't actually changed it just started at foo. Seems like the work around provided by @Ngorror is the best solution.

@rymohr
Copy link
Author

@rymohr rymohr commented Jun 24, 2013

@indexzero I don't follow your logic on this one. The whole point of this library is to handle your routing needs. You shouldn't have to hack it to get your first route to work. It's a bad assumption to think everyone will always start from the root route.

@indexzero
Copy link
Member

@indexzero indexzero commented Jun 24, 2013

@islandr I'm actually agreeing with you, but that's not how the browser sees it. If you go to /#foo the hash hasn't changed, so the onHashChanged event never fires.

What I'm really saying is that the suggested fix by @Ngorror isn't actually a hack, it's just how it has to be.

@bwiggs
Copy link

@bwiggs bwiggs commented Jul 21, 2013

I'm suffering from the same issue. I'm trying to have links that are shareable. So a user might send their friend a link like: /#/search?query here. As stated above, the initial route doesn't get picked up.

@jdreux
Copy link

@jdreux jdreux commented Dec 5, 2013

I found that running the following on page load fixed it for me (without changing the director source):

window.dispatchEvent(new HashChangeEvent(window.location.hash));

@ranadeep47
Copy link

@ranadeep47 ranadeep47 commented Dec 27, 2013

@jdreux window.dispatchEvent(new HashChangeEvent(window.location.hash)); is giving InvalidStateError in chrome .And for me this worked

window.dispatchEvent(new HashChangeEvent("hashchange"));

@nicolashery
Copy link

@nicolashery nicolashery commented Jan 13, 2014

For me, this seems to be fixed in 1.2.2?

I used to have a fix like the ones suggested above, but after upgrading to 1.2.2, it looks like it's irrelevant and that all I need is router.init('/') for the initial route to fire.

@rubiii
Copy link

@rubiii rubiii commented Apr 23, 2014

i’m running v1.2.2 and router.init('/') works for me. maybe we can add this to the readme?

@emrahayanoglu
Copy link

@emrahayanoglu emrahayanoglu commented Apr 28, 2014

It works for me in v 1.2.2. You can init the first routing with using the router.init('/') .

@bkwdesign
Copy link

@bkwdesign bkwdesign commented Aug 29, 2017

I'm using Version 1.2.6, but am having a slightly different problem.
My routing works so long as I delay the call to router.init()
So, I wrap it within a slight 500 millisecond timeout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.