概述字体加载影响首屏与可读性。本文给出 `font-display` 设置与前端缓存协同。CSS 与 HTML 设置@font-face {

font-family: 'Inter';

src: url('/fonts/inter.woff2') format('woff2');

font-display: swap;

}

<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>

SW 缓存示例self.addEventListener('fetch', event => {

if (event.request.destination === 'font') {

event.respondWith((async () => {

const cache = await caches.open('font-v1');

const cached = await cache.match(event.request);

if (cached) return cached;

const res = await fetch(event.request);

await cache.put(event.request, res.clone());

return res;

})());

}

});

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部