【发布时间】:2019-06-19 07:32:17
【问题描述】:
我有一个渐进式网络应用程序。我正在使用 Google 字体,并且在我的服务人员中使用 workbox。
我的内容安全政策定义为:
// Omitting all the other directives
style-src 'self' https://fonts.googleapis.com 'unsafe-inline';
font-src 'self' https://fonts.gstatic.com;
connect-src 'self';
我已经按照配方here 设置了工作箱来缓存字体。代码如下:
workbox.routing.registerRoute(
/^https:\/\/fonts\.googleapis\.com/,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'google-fonts-stylesheets',
})
);
workbox.routing.registerRoute(
/^https:\/\/fonts\.gstatic\.com/,
new workbox.strategies.CacheFirst({
cacheName: 'google-fonts-webfonts',
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30,
}),
],
})
);
这里的问题是,当我尝试在浏览器(Google Chrome / Safari)或独立应用程序中加载我的应用程序时,字体不会加载。经过多次拉扯,Chrome终于在控制台中给了我一个错误:
Refused to connect to 'https://fonts.googleapis.com/css?family=Montserrat|Quicksand&display=swap' because it violates the following Content Security Policy directive: "connect-src 'self'".
Uncaught (in promise) no-response: no-response :: [{"url":"https://fonts.googleapis.com/css?family=Montserrat|Quicksand&display=swap","error":{}}]
at o.makeRequest (https://storage.googleapis.com/workbox-cdn/releases/4.2.0/workbox-strategies.prod.js:1:3983)
GET https://fonts.googleapis.com/css?family=Montserrat|Quicksand&display=swap net::ERR_FAILED
看来我也需要在connect-src 下声明谷歌字体。我没有看到任何地方提到过(我搜索了很多)所以我想知道这是一个错误还是我确实需要在connect-src CSP 指令中定义字体?
【问题讨论】:
标签: http-headers progressive-web-apps content-security-policy workbox