CSP: script-src-elem и Serviseworker
Обычный сервис-воркер
const staticCacheName = 's-cache-app-v1';
const dynamicCacheName = 'd-cache-app-v1';
const assetUrls = ['index.html', 'offline.html'];
self.addEventListener('install', async () => {
const cache = await caches.open(staticCacheName);
await cache.addAll(assetUrls);
});
self.addEventListener('activate', async () => {
const cacheNames = await caches.keys();
await Promise.all(
cacheNames
.filter(name => name !== staticCacheName)
.filter(name => name !== dynamicCacheName)
.map(name => caches.delete(name))
)
});
self.addEventListener('fetch', e => {
const { request } = e;
const url = new URL(request.url);
if (url.origin === location.origin) {
e.respondWith(cacheFirst(request));
} else {
e.respondWith(networkFirst(request))
}
});
async function cacheFirst(request) {
const cached = await caches.match(request);
return cached ?? await fetch(request);
}
async function networkFirst(request) {
const cache = await caches.open(dynamicCacheName);
try {
const response = await fetch(request);
await cache.put(request, response.clone());
return response;
} catch {
console.log(caches);
const cached = await cache.match(request);
return cached ?? await caches.match('/offline.html');
}
}
и выдает ошибку
Refused to load the script 'http://localhost:3000/offline.html' because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
Получается запрет на выполнение сторонних скриптов. Много перепробовал чего (и в манифест добавлял СSP и через <meta http-equiv="...">
) Чет ничего не получается.