views:

757

answers:

2

I have a Cocoa app containing a WebView. I'm targeting the 10.4 SDK due to the app's installed customer base. (i.e. I cannot require Leopard.)

I have two files: index.html and data.js.

At runtime, in response to user input, I write over the data.js file, often, populating it with current data from the app.

(The data.js file is used by the index.html file on body load to populate tables within the index.html. index.html itself is not modified.)

In order to (supposedly) prevent WebKit caching of my oft-changing data, I call:

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:indexFileURL cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval:10];

and

[[myWebView mainFrame] loadRequest: urlRequest];

This works very well in Tiger and Leopard.

However, it fails in Leopard if Safari Beta 4 is installed. The drawing of the index.html file within the webview does not respect the current changes to the data.js file. It appears that the javascript file (data.js) is actually being cached, regardless of the URL's cache policy.

As a workaround, I have tried calling [[myWebView mainFrame] reload] after the loadRequest, but this causes other problems. (The webview not updating at all.)

Can anyone suggest a way to fix or work around this behaviour? Thanks.

+3  A: 

Not a solution, but a workaround that might work:

Try appending something like "?version=some_random_number" to your URL, with a different random number each time you reload. In my experience, that's pretty effective at forcing webkit to reload.

Dirk Stoop
Thanks muchly for the help. I've just tried your approach, but alas, the javascript is still being cached. :(
SirRatty
I've come up with a workaround, based on your advice. Instead of loading the index.html file as a URL, I'm now reading the contents of the file as an NSString. I then modify that string at the point where the HTML is looking for "data.js" to read: data.js?cacheBaffler=1234; // some random numberI then call loadHTMLString on the webview's mainframe. This works fine. Interesting data point: without the file name modifiation, loadHTMLString uses caching in both Tiger and Leopard, regardless of Safari 4 Beta. Great for browser test scores...
SirRatty
Glad it worked out :)
Dirk Stoop
A: 

[just closing this one off]

Javascript is still being cached in Safari 4 final, regardless of cache flags. Disappointing, especially given that this is known issue. I strongly suspect that the needs of marketing dot-points and test scores in the browser wars have won out over the need for properly working code.

However, the workarounds (see the below comments) do work.

SirRatty