baseprime / grapnel

The smallest JavaScript router with named parameters, HTML5 pushState, and middleware support
http://grapnel.js.org
468 stars 40 forks source link

Hashbang detection when sharing URL #52

Closed IngwiePhoenix closed 8 years ago

IngwiePhoenix commented 9 years ago

When client A uses a browser that does not support the pushState mechanism, they may link the URL to another user to share it. That user then opens the URL - but from hat I can see in the code, the hashbang is not checked for, only i the mode is set to be the hashchange one.

Client A: http://example.com/#!one/route Client B: http://example.com/one/route

Client B can share URL with client A - but possibly not vice versa.

A "if hashbang available, open that route" kind of fallback would be nice. :)

baseprime commented 8 years ago

This is the downfall of the front-end router. There are solutions to this, but none of them are unobtrusive. However, since Grapnel supports multiple routers, you could do something like this:

var regularRouter = new Grapnel({ pushState: true }),
    hashBangrouter = new Grapnel({ hashBang: true });

regularRouter.get = function(){
    hashBangrouter.get.apply(hashBangrouter, arguments);
    Grapnel.prototype.get.apply(this, arguments);
}

regularRouter.get('/one/route', function(req){
    // Do stuff
});

I have not tested this, but it should work. Either way, you get the idea. It basically allows you to add routes using the regularRouter pushState router and have them automatically listen for hashBang routes of the same path.

Hope this helps! @IngwiePhoenix

IngwiePhoenix commented 8 years ago

Thank you! I am so sorry I replied that late.

But this is pretty much what I was lookng for :)