ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

VUE - 启动 PWA ,使用 service-worker 缓存静态文件

2022-02-22 10:31:55  阅读:181  来源: 互联网

标签:缓存 service registerRoute worker VUE routing new workbox strategies


VUE - 启动 PWA ,使用 service-worker 缓存静态文件

 

方法1:https://www.jianshu.com/p/8f3ad5021b0a

方法2:https://www.cnblogs.com/lcosima/p/14537877.html

 开发环境:vue2,vuecli4

 

方法1:

在main.js 引用 sw配置

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('./sw.js').then(function (registration) {
    console.log('Registration successful, scope is:', registration.scope);
  }).catch(function (error) {
    console.log('Service Worker registration failed, error:', error);
  });
}

 

 

在 public 中 创建 sw.js 文件

'use strict';
//使用阿里的CDN
importScripts('https://g.alicdn.com/kg/workbox/3.3.0/workbox-sw.js');

workbox.setConfig({
  modulePathPrefix: 'https://g.alicdn.com/kg/workbox/3.3.0/'
});

if (workbox) {
  console.log(`Yay! Workbox is loaded`);
} else {
  console.log(`Boo! Workbox didn't load`);
}
workbox.routing.registerRoute(
  // Cache CSS files
  /.*\.css/,
  // 使用缓存,但尽快在后台更新
  workbox.strategies.staleWhileRevalidate({
    // 使用自定义缓存名称
    cacheName: 'css-cache',
  })
);
workbox.routing.registerRoute(
  // 缓存JS文件
  /.*\.js/,
  // 使用缓存,但尽快在后台更新
  workbox.strategies.staleWhileRevalidate({
    // 使用自定义缓存名称
    cacheName: 'js-cache',
  })
);

// 模型缓存
workbox.routing.registerRoute(
  new RegExp('http://tile.railplus.com/'),
  workbox.strategies.staleWhileRevalidate({
    cacheName: 'model-cache',
  })
);

// 模型缓存
workbox.routing.registerRoute(
  new RegExp('.+\.b3dm$'),
  workbox.strategies.staleWhileRevalidate({
    cacheName: 'model-cache',
  })
);

// 模型缓存
workbox.routing.registerRoute(
  new RegExp('.+\.gltf$'),
  workbox.strategies.staleWhileRevalidate({
    cacheName: 'model-cache',
  })
);
// 模型缓存
workbox.routing.registerRoute(
  new RegExp('.+\.glb$'),
  workbox.strategies.staleWhileRevalidate({
    cacheName: 'model-cache',
  })
);

// workbox.routing.registerRoute(
//   // 缓存gravatar文件
//   new RegExp('https://cdn\.v2ex\.com/'),
//   // 如果缓存可用,请使用它
//   workbox.strategies.cacheFirst({
//     // 使用自定义缓存名称
//     cacheName: 'gravatar-cache',
//     plugins: [
//       new workbox.expiration.Plugin({
//         // 缓存最多30天
//         maxAgeSeconds: 30 * 24 * 60 * 60,
//       })
//     ],
//   })
// );

 

 

如下这些文件是从缓存读取的,标识为 (serviceworker)

 

 

 

 方法2:

 

 

 

 

 

 

 

 

 配置:

大部分路由都内置的缓存策略来处理的。

  • Stale While Revalidate(重新验证过期)
    • 当前策略会在缓存有效的情况下使用缓存响应,同时在后台使用网络更新缓存。(如果缓存无效的情况下,会等到网络请求响应再使用。)这是一种相对安全的策略,意味着用户会定期的更新缓存 。缺点就是,它总是请求网络,会占用用户的带宽。
  • Network First(网络优先)
    • 当前策略会优先从网络请求并响应,如果接到响应,将把它传给浏览器,同时进行缓存。如果网络请求失败,会使用最新的缓存资源进行响应。
  • Cache First(缓存优先)
    • This strategy will check the cache for a response first and use that if one is available. If the request isn’t in the cache, the network will be used and any valid response will be added to the cache before being passed to the browser.
    • 当前策略会检查缓存资源是否有效,如果有效,会优先使用缓存进行响应。如果请求的资源不在缓存中,会再请求网络,并将有效的响应先加载到缓存中,再传给浏览器使用。
  • Network Only(仅网络)
    • 强制从网络请求资源。
  • Cache Only(仅缓存)
    • 强制从缓存请求资源。
workbox.routing.registerRoute(
  match,
  new workbox.strategies.StaleWhileRevalidate()
);
 
workbox.routing.registerRoute(
  match,
  new workbox.strategies.NetworkFirst()
);
 
workbox.routing.registerRoute(
  match,
  new workbox.strategies.CacheFirst()
);
 
workbox.routing.registerRoute(
  match,
  new workbox.strategies.NetworkOnly()
);
 
workbox.routing.registerRoute(
  match,
  new workbox.strategies.CacheOnly()
);

 

 

 路由匹配实例

workbox.routing.registerRoute(
  '/logo.png',
  handler
);

workbox.routing.registerRoute(
  'https://some-other-origin.com/logo.png',
  handler
);

workbox.routing.registerRoute(
  new RegExp('\\.js$'),
  jsHandler
);
 
workbox.routing.registerRoute(
  new RegExp('\\.css$'),
  cssHandler
);

workbox.routing.registerRoute(
  new RegExp('/blog/\\d{4}/\\d{2}/.+'),
  handler
);

workbox.routing.registerRoute(
  new RegExp('.+/blog/\\d{4}/\\d{2}/.+'),
  handler
);


workbox.routing.registerRoute(
  new RegExp('.+\\.js$'),
  jsHandler
);
 
workbox.routing.registerRoute(
  new RegExp('.+\\.css$'),
  cssHandler
);

const matchFunction = ({url, event}) => {
  // Return true if the route should match
  return false;
};
 
workbox.routing.registerRoute(
  matchFunction,
  handler
);

 

 

 

 

参考:https://www.jianshu.com/p/8f3ad5021b0a

参考:https://www.cnblogs.com/lcosima/p/14537877.html

参考:https://blog.csdn.net/lw001x/article/details/103694534

 

标签:缓存,service,registerRoute,worker,VUE,routing,new,workbox,strategies
来源: https://www.cnblogs.com/1285026182YUAN/p/15921906.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有