我一直在尝试在 IIS 托管网站中使用服务工作线程来缓存该网站的一些静态内容。该站点是使用 Windows 身份验证的内部应用程序。我已经能够注册并运行服务工作线程,没有太多麻烦,但是一旦我打开缓存并开始向缓存添加文件,承诺就会因授权失败而失败。返回的HTTP结果是401 Unauthorized。这是前几个请求的通常响应,直到浏览器和服务器能够协商授权。
我很快就会发布一些代码来帮助解释。
EDIT
var staticCacheName = 'app-static-v1';
console.log("I AM ALIVE");
this.addEventListener('install', function (event) {
console.log("AND I INSTALLED!!!!");
var urlsToCache = [
//...many js files to cache
'/scripts/numeral.min.js?version=2.2.0',
'/scripts/require.js',
'/scripts/text.js?version=2.2.0',
'/scripts/toastr.min.js?version=2.2.0',
];
event.waitUntil(
caches.open(staticCacheName).then(function (cache) {
cache.addAll(urlsToCache);
}).catch(function (error) {
console.log(error);
})
);
});
由于缺乏代码,这只是一个猜测,但如果你正在做类似的事情:
caches.open('my-cache').then(cache => {
return cache.add('page1.html'); // Or caches.addAll(['page1.html, page2.html']);
});
你正在利用隐式的Request
对象创建 https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#cache-addAll-method(参见第 6.4.4.4.1 节)当您将字符串传递给cache.add()
/cache.addAll()
. The Request
创建的对象使用默认值凭证模式 https://fetch.spec.whatwg.org/#concept-request-credentials-mode,即'omit'
.
你可以做的是显式构造一个Request
包含您想要的凭据模式的对象,在您的情况下可能是'same-origin'
:
caches.open('my-cache').then(cache => {
return cache.add(new Request('page1.html', {credentials: 'same-origin'}));
});
如果您有一堆要传递数组的 URLcache.addAll()
, 你可以.map()
它们对应的数组Request
s:
var urls = ['page1.html', 'page2.html'];
caches.open('my-cache').then(cache => {
return cache.addAll(urls.map(url => new Request(url, {credentials: 'same-origin'})));
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)