XMLHttpRequest and cache...

Talk about add-ons and extension development.
Post Reply
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

XMLHttpRequest and cache...

Post by heavensdoor »

Hi all.
I need to download a document in background.
I use XMLHttpRequest of course... it works ok but if the doc is in the cache it loads from there and I don't wish this. I need always a fresh copy every time. How can I do this ?

Extra info:
- I don't wish to put in the URL extra arguments ( like '&' + d.getTime() ... )
- The pragma & cache-control seem to be useless...

Here's my code:

Code: Select all

XHRequest = new XMLHttpRequest();
XHRequest.onload  = OnLoad;
XHRequest.onerror = OnError;
XHRequest.open( 'GET', sURL, true );
// XHRequest.setRequestHeader( 'Expires', 'Sat, 1 Jan 2005 01:00:00 GMT' );
// XHRequest.setRequestHeader( 'Last-Modified', '-1' );
XHRequest.setRequestHeader( 'Pragma', 'no-cache' );
XHRequest.setRequestHeader( 'Cache-Control', 'no-cache, must-revalidate' );
XHRequest.send( null );


I always got the cached version of the document...
Any comments is appreciated!
Bye bye,
-Mat-
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

You could always use necko interfaces directly, there's a flag on nsIRequest allowing you specify that the request must bypass cache.
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post by heavensdoor »

asqueella wrote:You could always use necko interfaces directly, there's a flag on nsIRequest allowing you specify that the request must bypass cache.

Can you provide me some lines?
It would help a lot...

I saw what the Reload button does to bypass the cache... ( nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE ) ... but I don't have a browser object nor an hidden frame to load the content and I don't wish this...
doron
Posts: 935
Joined: November 4th, 2002, 4:50 pm

Post by doron »

THe reason I think is that the server needs to send that in the response - setting no-cache on a request is probably silly.

Changing the server to send the no-cache header is what you need.
If you see a marquee, clap your hands!
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post by heavensdoor »

doron wrote:THe reason I think is that the server needs to send that in the response - setting no-cache on a request is probably silly.
Changing the server to send the no-cache header is what you need.

Nice point of view :)
But... in this way, how could the browser obtain a refresh of the current document bypassing the cache? It doesn't change anything on the server-side...
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

Doron reacted to your confused example in the first post. There certainly is a way to bypass cache from browser code, but it's not done through setting request headers. I'm not willing to write a complete example of using necko interfaces, but you should be able to find the code you need by looking at references and examples for nsIChannel/nsIRequest. (Also lxr.mozilla.org is your friend.)
richwklein
Posts: 331
Joined: November 24th, 2002, 8:20 pm
Location: Iowa
Contact:

Post by richwklein »

you can use the xmlhttprequest to bypass the cache. After calling the open method on the request, you need to do an instanceOf(nsIchannel) then set the load flags.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a>
<a href="http://tipbar.mozdev.org">Tip of the Day</a>
<a href="http://urlnav.mozdev.org">Location Navigator</a>
<a href="http://finder.mozdev.org">Finder</a>
<a href="http://rsszilla.mozdev.org">RSSzilla</a>
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post by heavensdoor »

asqueella wrote:Doron reacted to your confused example in the first post.

Confused example... :)
BTW nothing wrong with Doron's post.
I explained my point of view.

asqueella wrote:I'm not willing to write a complete example of using necko interfaces, but you should be able to find the code you need by looking at references and examples for nsIChannel/nsIRequest. (Also lxr.mozilla.org is your friend.)

I didn't asked a complete example but some lines of code because my experience in this area is low.
And my experiments actually don't work.

BTW Don't worry, you don't need to find the solution for me.
I'll find my way. I only asked.
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post by heavensdoor »

richwklein wrote:you can use the xmlhttprequest to bypass the cache. After calling the open method on the request, you need to do an instanceOf(nsIchannel) then set the load flags.

I tried to access to the load flags throught XMLHttpRequest.channel with no good results...
But perhaps... you show me something useful... mmm...
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post by heavensdoor »

asqueella wrote:but you should be able to find the code you need by looking at references and examples for nsIChannel/nsIRequest

Got it.
From Venkman ext, in the mozilla source:

Code: Select all

var serv = Components.classes["@mozilla.org/network/io-service;1"].getService( Components.interfaces.nsIIOService );
var chan = serv.newChannel( sURL, null, null );
chan.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
var instream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
instream.init( chan.open() );
var result = "";
var avail;
while( ( avail = instream.available() ) > 0 )
    result += instream.read( avail );

( sync version )
richwklein
Posts: 331
Joined: November 24th, 2002, 8:20 pm
Location: Iowa
Contact:

Post by richwklein »

You have to make sure and access the channel after open is called.
Here is basically what we do in Forecastfox

Code: Select all

var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);      
request.open("GET", query, true);   
request.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
if (request.channel instanceof Components.interfaces.nsISupportsPriority)
  request.channel.priority = Components.interfaces.nsISupportsPriority.PRIORITY_LOWEST;
request.send(null);
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a>
<a href="http://tipbar.mozdev.org">Tip of the Day</a>
<a href="http://urlnav.mozdev.org">Location Navigator</a>
<a href="http://finder.mozdev.org">Finder</a>
<a href="http://rsszilla.mozdev.org">RSSzilla</a>
heavensdoor
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post by heavensdoor »

richwklein wrote:You have to make sure and access the channel after open is called.
Here is basically what we do in Forecastfox

Great. It works like a charm :)
( my eng is not very good but this expression should be right in this context :) )

However, is there a special reason to low the priority level of the request... ?
I was looking <a href="http://developer.mozilla.org/en/docs/Changing_the_Priority_of_HTTP_Requests">here</a>... but... mmm.

Thank you very much Rich!
richwklein
Posts: 331
Joined: November 24th, 2002, 8:20 pm
Location: Iowa
Contact:

Post by richwklein »

We set a lower priority to try and not interfer with page loads, but really no other reason.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a>
<a href="http://tipbar.mozdev.org">Tip of the Day</a>
<a href="http://urlnav.mozdev.org">Location Navigator</a>
<a href="http://finder.mozdev.org">Finder</a>
<a href="http://rsszilla.mozdev.org">RSSzilla</a>
Post Reply