### Introduction The blog requires loading third-party fonts, which can be quite large, a whopping 16MB. As a result, I sought to optimize the on-demand loading of fonts and leverage Service Workers. #### On-Demand Font Loading In a typical CSS setup, fonts are automatically loaded, but every time the webpage is refreshed, they are downloaded again. To address this, I used JavaScript to create a binary blob file in the browser, caching the font. Here's the code: ```js fetch('https://api.yuhenm.com/api/file/source?name=LXGWWenKaiGBScreen.woff', { cache: 'force-cache' // Force cache }) .then(response => response.blob()) .then(blob => { // Create a temporary URL for the font file const fontUrl = URL.createObjectURL(blob); // Create a 发表评论 取消回复 使用cookie技术保留您的个人信息以便您下次快速评论,继续评论表示您已同意该条款 评论 * 私密评论 名称 * 🎲 邮箱 * 地址 发表评论 提交中... Blog Optimization with Service Workers and Caching 玉衡 • 2023 年 11 月 06 日