Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django URL conf and Backbone.js Router

I have a backbone.js single-page app that is all set up with the router (well, actually a Backbone.Marionette app with a Backbone.Marionette AppRouter, but nevertheless). However, the backend is based in Django, where I do not have the URL conf directing to views for all URLs that are already in the backbone.js routes.

Based on the existing URLs in the Django URL conf, Backbone.js will serve the backbone routes regardless of what is listed in the Django conf - it seems something, anything just needs to be there.

Do I need to have proper Django views in order to offer a fallback for older browsers/SEO?

What are the best practices to coordinate the Django URL conf and the Backbone.js Router?

like image 843
snakesNbronies Avatar asked Dec 17 '12 03:12

snakesNbronies


2 Answers

I've found a post that addresses this issue quite well:

http://duganchen.ca/single-page-web-app-architecture-done-right/

Briefly, my reasoning for including a fallback is for non-javascript browsers and SEO reasons. At the time of this post, non-javascript browsers account for ~1.4% (less than 2% from everything I've read) of users, making SEO The major consideration. Again, SEO may not be relevant for everyone reading this post, in which case, this can be skipped.

I found Thomas Davis' tutorial using phantom.js quite helpful. http://backbonetutorials.com/seo-for-single-page-apps/

However, another issue that I needed to account for was the history API, which has been neglected by all but the latest IE browsers. Given my client's users, about 15% of which are using IE <= 9, this was also a problem.

In the end, I also needed to use history.js. All in all, this was a lot of work to update an otherwise very simple website. However, I learned a lot from this ordeal.

like image 113
snakesNbronies Avatar answered Oct 17 '22 15:10

snakesNbronies


In my opinion if your backbone app is truly a single page then you don't need any django views whatsoever. You can serve your index.html as a static file (in production, not even by django) and then let backbone's router take care of your url configuration, as you're doing already. You can use backbone's history and navigate to fake urls, add urls parameters etc, for resources in your app.

like image 25
elynch Avatar answered Oct 17 '22 14:10

elynch