Workbox will not load

2019-08-12 23:43发布

问题:

I'm trying to use Workbox to add PWA functionality to my website. I'm following the Get Started guide, but I'm not getting far. When I run the website in Chrome I get the following error:

Refused to load the script 'https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval'".

Per the docs, I am importing workbox-sw.js in my service worker file that is in my wwwroot folder with the following line:

importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js');

The "Refused to load..." error occurs on that import statement.

How can I get rid of this error?

回答1:

You have a CSP configuration that prevents loading scripts from third-party origins and then you are trying to load a script from Google. You either need to allow the script to load or load the page from your site.

  1. Update the CSP header to allow the Google domain by adding script-src 'self' 'unsafe-eval' https://storage.googleapis.com.

  2. Using Local Workbox Files Instead of CDN