mtrpcic/pathjs — Issue #13: HTML5 pushState triggers path dispatch for initial page load
Author: micheljansen
Created: 2011-12-08 11:16:15+00:00
State: open

When using the HTML5 history mode, like in the "HTML5 Basics" example at http://mtrpcic.github.com/pathjs/html5_basic.html,
the initial page load also triggers a Path dispatch. I realise that in a lot of cases this is desirable, but I feel there should be a way to disable this.

A common use case for Path is using it to catch links out and replace them with ajax requests that replace partial content: The initial page is rendered fully and every subsequent click on a link fetches just the content that is new.

However, if you use Path in HTML5-mode, it also triggers an AJAX request for the first request, essentially causing duplicate work. Of course there are ways to hack around this, but it seems to me there should be a more elegant way to solve this.

I have given a simple example below. On the first visit of /pages/test, it says "loading..." and fires off an XHR to /partials/test. This is unnecessary, as the content of /pages/test already contains what it needs prerendered by the server.

Path.map("/pages/:page").to(function(){

$("#content").html("loading...");

$.ajax({
url: "/partials/"+this.params["page"],
timeout: 2000,
success: function(data) {
$("#content").html(data)
}
});
})

$(document).ready(function(){
Path.history.listen();

$("a").click(function(event){
event.preventDefault();
Path.history.pushState({}, "", $(this).attr("href"));
});
})