使用谷歌黑科技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
- 桔子数据香港沙田 GIA 上线,三网回程 CN2 GIA,30M 大带宽香港VPS[主机]
- CoalCloud→268 元 月 2 核 2GB 内存 20GB SS虚拟空间(主机)
- 八月优惠 LOCVPS→香港大埔 cn2 VPS 补货 VPS 全场 8香港VPS[主机]
- Online→€1.99 月 C2350 4GB 内存 1TB 硬盘 不全球[VPS测评]
- 腾讯云 - 双十一促销 11.11 智惠上云促销方案整理汇总全球[VPS测评]
- 如何关闭(注销)QQ 空间?虚拟空间(主机)
- notepad++设置中文 notepad++如何设置简体中文界面全球[VPS测评]
- 优惠 ¥56 月 2CPU 2G 内存 30G 6Mbps@不限流量 日日本VPS[主机]
- friendhosting→11 周年庆 5 折优惠 多送一倍时间 VP全球[VPS测评]
- 正确看待域名是否需要添加 www 前缀,对SEO有何影响?全球[VPS测评]
- 可能违规的5个Google Adsense广告规则全球[VPS测评]
- HydroVPS 加拿大绿色环保VPS 1G内存/15GNvme/500全球[VPS测评]
- RackNerd洛杉矶DC02机房补货,年付12美元起,1Gbps带宽/美国VPS[主机]
- ServerGlobalServices → 6.5$月付 2C2G30全球[VPS测评]
- HKServerSolution → 2G 内存 20G HDD 2T单全球[VPS测评]
- 快速云:怎么连接vps?VPS的用途有哪些?全球[VPS测评]
- 做跨境电商一个月都有多少收入?全球[VPS测评]
- 极光kvm怎么样,便宜大带宽香港cmi vps/美国gia vps低至1美国VPS[主机]
- Mysql连接数过高,如何检查?全球[VPS测评]
- 麻花云怎么样?安徽移动vps月付29元/香港vps月付21元香港VPS[主机]
- 云服务器能干什么?有几十种玩法?阿里云,腾讯云服务器如何购买?全球[VPS测评]
- 从阿里云七代云服务器,谈云计算四大趋势全球[VPS测评]
- 快云科技:香港CN2VPS,2核/2GB内存/3M独享,仅售14.98元香港VPS[主机]
- 樊云:9折优惠,香港cn2 vps+(强制三网、50G高防);美国cn2美国VPS[主机]
- 拼多多正筹建跨境电商平台:密集挖角SHEIN员工,零佣金招商入驻全球[VPS测评]
- Vultr新用户100美金 最新活动来啦!新用户充值活动来了,充100美全球[VPS测评]
- 景文互联年终活动:云服务器7折,多送2G内存,香港cn2\\日本cn2\日本VPS[主机]
- 桔子数据:香港CN2大带宽云服务器28元/月_1核/1G/10M带宽/C香港VPS[主机]
- 麻花云怎么样?安徽移动VPS月付29元 香港VPS 2M月付21元香港VPS[主机]
- 源云主机,香港沙田VPS CN2 1核1G 24/月 NTT 50M 9香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-31163.html











