Prefetching documents
Created: Last updated:
Making the web faster is always a good idea one might think but sometimes it backfires. Your web application may all of a sudden not work as expected as if somebody is messing with it in the background? You may see sharp increases in the log files because of duplicate entries and you have no clue why a page shows up twice just within seconds?
The secret thing going on in the background might be this:
Link prefetching
One obvious problem caused by browsers is what they call link prefetching. In most cases this is business as usual but I had a serious problem because of that.
Let me say this first, though: As it looks right now only Firefox is actively using this method but I think others might follow soon.
Why browser prefetch
Lets say you want users to experience faster loading web pages. One simple way is downloading the next page while the user is still reading the current page. However, they need some assistance. Mozilla Developer Network has an excellent FAQ about how they do it. They are looking for <meta> or <link> tags in the head section; for link its the rel and next attribute. If you place these tags into the head section the browser will go ahead and download the page in the background. When the user finally clicks the link the browser can load the page from its cache.
A problem for session state
At first prefetching seems to be a wonderful thing but for me it was messing with my session state.
If you work with forms and need to know if the user is following a certain path you usually do that with session state. A prefetch can easily mess with your state.
The background conundrum
Prefetching works in the background, you do not see it unless you peek into your log files. The browser displays the initially requested page as if nothing happened. You can print some debug messages into your pages as long as you want; you will not see what you expect or helps in the browser window.
If you set a counter to your session and echo the value to your page it probably will skip a beat, every time. If you suspect your application is doing this it will drive you absolutely mad.
The log files finally convinced me that the browser was sending requests which caused my application to set a different state as well as advance the counter. Consequently the next button in my form lead to an unexpected result.
There are ways you can handle this behavior in your application but overall I believe it is easier to simply avoid the <meta> or <link> tags if you work with forms and session state.