Rails seems to be serving the page twice
this is what I see in my local log file when I make a single page request:
Started GET "/" for 127.0.0.1 at Mon Apr 25 22:12:22 -0700 2011
User Load (0.9ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Processing by PagesController#beta as HTML
Rendered pages/beta.html.erb within layouts/beta (16.2ms)
Completed 200 OK in 23ms (Views: 18.8ms | ActiveRecord: 7.7ms)
Started GET "/" for 127.0.0.1 at Mon Apr 25 22:12:23 -0700 2011
User Load (0.6ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Processing by PagesController#beta as */*
Rendered pages/beta.html.erb within layouts/beta (17.9ms)
Completed 200 OK in 27ms (Views: 21开发者_JS百科.5ms | ActiveRecord: 6.6ms)
Anyone have any ideas what could be causing this? the Processing line is interesting?
PagesController#beta as HTML
PagesController#beta as */*
What does that mean?
I've seen this happen because of an img
tag with a blank src
attribute. Giving an img
, script
, link
or similar tag a src
or href
attribute =""
will resolve to the current page. So it will try to load the page a second time as it tried to load the asset. It could also be in a CSS url()
property or an AJAX call.
One easy way to tell is to temporarily put something like <base href="http://example.com" />
in your <head>
section. That will prefix any links on the page, which should prevent your local page from being called twice if it is that sort of problem.
I've also seen some browser extensions that query the page in a separate request. I've noticed this specifically with the Web Server Notifier and Web Technology Notifier extensions for Chrome. Try an "incognito" window or a separate browser and see if you get the same result.
I experienced this before and the culprit was poor implementation of Turbolinks (on my end).
I had to learn the proper way of getting Turbolinks to work (I've included my learning resources at the bottom) ... watch the Ryan Bates video to see what a properly working implementation looks like by the way.
Problem/Culprit:
Is Javascript. As described on the Turbolinks Github page ...
With Turbolinks pages will change without a full reload, so you can't rely on DOMContentLoaded or jQuery.ready() to trigger your code. Instead Turbolinks fires events on document to provide hooks into the lifecycle of the page.
Basically anything that implies 'run after DOM/page is loaded' will break/not-work-as-expected ... this will cause Turbolinks to not be effective. And since Turbolinks gracefully fails, your browser will load the page twice (check logs/console) to and hide the problem for you.
In my case I was able to notice the above behaviour when in 'incognito mode' ... all my Javascript that depended on document/DOM/page-ready .... broke. So ...
Solution:
If you have anything like this in your javascript (below) ...
$(function() {
alert("DOM is ready!");
});
$(document).ready(function() {
alert("DOM is ready!");
});
jQuery(document).ready(function() {
alert("DOM is ready!");
});
Change to this ...
$(document).on('page:change', function() {
alert("DOM is ready!");
});
Turbolinks don't actually don't request for an entirely new page so you'll notice your Javascript only works on full page loads but not when you click from a Turbolink enabled link.
Which means that we should rely on Turbolinks to detect what DOM ready actually is.
In my opinion the current answers are (could) wrong as I have Chrome extensions, blank-hrefs and my problems are gone.
Further reading:
- How Turbolinks Work
- Turbolinks Railscast #390 by Ryan Bates (video)
We had this issue with a blank background url css, forcing a page reload with rails.
If the cover_url param was blank, it would force a page reload.
"style" = "background: url( #{cover_url} );"
This behavior was occurring for me due to the SPOF-O-Matic Chrome extension. Disabling it removed the extra request. You can disable it from chrome://extensions/
Following on from the clues above I started disabling Chrome extensions one by one. Eventually I disabled AdBlock Plus extension and this cured the problem. Strange thing is, when I re-enabled the extension it still worked. I have no idea what this might have been.
Something to try, but I simply closed the tab and openened a new one and this resolved the problem for me.
Just adding the issues I had which was a combination of what has been mentioned above plus the order of turbolinks in the application.js
First issue
Turbolinks was required in the wrong place
//= require jquery3
//= require jquery_ujs
//= require popper
//= require bootstrap
//= require turbolinks
//= require_tree .
First Solution
Require turbolinks just after jquery3
//= require jquery3
//= require turbolinks
//= require jquery_ujs
//= require popper
//= require bootstrap
//= require_tree .
After this change I noticed the pages were loading much faster but still loading twice.
Second Issue
AddBlocker extension
Second Solution
Use Incognito
精彩评论