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

Comments

Projects
None yet
@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

This comment has been minimized.

Show comment
Hide comment
@Couto

Couto 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

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

This comment has been minimized.

Show comment
Hide comment
@Ngorror

Ngorror 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();
        }

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

This comment has been minimized.

Show comment
Hide comment
@indexzero

indexzero Jun 22, 2013

Member

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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@rymohr

rymohr 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.

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

This comment has been minimized.

Show comment
Hide comment
@indexzero

indexzero Jun 24, 2013

Member

@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.

Member

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

This comment has been minimized.

Show comment
Hide comment
@bwiggs

bwiggs 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.

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

This comment has been minimized.

Show comment
Hide comment
@jdreux

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

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

This comment has been minimized.

Show comment
Hide comment
@ranadeep47

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

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

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

@nicolashery

This comment has been minimized.

Show comment
Hide comment
@nicolashery

nicolashery 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.

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

This comment has been minimized.

Show comment
Hide comment
@rubiii

rubiii Apr 23, 2014

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

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

This comment has been minimized.

Show comment
Hide comment
@emrahayanoglu

emrahayanoglu Apr 28, 2014

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

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

@bkwdesign

This comment has been minimized.

Show comment
Hide comment
@bkwdesign

bkwdesign 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

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