문제

I'm trying to access the offline application cache of a shared webworker (HTML5) with no luck. I've been banging my head against this problem for many hours, so I must be missing something... Any help from a JavaScript Ninja out there would be highly appreciated!

The W3C the spec says that:

cache = self.applicationCache

(in a shared worker) should return the ApplicationCache object that applies to the current shared worker.

I'm spawning a shared worker from my app's main script via:

var worker = new SharedWorker('js/test.js');
worker.port.addEventListener('message', function(e) {
    alert('got message: ' + e.data);
}, false);
worker.port.start();
worker.port.postMessage('hi there...');

And here's the code of my shared worker (test.js):

var cache = self.applicationCache;

onconnect = function(e) {
    var port = e.ports[0];
    port.onmessage = function(e) {
        // test.html contains a <html manifest='test.manifest'> tag 
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", "test.html", false);
        xmlHttp.send(null);
        var result = xmlHttp.responseText;
        port.postMessage(result);
        port.postMessage('cache: '+ cache);
}

}

The alerts I'm getting are:

  1. the contents of test.html (as I expected)
  2. the message "cache : undefined" (oops!)

I tried this on Google Chrome 7.0.517.44 and Safari 5.0.2 (Mac OS X 10.6.4). I also tried to trigger the HTTP GET before accessing the cache and many other variations, but all of these attempts resulted with the same outcome.

Am I missing something obvious? Is this is a known limitations of the browsers I've tested?

Many Thanks,

Ori

도움이 되었습니까?

해결책

I found the same thing - though to be honest, I'm not even sure WHY we'd want access to the applicationCache object... I thought it just, cached things?! Anyway - when I was trying to get it to work I found this thread talking about it:

http://lists.w3.org/Archives/Public/public-webapps/2009OctDec/0519.html

I assumed that I could just stick and entry in my cache.manifest file of the main page that referenced the worker file and it would populate the applicationCache magically. But it didn't appear to (I just got undefined like you did).

In the w3c spec, in the Processing Model section it says:

If worker global scope is actually a SharedWorkerGlobalScope object (i.e. the worker is a shared worker), and there are any relevant application caches that are identified by a manifest URL with the same origin as url and that have url as one of their entries, not excluding entries marked as foreign, then associate the worker global scope with the most appropriate application cache of those that match.

But I can't make it work!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top