Fakito
Fakito

Reputation: 95

Backbone Router root duplicates in the url

I've just finished developing my first Backbone app and I detected a small quirk with the Router which I don't seem to be able to fix.

My routes look like this:

routes: {
  '': 'index',
  'jobs/:id': 'viewJob',
  '*default': 'notFound'
}

It works fine when serving the app from the server root, but it doesn't when I serve it from a subfolder. In that case I always get the default route.

I though adding the root param to the backbone history would do the trick so I added it like this:

Backbone.history.start({ pushState: true, root: '/subdir/' });

With this the app seems to load as expected but the Router is automatically adding the root to all the routes and it ends up being duplicated, so when I first visit the website:

http://mysite.com/subdir/

It loads the app and changes it to this:

http://mysite.com/subdir/subdir/

This makes the app to break when reloading the page or using the browser back button as that route doesn't really exist.

What would be the approach to avoid this? I don't want to hardcode the folder name in the routes as it might change or be served from the root.

UPDATE: I've just realized I was adding the duplicated folder name myself using router.navigate somewhere in my code. I just removed it and everything works as expected.

Upvotes: 1

Views: 1331

Answers (1)

Morgan ARR Allen
Morgan ARR Allen

Reputation: 10678

Just a small guess.. do you really want pushState turned on? This can cause problems on reload if the server is not setup to serve out of the new directory.

Upvotes: 1

Related Questions