使用谷歌黑科技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
- 优惠 腾讯云限时秒杀→1 核 2G 内存云服务器年付 128 元 非常适全球[VPS测评]
- Krypt 新平台 KVM 月付 5 美元起 年付 5 折 洛杉矶 CN全球[VPS测评]
- 疯狂猜成语 图猜成语一个三字摔了两下是什么成语?全球[VPS测评]
- HostYun→17 元 月 KVM-512MB 10GB 500GB 全球[VPS测评]
- 腾讯云服务器秒杀→新用户 2 核 4G 6M 独享 1499 元 3 年全球[VPS测评]
- veesp→$1 月 KVM-512MB 10GB 100GB 圣彼得堡全球[VPS测评]
- 腾讯云秒杀→个人上海云服务器年付 99 元起 企业用户 2C4G5M 三全球[VPS测评]
- LOCVPS12月新上韩国VPS全场八折2G内存40G硬盘15M带宽BG韩国VPS[主机]
- WordPress文章发布时间怎么精确到秒?全球[VPS测评]
- Cloudcone双十一特价便宜VPS,KVM架构,1Gbps带宽,年付全球[VPS测评]
- Seflow → 意大利 3.99欧元 月付 1C1G15G硬盘 500全球[VPS测评]
- HKServerSolution → 4G内存 50G SSD 100M全球[VPS测评]
- 跨境电商平台首选香港服务器香港VPS[主机]
- Bloggar-移动端友好的新闻博客类HTML5模板全球[VPS测评]
- Vultr新用户100美金 最新活动来啦!新用户充值活动来了,充100美全球[VPS测评]
- 宝塔面板怎么放行端口?宝塔面板放行端口的方法全球[VPS测评]
- 奇维云优惠活动:香港CN21核1G1M带宽云服务器免费领取香港VPS[主机]
- 买了一台云服务器到底能干什么?全球[VPS测评]
- 原创解析:云计算含义及定义全球[VPS测评]
- OneTechCloud易科云双十二优惠:香港/美国CN2全场VPS季付美国VPS[主机]
- 源云主机,香港沙田VPS CN2 1核1G 24/月 NTT 50M 9香港VPS[主机]
- 搬瓦工VPS:高端线路助力企业运营,10Gbps美国 cn2 gia,1日本VPS[主机]
- 六六云:美国cera机房CN2 GIA高防/香港CN2 GIA建站/CM美国VPS[主机]
- 孤狼云:香港CN2/美国CN2 VPS首月特价¥6.8元;香港虚拟主机仅美国VPS[主机]
- 拼多多回应筹备跨境电商业务:不予置评全球[VPS测评]
- 恒星云:香港沙田CN2VPS低至9.9元/起_国内BGP云服务器4核4G香港VPS[主机]
- 硅云:香港云服务器2核2G5M促销1999元/3年起,香港虚拟主机1G空虚拟空间(主机)
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企美国VPS[主机]
- 互盟云怎么样?1核2G云服务器仅468元/年,香港云主机58元/月香港VPS[主机]
- 狗云DogYun,香港cn2云服务器/优惠码/流量包,按小时计费云服务器香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-27924.html











