Web worker file is cached and never re-loaded in I

2019-04-11 20:53发布

I am running a simple HTML5 app that works in Chrome and Firefox. It uses a web worker, as in:

var worker = new Worker("the/worker/URL/Code.js");

I have experimented for over an hour in IE, and I finally found that the web worker's code is never reloaded. When I get the version that it has, to throw an error, the debugger shows me a completely outdated version of the worker code file, even though, all other files have been reloaded properly.

I flushed the cache, using the standard advice found everywhere: Safety -> Delete Browsing History -> Select items -> Ok -> Wait -> Ctrl+F5 to reload -> BAM, thee debugger still shows 100% the same file as several hours ago (remember that reloading works as expected in Chrome and FF).

When I look at the Network profiler, I see:

URL Protocol    Method  Result  Type    Received    Taken   Initiator   Wait‎‎  Start‎‎ Request‎‎   Response‎‎  Cache read‎‎    Gap‎‎
/js/core/WorkerScriptCode.js    (Pending...)    GET (Pending...)    (Pending...)    0 B (Pending...)    webworker   1311    0   0   0   0   31

I don't know why it says "Pending"; I can see that the worker already runs: I can see the work of the worker being done (e.g. the importScripts calls show up, and there are also above mentioned stacktraces). But it simply runs a completely outdated version, even though I flushed the entire cache tens of times.

Is this an uber-bug, or am I being stupid?

1条回答
Summer. ? 凉城
2楼-- · 2019-04-11 21:06

The same problem exists in Chrome. In order to get around it you can use a cache buster:

Main Code:

var buster = randomNumberOrBuildNumber;
var worker = new Worker( "the/worker/URL/Code.js?buster=" + buster );

Worker:

importScripts( 'script/to/import.js' + self.location.search );

This will apply ?buster=12345 (or whatever your randomNumberOrBuildNumber is set to) to both the loading of the worker and any importScripts() calls the worker makes.

查看更多
登录 后发表回答