1 + 1 = 3
Saturday, June 30th, 2007or
the curious Firefox double page request
One of my current web-related projects involves a simple page counter. A page counter is really a very simple thing: each time a specific page is requested by a client, the page count should increase by one. The stateless nature of the web means that we need to persist this page count value somewhere – it could be a simple flat file, or it could be a database – whatever suits your needs. Whatever you choose, it’s really not that hard: 1 + 1 = 2 + 1 = 3 + 1 = 4 etc…
I was pretty upset to find that my page counter (which I wrote a few months ago and was working correctly) had suddenly started incrementing by two for each page request… WTF??? I scoured the code for any instances where I was updating the “PageViews” field in my table (there was only one, and it looked like it was only adding one each time). I checked that I hadn’t accidentally executed that SQL twice (nope – nothing had changed there and it all looked normal). I started wondering if, after too much red wine, I’d mistakenly coded a database trigger to auto-increment the page views somehow… but nope…
After fruitlessly wading through my code for several hours, I started typing random words into Google hoping to find the answer. I quickly stumbled upon this. My thanks go out to Mr Brian Pontarelli, as he has spent many more hours than I tracking down the source of the bug… Yup – the problem is that Firefox will request a page twice if your HTML markup contains an img tag with an empty src attribute. I’ve been reading through the comments on Mr Pontarelli’s blog, and a lot of people were asking why you’d have an empty src tag for an image in the first place. I was dynamically assigning the value using javascript when the page had loaded, and therefore the double-load problem was occurring.
Just another one of those little gotchas you’ve have to look out for as a developer I guess…