【发布时间】:2021-02-21 04:02:10
【问题描述】:
是否可以将 preconnect 和 preload 添加到 CSP 元标记?
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Security-Policy"
content="base-uri 'self'; object-src 'none'; script-src 'nonce-pb+8iY9WcWmhlEX6d1cjqA=='; style-src 'nonce-IOaKe0RIRR+SDo/VCRRk/g==' 'nonce-1akTtzYTJ2M1nq/GqmKH8Q==' 'nonce-bebsmDPpynkvU6DpcJMXOA=='"/>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
rel="preconnect"
as="fetch"
href="https://fonts.gstatic.com"
crossorigin
/>
<link
rel="preload"
as="style"
href="https://fonts.googleapis.com/css2?family=Inter:wght@200;400;500;700&family=Roboto+Slab:wght@400;500&display=swap"
/>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Inter:wght@200;400;500;700&family=Roboto+Slab:wght@400;500&display=swap"
media="print"
onload="this.media='all'"
nonce="IOaKe0RIRR+SDo/VCRRk/g=="
/>
我可以添加“style-src”策略,但不确定如何为preconnect 和preload 添加https://fonts.gstatic.com 和https://fonts.googleapis.com 域。
上下文:
这可能与 Webpack 有关,因为我使用 webpack 作为捆绑器,使用 index.html 作为模板。其次,我正在构建一个静态 SPA 并在 s3 中提供服务并在云端进行缓存。
我正在使用想法来加快谷歌字体加载速度 https://csswizardry.com/2020/05/the-fastest-google-fonts
【问题讨论】:
标签: html security webpack content-security-policy