Showing posts with label Backbonejs. Show all posts
Showing posts with label Backbonejs. Show all posts

Wednesday, February 19, 2014

SEO - Single Page Websites

In this article, we will see how to optimized the ranking of your single page application. Today, the single page website development is really common. It exists a lot of frameworks to create single page website like AngularJS, BackboneJS...

But the problem when you try to index your website in Google or Bing, just the html page will be index. That means just 1 entry with only 1 title and description. It is not really good  if your website is composed of many pages.

Adapt your webapp

Google provides a solution to crawl the dynamically created content :
  • Don't use only the # as Hash fragment, replace it by #!
    • Google will replace the hash fragment with exclamation mark by _escaped_fragment_
    • http://www.exemple.com/#toto => http://www.example.com/#!toto
    • Url GoogleBot : http://www.example.com/?_escaped_fragment_=toto
  • Add a trigger to the head of the HTML of a page
    • <meta name="fragment" content="!">
Now Google or Bing will be able to crawl all the page of your website.  The next step is to return the html page or content for the good hash fragment.

Adapt your Backend

If you use NodeJs to host you webapp my suggestion is to read this good post : Easily index your SPA thanks to PhantomJS

In my case, I could not use this trick because my single page app is hosted on Apache and I can not install external tools...
So my solution is to use the .htaccess to create a redirection on pre-render pages. To pre-render a page you can use PhantomJS or an other tools to create a html file for each page. 

You can create an automatic script to generate these pages. Also you can modify the title and the description meta tag for each page.
After create a folder seo/ on your server, hosted all your html pages on it. 

.htaccess

Create the .htaccess file in your root folder, with this following content :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ seo/%1.html [NC,L]
</IfModule>

This code means if the url contains _escaped_fragment_=, we redirect the call to the prerender html page.
The html must have the same name of your hash fragment toto.html in my previous example.

You can test your rule with the "Fetch as Google" in Crawl menu on Google Webmaster Tool.

Now Google, Bing and the other can index all the pages of your single page application!!!

Sources : Google specification Ajax Crawling

Friday, November 23, 2012

Pattern MVC BackboneJs

L'objectif de cet article est de proposer plusieurs modélisation UML d'un projet de WebApplication avec la librairie Javascript BackboneJs.

Router > View > Model

Tout d'abord une modélisation très simple, qui comprend un router, une view et un model. 

Modélisation UML Architecture simple Backbone

Cette modélisation est un bonne solution si on est dans un contexte relativement simple. C'est à dire que l'on a qu'un seul Model, que l'on affichera grâce à le View.

Le router permet d'instancier la view à laquelle il passera le model à afficher.

  1. Router = Backbone.Router.extend({
  2.   routes: {
  3.          "Page1": "maPage1",   // http://monsite/#Page1
  4.          "*actions" : "defaultRoute" //http://monsite/
  5. },
  6. maPage1 : function(){
  7.      var view = new MaView({model : monModel, viewContainer : monContainer})
  8. }
  9. });

Router > Collection Model > View

Maintenant passons à une modélisation un peu plus complexe.
Modélisation UML avec une collection de model

 Cette fois on souhaite afficher une liste de model. Dans cette architecture, la View permet l'affichage d'une collection de Model. 
C'est à dire que dans la fonction Render(), nous allons faire un foreach() pour chaque élément de la liste, et générer directement le code HTML que l'on ajoutera à notre conteneur HTML de la liste.
  1. render : function (){
  2.      this.collection.each(function (item){
  3.              myContainterListe.append($(template(item.toJSON())));
  4.      }
  5. }

Ce principe n'est pas optimal notamment si l'on souhaite gérer des évènements sur les items de la liste générée par le View. 

Router > Collection > View > Model > View 

L'architecture présentée ci-dessous à une granularité plus fine que celle ci-dessus. 
Modélisation UML avec une view pour la collection et une view par item

On souhaite toujours afficher une liste de Model, mais à la différence le modélisation précédente, on crée une view pour la collection et une view pour les items de la collection.

Cette fois la fonction render() de la View de la collection, va faire appel ,pour chaque item, à la fonction Render() de la view des items.

  1. ListView = Backbone.View.extend({
  2.         renderItem: function(model){
  3.               var itemView = new ItemView({model : model});
  4.               itemView.render();
  5.         },
  6.         render : function() {
  7.                this.collection.each(this.renderItem);
  8.         }
  9. });


On pourra donc ajouter des évènements sur notre view Item, pour ajouter du comportement à nos items.

Pour ajouter des évènements sur une Backbone.View :


  1.  events :
  2. {
  3.      "click .classname" : "mafonction"
  4. }