Anna
Anna

Reputation: 554

davis.js generating two requests on page load/reload

In davis.js when I reload or load a page in chrome/safari it will generate two requests to my route. When I click on a link it only generates one request. In firefox loading, reloading and clicking on a link will generate only one request. I only want one request to fire so I don't redraw my content and do my animations twice.

Is there a way I can fix this so I only ever get one request? Should I put a timeout on my route so it can only send one request per x milliseconds? That sounds like a hack though.

Explanation of what I'm doing: I have a single index page which uses the route to load different content in. It grabs the data from an API and loads an underscore template then places it in the content container.

I am using the latest version of davis.js 0.9.2.

.htaccess setup:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /catalog/index.php [QSA,L]

davis setup:

var app = Davis(function(){
    this.configure(function(){
      this.generateRequestOnPageLoad = true;
    });

    this.get(directory, function(){
      RenderPage('index');
    });

    this.get(directory+':id', function(req){
      RenderPage(req.params['id']);
    });

    this.get(directory+':id/:page', function(req){
      RenderPage(req.params['id'], req.params['page']);
    });
});

Firefox console:

[Tue Aug 21 2012 16:02:01 GMT-0400 (EDT)] application started
[Tue Aug 21 2012 16:02:01 GMT-0400 (EDT)] runRoute: GET /catalog/users

Chrome console:

[Tue Aug 21 2012 16:01:59 GMT-0400 (EDT)] application started
[Tue Aug 21 2012 16:01:59 GMT-0400 (EDT)] runRoute: GET /catalog/users
[Tue Aug 21 2012 16:01:59 GMT-0400 (EDT)] runRoute: GET /catalog/users 

Upvotes: 0

Views: 579

Answers (1)

Oliver Nightingale
Oliver Nightingale

Reputation: 1835

I have pushed a fix for Davis (0.9.3) that should fix these issues.

At the root of the problem is the difference in the way that webkit browsers (specifically Chrome and Safari) and other browsers treat the onpopstate event.

The current version of both Chrome and Safari fire an onpopstate event for the initial page load, both Firefox and Opera (and possibly others) do not do this. This difference causes quite a headache when implementing a cross browser library on top of the history API.

This causes strange cross browser issues with Davis including routes being fired run twice in Chrome and the first route not being run in Firefox if you are navigating using the back button.

The solution is to keep track of whether the browser has issued the popstate event and whether it is one for an initial page load or not.

Upvotes: 1

Related Questions