惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
罗磊的独立博客
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
V
V2EX
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
量子位
MyScale Blog
MyScale Blog
G
Google Developers Blog
M
MIT News - Artificial intelligence
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
B
Blog

szhshp 的第三边境研究所

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 如果我用手搓了个暗物质雏形 | szhshp 的第三边境研究所
Workbox-Getting Started | szhshp 的第三边境研究所
2020-05-30 · via szhshp 的第三边境研究所

Meta

目录

Workbox-Getting Started

适用于 Workbox V5.1.2

Installation

Installation (CDN Mode)

其实很简单, 项目根目录放置一段 service-worker 的入口:

if ('serviceWorker' in navigator) {
  // Use the window load event to keep the page load performant
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('service-worker.js');
  });
}

然后新建一个文件 service-worker.js , 你们可以写上一个最简单的 registerRoute 方法:

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

const {
  registerRoute
} = workbox.routing;
const {
  CacheFirst
} = workbox.strategies;
const {
  CacheableResponse
} = workbox.cacheableResponse;

registerRoute(
  new RegExp('.*.css'),
  new CacheFirst()
);

Installation (Webpack Mode)

(TODO)

Cache Strategies

TLDR

使用 StaleWhileRevalidate() 策略

registerRoute(
  new RegExp('\.(jpg|gif|bmg|png|jpeg|gif|ico)'),
  new StaleWhileRevalidate(),
);

Detailed Explanation

workbox-strategies 这个库会暴露几个方法, 其中主要关注以下几个方法:

  • Stale While Revalidate 优先使用缓存, 如果缓存存在则使用, 并且同时请求网络, 在后台更新缓存. 如果缓存不存在则要等待网络请求结束
  • Network First 先请求网如果网络成功则会加入缓存, 如果网络失败直接使用缓存
  • Cache First 先拦截请求, 并查看缓存, 如果返程存在则直接使用, 如果不存在则放行网络请求
  • Network Only (略)
  • Cache Only (略)

Implementation

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

const {
  registerRoute
} = workbox.routing;
const {
  StaleWhileRevalidate
} = workbox.strategies;
const {
  CacheableResponsePlugin
} = workbox.cacheableResponse;
const {
  ExpirationPlugin
} = workbox.expiration;

/* cache js and css, use StaleWhileRevalidate strategy  */
registerRoute(
  ({
    request
  }) => request.destination === 'script' ||
  request.destination === 'style',
  new StaleWhileRevalidate({
    cacheName: 'notebook/cache/jsAndCss',
  }),
);

/* Cache pic for 180 days */
registerRoute(
  new RegExp('\.(jpg|gif|bmg|png|jpeg|gif)'),
  new StaleWhileRevalidate({
    cacheName: 'notebook/cache/image',
    plugins: [
      new ExpirationPlugin({
        maxAgeSeconds: 60 * 60 * 24 * 180,
        maxEntries: 30,
      }),
    ],
  }),
);

/* cache font for 1 year */
registerRoute(
  new RegExp('\.(woff|woff2|tff|ico)'),
  new StaleWhileRevalidate({
    cacheName: 'notebook/cache/fontAndIcon',
    plugins: [
      new CacheableResponsePlugin({
        statuses: [0, 200],
      }),
      new ExpirationPlugin({
        maxAgeSeconds: 60 * 60 * 24 * 365,
        maxEntries: 30,
      }),
    ],
  }),
);

Troubleshooting

Uncaught ReferenceError: importScripts is not defined

importScripts 方法只能放在 service-worker 里面执行

这个是因为 importScripts 只有在 worker 这个 object 的 context 中才能执行.

需要将 importScripts 方法放置在 navigator.serviceWorker.register('xxx.js'); 这一行这里注册的 xxx.js 这个 service-worker 里面执行

Reference: https://stackoverflow.com/questions/14500091/uncaught-referenceerror-importscripts-is-not-defined#answer-28620642