使用谷歌黑科技Service Workers为网站加速
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

Service Workers是谷歌公司推出的加速黑科技,它可以在浏览器层面动态缓存一些请求,从而使网站的二次加载速度大幅提高,今天就给大家分享一下如果在自己的博客里部署Service Workers。
需要注意的是,Service Workers 只有在 HTTPS 的前提下才能发挥作用。
添加 sw-toolbox 核心
下载 sw-toolbox.js 并且放置到主题目录下的 assets/js 目录
*如果你是默认主题,应该是在path_to_ghost/content/themes/casper/assets/js
创建缓存规则
在主题根目录下创建 serviceworker.js,并且写入下面内容
'use strict';(function () { 'use strict'; var cacheVersion = '-toolbox-v1'; var dynamicVendorCacheName = 'dynamic-vendor' + cacheVersion; var staticVendorCacheName = 'static-vendor' + cacheVersion; var staticAssetsCacheName = 'static-assets' + cacheVersion; var contentCacheName = 'content' + cacheVersion; var maxEntries = 50; self.importScripts('assets/js/sw-toolbox.js'); self.toolbox.options.debug = false; // 缓存本站静态文件 self.toolbox.router.get('/assets/(.*)', self.toolbox.cacheFirst, { cache: { name: staticAssetsCacheName, maxEntries: maxEntries } }); // 缓存 googleapis self.toolbox.router.get('/css', self.toolbox.fastest, { origin: /fonts\.googleapis\.com/, cache: { name: dynamicVendorCacheName, maxEntries: maxEntries } }); // 不缓存 DISQUS 评论 self.toolbox.router.get('/(.*)', self.toolbox.networkOnly, { origin: /disqus\.com/ }); self.toolbox.router.get('/(.*)', self.toolbox.networkOnly, { origin: /disquscdn\.com/ }); // 缓存所有 Google 字体 self.toolbox.router.get('/(.*)', self.toolbox.cacheFirst, { origin: /(fonts\.gstatic\.com|www\.google-analytics\.com)/, cache: { name: staticVendorCacheName, maxEntries: maxEntries } }); self.toolbox.router.get('/content/(.*)', self.toolbox.fastest, { cache: { name: contentCacheName, maxEntries: maxEntries } }); self.toolbox.router.get(' var cacheVersion = '-toolbox-v1'; var dynamicVendorCacheName = 'dynamic-vendor' + cacheVersion; var staticVendorCacheName = 'static-vendor' + cacheVersion; var staticAssetsCacheName = 'static-assets' + cacheVersion; var contentCacheName = 'content' + cacheVersion; var maxEntries = 50; self.importScripts('assets/js/sw-toolbox.js'); self.toolbox.options.debug = false; // 缓存本站静态文件 self.toolbox.router.get('/assets/(.*)', self.toolbox.cacheFirst, { cache: { name: staticAssetsCacheName, maxEntries: maxEntries } }); // 缓存 googleapis self.toolbox.router.get('/css', self.toolbox.fastest, { origin: /fonts\.googleapis\.com/, cache: { name: dynamicVendorCacheName, maxEntries: maxEntries } }); // 不缓存 DISQUS 评论 self.toolbox.router.get('/(.*)', self.toolbox.networkOnly, { origin: /disqus\.com/ }); self.toolbox.router.get('/(.*)', self.toolbox.networkOnly, { origin: /disquscdn\.com/ }); // 缓存所有 Google 字体 self.toolbox.router.get('/(.*)', self.toolbox.cacheFirst, { origin: /(fonts\.gstatic\.com|www\.google-analytics\.com)/, cache: { name: staticVendorCacheName, maxEntries: maxEntries } }); self.toolbox.router.get('/content/(.*)', self.toolbox.fastest, { cache: { name: contentCacheName, maxEntries: maxEntries } }); self.toolbox.router.get('/*', function (request, values, options) { if (!request.url.match(/(\/ghost\/|\/page\/)/) && request.headers.get('accept').includes('text/html')) { return self.toolbox.fastest(request, values, options); } else { return self.toolbox.networkOnly(request, values, options); } }, { cache: { name: contentCacheName, maxEntries: maxEntries } }); // immediately activate this serviceworker self.addEventListener('install', function (event) { return event.waitUntil(self.skipWaiting()); }); self.addEventListener('activate', function (event) { return event.waitUntil(self.clients.claim()); }); })();启用 Service Workers
在主题下的 default.hbs 文件 标签前加入下面代码
| 1234567891011121314151617 | <script> var serviceWorkerUri = '/serviceworker.js'; if ('serviceWorker' in navigator) { navigator.serviceWorker.register(serviceWorkerUri).then(function() { if (navigator.serviceWorker.controller) { console.log('Assets cached by the controlling service worker.'); } else { console.log('Please reload this page to allow the service worker to handle network operations.'); } }).catch(function(error) { console.log('ERROR: ' + error); }); } else { console.log('Service workers are not supported in the current browser.'); } </script> |
然后强制刷新一下吧!再次加载后按F12查看一下,加载的内容大小会减小很多。
[建站教程]历史优惠活动内容
猜你可能想看的VPS
- vServer.site→€12 年 1GB 内存 5GB SSD 空间虚拟空间(主机)
- Krypt 第二款新套餐→Intel Xeon E-2286G 4.0 全球[VPS测评]
- CloudIPLC→70 元 月 20M 宽带 100G 流量 沪日 I全球[VPS测评]
- 价格一般-KoDDoS→$12.95 月 香港服务器 含 DDOS 防御香港VPS[主机]
- 疯狂猜成语 图猜成语一个虚线的禾字旁一个公字是什么成语?全球[VPS测评]
- 一键更换 Linux 软件源脚本→SuperUpdate.sh全球[VPS测评]
- MoeCloud→600 元 年 512MB 内存 5GB SSD 空间虚拟空间(主机)
- dedicated 月底促销 基于 LA 的 E3-1270 服务器都具全球[VPS测评]
- 黑色星期五 hostsolutions→9 欧 年 1G 内存 10T 全球[VPS测评]
- JQuery 根据指定值动态设置 select 下的 option 选中全球[VPS测评]
- 练手专用 $7 年 2G 内存 30G SSD 2T 流量 100Mbp全球[VPS测评]
- 几款常用的WordPress性能优化缓存插件全球[VPS测评]
- 快云科技 香港云服务器 纯SSD硬盘/三网回程CN2 GIA/20Mbp香港VPS[主机]
- 美国便宜 VPS 春节促销汇总,最低$7.5/年。美国VPS[主机]
- 长三角地区的算力需求规模到底有多大?全球[VPS测评]
- 世界杯季疫情突发,跨境电商订单排队,义乌商人面临难题全球[VPS测评]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
- 邢志宏:加快推动高质量建设跨境电商综试区起势成势全球[VPS测评]
- 恒创主机优惠码85折_服务器优惠券8折_恒创科技优惠_代金券全球[VPS测评]
- 56云服务器怎么样?56云服务器价格和优势是什么?全球[VPS测评]
- 六一云:国内BGP高防vps套餐,1核/1G/2Mbps/香港CN2,月香港VPS[主机]
- 阳帝云:香港大宽带vps仅15元/月;美国vps200G防御仅28元/月美国VPS[主机]
- Ubuntu关机命令是什么?Ubuntu关机命令介绍全球[VPS测评]
- 啤啤云怎么样?香港CN2线路vps,有香港安畅机房,全部9折,38.7元香港VPS[主机]
- 2020畅行云双11活动:海内外云服务器超低1.5折,2核2G5M云主机全球[VPS测评]
- 咖啡主机怎么样?美国洛杉矶高防vps,cn2直连,香港沙田vps美国VPS[主机]
- 华纳云年终钜惠活动:云服务器半年低至280元,香港高防服务器999元香港VPS[主机]
- 快速云:怎么连接vps?VPS的用途有哪些?全球[VPS测评]
- 再“掷”53亿元 宜家能否挽回中国消费者全球[VPS测评]
- 兼容移动和PC的loading加载和toast消息插件全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-31163.html











