Backbone.js Routing: From hash to hashbang

1.5k Views Asked by At

I have a legacy backbone.js app that I'm currently retrofitting to be properly crawlable by search engines. I've settled on using prerender.io's Node.js + Phantom.js system to serve static HTML renders of my javascript powered site to search engines. One of the requirements for using prerender.io however is that all hash URLs be converted to hashbang (so site.com/#gallery should be site.come/#!gallery). My site currently only uses hash for url routing. How can I switch this to a hashbang?

1

There are 1 best solutions below

0
On

I suppose you have to change the url attributes of the anchor links pointing to #gallery to #!gallery and modify your router accordingly, e.g.

var app = app || {};
(function($){
  Workspace = Backbone.Router.extend({
    routes: {
      '!gallery': 'gallery',
      ..
    },
    ..
    gallery: function() {
      ..
      this.navigate('!/gallery', {trigger:true});
    }
);