Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Durandal 2.0 router can you replace # for #! for ajax web crawling purposes?

Is it possible to replace the default router behavior in Durandal 2.0 to replace the default route for example: 'mysite.com/#/myroute' with 'mysite.com/#!/myroute' the reason being is that in order for the google spider to detect the page is ajax crawl-able, i need to insert a hashbang in the url not just a hashtag. Any help would be greatly appreciated.

Thanks!

like image 416
ccorrin Avatar asked Aug 25 '13 10:08

ccorrin


1 Answers

As far as I know, in Durandal 2.0 you are not able to do this. The router and history plugin do not support this, and it would not be an easy fix since there are some places in the code that rely on the #.

However, I don't think this is a problem in your scenario. Maybe you need to change the way to deal with the requirement of making your application crawlable.

You still can make your application SEO compatible using #by specifying the meta fragment type in the main HTML of the application:

<meta name="fragment" content="!">

So you are telling google that your links don't have the #!but that the application is using JavaScript rendering. Then the requests from the crawler will include ?_escaped_fragment_.

So in your app you will use:

mysite.com/#myroute

and the crawler will request:

mysite.com?_escaped_fragment_=myroute

Check the Section 3 of Google's documentation on crawling to know more about the meta fragment tag.

SUGGESTION

If you want to fully take advantage of this feature I suggest using pushState in Durandal 2.0 by activating the router like this:

router.activate({ pushState: true } );

And also including the meta fragment tag.

 <meta name="fragment" content="!">

Things you should consider if using pushState:

  • Use pushState only if you are not planning to support old browsers.

  • You need to make your server side pushState ready, this means that when the server is requested with mysite.com/myrouteshould be able to return the same JS application and let the client process the query string parameters. For example, this can be achieved using IIS URL rewrite if you are using ASP.NET. There's a good blog post about this topic.

like image 104
margabit Avatar answered Oct 19 '22 17:10

margabit