Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js Routing: From hash to hashbang

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?

like image 925
rgb Avatar asked Oct 21 '22 18:10

rgb


1 Answers

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});
    }
);
like image 186
brontoluke Avatar answered Oct 27 '22 09:10

brontoluke