today I had the marvelous joy of making my app work with IE 6. Time to fall to
ones knees and praise Bill - for sure. Such a great browser!
Just one little peculiarity made me stumble - for a few hours only, nothing
really worth mentioning. Caching. Which prevented my beautiful JSON requests
to be actually issued.
Now my colleagues work with dojo, and after I questionend them how they've
setup their server to prevent caching, they tell me that there is a generated
parameter attached to each request that will prevent caching - a timestamp I
presume.
So - is there any chance such a thing could be made (optionally) part of the
Async-package?
Regards,
Diez
> So - is there any chance such a thing could be made (optionally) part of the
> Async-package?
If a patch is provided, why not. Until then, you have several options:
* define the cache control in your web server (with pragma no-cache and
cache-control).
* Add an argument to loadJSONdoc(): loadJSONdoc('http://toto',
{'no-cache': new Date().getTime()})
--
Thomas
I use Math.random to generate a random number with every request. This is
discarded by the application.
--
Jorge Godoy <jgo...@gmail.com>
There's always the chance of getting the same random number twice.. A
timestamp with enough precision is generally better. It's *extremely*
unlikely that a single user is going to get the same exact timestamp
twice... there would have to be something seriously wrong with their
UA for that to happen.
-bob
> There's always the chance of getting the same random number twice.. A
> timestamp with enough precision is generally better. It's *extremely*
> unlikely that a single user is going to get the same exact timestamp
> twice... there would have to be something seriously wrong with their
> UA for that to happen.
What's the precision for the timestamp? If it doesn't get to at least
milliseconds then it starts being a problem with daylight saving time,
when the time change occurs.
--
Jorge Godoy <jgo...@gmail.com>
Date.prototype.getTIme() is defined as an integer number of
milliseconds since the epoch. Daylight savings time or any other
timezone change is totally irrelevant, regardless of the precision.
Even if it was relevant, the chances of doing a request precisely one
hour after a previous request during the one hour per year in which
that happens is a whole lot less than getting the same random number
twice during any session at any time of the year. Especially
considering the fact that most normal people are sleeping during (at
least) the second 2am...
-bob
See the patch attached.
One turns on cache prevention via
MochiKit.Async.PREVENT_CACHING_PARAM = 'preventCaching';
The set value is then used as parameter-name. As by your suggestion, I use the
timestamp.
Works great for me so far. I chose the global variable way because I wanted to
be as unobtrusive as possible - after all, if you want this, I presume you
will want it everywhere, and hunting down all async calls that possibly need
an additional parameter is considerably harder.
Diez
> Even if it was relevant, the chances of doing a request precisely one
> hour after a previous request during the one hour per year in which
> that happens is a whole lot less than getting the same random number
> twice during any session at any time of the year. Especially
> considering the fact that most normal people are sleeping during (at
> least) the second 2am...
Time changes earlier here in .br, for example. Anyway, point taken and it is
better using the timestamp then.
--
Jorge Godoy <jgo...@gmail.com>
var d = doXHR(url, {
method: 'POST',
'mimeType': 'text/plain',
'headers': [['Accept', 'application/json']]
}).addCallback(MochiKit.Async.evalJSONRequest);
-bob
-bob
POSTing is a fine idea if and only if the server expects it. POSTing
by default is a really bad idea because it's semantically not a POST
operation. It also thwarts caching altogether, when you really only
want to thwart caching for volatile documents. Not all documents are
volatile.
For example, the MochiKit documentation generates an index dynamically
by fetching the XHTML documents and parsing out the function
definitions. These XHTML documents really don't change much at all and
should be cached.
-bob
Date.getTime is in milliseconds since the epoch, but browsers rarely
support the full resolution. On my machine, Firefox/2.0/Win32, the
minimum resolution is about 10 milliseconds. You can test this by:
var y = (new Date).getTime() ;
for ( var x = 0 ; x < 100000 ; x++ ) {
if ( y != (new Date).getTime() ) break ;
}
alert( x + ":" + (new Date).getTime() - y ) ;
Which should give 1:1 but on my machine gives about 500:10.
So multiple calls to getTime can return the same number, although if
you're using it for network stuff, the latency will probably stop this
from being a problem. When I need a unique number I append a global
counter onto getTime, like:
var newunique = (new Date).getTime() + "" + (unique++) ;
You can't just use a global counter, because restarting a browser or
reloading the page will give non-unique values. It's still not
strongly unique across multiple windows, but it's not too bad.
I'm against any attempt to make having a dontCache parameter the
default - cherrypy throws when receiveing a method call with an
unknown parameter name, and I suspect other frameworks will do the
same.
A much better solution is server side - IE seems to ignore plenty of
headers which should make it not cache, but this combination seems to
work:
request.setHeader( 'Expires', "Mon, 26 Jul 1997 05:00:00 GMT" )
request.setHeader( 'Cache-Control', 'no-cache, must-revalidate' )
request.setHeader( 'Pragma', 'no-cache' )
Caching is a general issue, not just related to XMLHttpRequest - IE
will also often ignore most caching headers for images, and other
dynamically inserted elements, so server-side headers or src mangling
is needed for them too.
Hamish Friedlander,
Mind Control Dogs