Invert Your Mind

NEWS:
02 May

Is your browser requesting a page twice?

For the initial release of Naymz, Michele Lopatin and I have been tracking down a bug that was causing the browser to fetch the same URL twice. In order to track this down I had to systematically comment out lines of HTML in the page until it stopped and then uncomment lines until it came back to figure out exact what was requesting the page the second time. This was a huge issue because it essentially doubled the load on the server and the bandwidth usage.

As it turns out in FireFox (and possibly IE, but I’m not sure as I run Linux completely now), empty image src attributes cause the browser to request the same URL of the current page the browser is pointing to. This is really DUMB on the FireFox developers (or the W3C if it is in the specification) part since this should just error out:

<img src=”"/>

Instead they decided to try requesting the URL of the current page again in hopes that it would magically return an image, which of course it is never really gonna do because the current URL is what your are rendering. Lame. At least we figured it out before launching and getting pounded with double requests.

26 Responses to “Is your browser requesting a page twice?”

  1. 1
    Dan Moore Says:

    I haven’t noticed that, but looking over my blog stats I was seeing some bizarre prefetch behavior (two pages being requested by a browser in a very short time span). This article sorted it out for me: http://www.petefreitag.com/item/312.cfm.

    Actually, doing a bit of research reveals that what IE and Moz are doing is according to the spec. The src attribute of an image is a URI, and according to the definition of a URI:

    4.2. Same-document References

    A URI reference that does not contain a URI is a reference to the
    current document. In other words, an empty URI reference within a
    document is interpreted as a reference to the start of that document,
    and a reference containing only a fragment identifier is a reference
    to the identified fragment of that document.

    I will admit that this is rather counter intuitive for an img src.

  2. 2
    Brian Pontarelli Says:

    This seems like a cheap way to allow links to link to the current page, which really is the same as a refresh, but besides that behavior I can’t think of any other URIs that need to be the current page. This just seems dangerous to me. They should have made this point to some universally non-existent URI such as page:null or something.

    Then again we all know how I feel about the current web! Haha

  3. 3
    Chopped Liver Says:

    Why in the world do you have img src tags that reference nothing to begin with? I think the spec is lame too, but couldn’t the author of that spec say the same about your page with lame empty tags!?

    Sorry.. had to ask..

  4. 4
    Brian Pontarelli Says:

    Well, mister smarty pants! Being a nihilist I like empty images. In fact, images that have colors, dimensions, shapes, yeah they really piss me off! The beauty of nothing is absolute.

    Ooooooorrrrrr…. You might have a variable that ends up being empty for some reason and didn’t do a null check on the page. Not a good case, because it’s essentially a bug, but if the spec just said to ignore these img tags it wouldn’t bite you quite so hard.

    example:

    <img src=”${foo.bar}”/>

  5. 5
    Chopped Liver Says:

    Wow! Great example. I never really understood what means. A check for null. Genius! I wanted to understand more about this “null” you speak of and found:
    http://en.wikipedia.org/wiki/Null_%28computer%29

    Being enslaved by the German community, I can respect your nilhilist stance.

  6. 6
    虚拟主机 Says:

    Why in the world do you have img src tags that reference nothing to begin with

  7. 7
    Brian Pontarelli Says:

    See my comment above. If you have a variable that is null in your JSP, ASP, PHP, whatever, you might end up with an image with an empty source attribute. Now don’t get me wrong, this doesn’t mean that it isn’t a bug, but that bug could eat up some serious band-width on the server since each request is done at least twice if not more.

  8. 8
    Josh Says:

    This doesn’t seem to happen in IE.

    I ran into it when I didn’t specify an SRC because I am waiting for a Graphic Designer to furnish images for my project.

    Instead, I specified alt text, incorrectly assuming that being a “good w3 developer” by specifying the ALT text would prevent my browser from executing a GET for my page again.

    Of course, I wasn’t conscisouly thinking that specifying ALT would prevent this , because it’s completely ridiculous that it does this in the first place!

  9. 9
    Brian Pontarelli Says:

    So does the ALT tag prevent it from requesting the current URL again or is it just IE not following the crazy W3C specification (which is normal so I won’t be astonished if that’s the case)?

  10. 10
    Malvolio Says:

    IE, amazingly, does something worse: it requests the directory the current page belongs to. I understand the underlying logic. If the page /x/y.html has the tag , the browser requests /x/a — so degenerately should cause a request to /x/

    Madness.

  11. 11
    Madhu Says:

    Hi All,

    I dont think this is happening because of img tags with src=”". I had the same problem with IE and i am not using img tags anywhere in my page. It is working well in Firefox and Netscape. Is there any other reasons?

  12. 12
    Brian Pontarelli Says:

    This is definitely a by-product of img src=”" tags in the page, but I would imagine that IE does this for a bunch of other things as well. I would start by commenting out the entire JSP/ASP/RHTML/PHP (or whatever you are using) file and then slowly uncommenting each piece until you figure out which line of code is causing the double request. Then purge it with the force of a thousand lions, or something like that.

  13. 13
    Madhu Says:

    Hi All,

    I found the root cause of this issue. This is happening becuase of “input type=’image’” tags. Two my ASP pages are using these tags to submit the page with the help a javascript function on onclick event. Now i changed these tags to “img”, the issue got resolved.

    Thanks,
    Madhu.

  14. 14
    nick Says:

    I found that this also occurs (in Firefox1.5, Opera8.0, not IE7.0) when there is a blank url reference in the page’s in-line css.
    (But not if the css file is linked.)

    Firebug for Firefox helps alot!

  15. 15
    nick Says:

    above chopped my code; this

    style='background:#fff url();'

  16. 16
    Jeramy Says:

    Hi,

    You really saved my beeswax with posting this comment, i uncommented a line in my PHP menu object which disabled any images being placed before them, so empty tags which also kept creating a dublicate request which was starting to mess the rest of the site up, but god thanking for find this comment i’ve saved hours of debugging my PHP to see if it was creating a problem.

    Cheers again. :)

  17. 17
    Aaron L. Says:

    Thanks for pointing this out! I was using the onerror attribute of the img tag to try to initiate some javascript that would replace broken images with a placeholder image, but the double-requests were making a mess out of my cookies… I had a feeling the empty tags might by the reason, so I googled < empty src tag on img > and found your site. Thanks for the confirmation!

  18. 18
    Jerry Says:

    No Wonder. I had this problem about 1-2 years ago. One of our PHP pages creates user account, and if the new account coincides with an existing account, an error message is displayed, indicating the account exist. With firefox, each time a totally new account is created, it mentions that the account exist. It seems there’s a double page request there. What happens is that the new account is created, but the successful message is not shown, indicating the output was not sent to the browser internally. Then the second request is made and the error message is shown, this time to the browser. This is not a page refresh at all, but only the second request is being displayed to the browser. We spent loads of time going through every line of PHP code, but could never find the bug. In case of a GET or POST page, firefox will do it twice, and could really ruin dynamic scripts, if certain checks are not in place!

  19. 19
    Tony Estrada Says:

    I found that the double request is done whenever the response contains the tag in the body…
    I narrowed it down to just creating an a.html file in my http server dir, and requesting that file

    test 1:
    contents: hello
    results: one request processed

    test 2:
    contents: hello
    results: two requests processed

    test 3:
    contents: hello
    results: two requests processed

    so it appears that ff checks for

  20. 20
    Tony Estrada Says:

    need to view source to see the html around hello sorry

    ust don’t put

  21. 21
    Tony Estrada Says:

    Ok, finalized the issue here.
    Returning any mark up language tags that require a src URL as a parameter but none is specified….

    so,

    will make firefox re-request the original URL

  22. 22
    James Says:

    This issue had me stumped for a long time because a site I was developing had no images, external stylesheets or javascript sources whatsoever, yet still two requests were being made to the server for every single page!

    Worse still, the response from the second of those requests instantly overwrote the response from the first yet left only one entry in the browser’s history object - this made debugging pretty tricky.

    Anyway, after much furrowing of brow and tea-drinking the root cause of this problem seems to have boiled down to an extra “;” (semi-colon) in the meta “Content-Type” tag. I had …

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8;”

    instead of …

    meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″

    (removed the semi-colon from the charset).

    It was an oversight on my part, but it’s strange that Firefox decides to behave in this manner (IE seemingly doesn’t mind the extra semi-colon).

  23. 23
    Alexander Says:

    It’s crazy but after removing an image with null src attribute resolved the issue. crazy from the FF side

  24. 24
    Finn Says:

    Same issue, but different.

    First of all, thanks for all the above, it helped me to figure out a solution to my version of the problem.

    My problem:
    - FireFox loaded a certain page twice, no clue why.
    - NO src=”" attribute in any of my img tags
    - The charset=utf-8 did not have the semi-colon in the end but this lead me to the answer

    After locating the problem by commenting out line by line I found that the charset meta was causing the problem. At some point I changed the utf-8 to iso-8859-1 and the page loaded only once! Switching it back to utf-8 introduced the problem again.

    It seems that my source php files should have had the same encoding as the charset you give the output xhtml. Changing the php-file encoding into utf-8 solved the problem.

    I found the answer on the following site (Section 1.5):
    http://www.imarc.net/communique/view/310/9_expert_css_ideas_you_should_think_twice_about_before_using

    Enjoy (en)coding!

  25. 25
    Rhys Says:

    I had this issue only in Firefox 2.0.0.13. Safari 3.1 and Opera 8.54 did not exhibit this behavior. Several of my POST requests were processed twice resulting in 2 (and later 3 for some reason) comment entries etc for each submission. Found this via google and then located an empty src in one of my templates. Removed and all in now fine. Thanks!

  26. 26
    Valery Says:

    Сайт, да нет это целый портал - супер. Буду теперь постоянно посещать…. и всем советую… Кстати глянька мой сайт

Leave a Reply

© 2008 Invert Your Mind | Entries (RSS) and Comments (RSS)