使用谷歌黑科技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
- 疯狂猜成语 图猜成语一个老人在棺材里坐着外面一个人吓的跳起来是什么成语?全球[VPS测评]
- VirMach→$37 月-E3 1240 16GB 1TB 10TB 全球[VPS测评]
- HostKvm 全场 8 折 美国高防 7 折美国VPS[主机]
- $7.5 月 1G 内存 20G 硬盘 700G 流量 100Mbps 香港VPS[主机]
- 便宜 酷番云→2G 内存 180G 硬盘 5M 带宽仅需 50 元 月 全球[VPS测评]
- ONEVPS→$4 月 KVM-512MB 20G SSD 1G 无限流日本VPS[主机]
- CI 框架提示 Undefined property: XXX::$db全球[VPS测评]
- PHP 防止 XSS 注入解决方案大全全球[VPS测评]
- HKServerSolution→999 元 月 E3-1230v3 1全球[VPS测评]
- 90VM→100 元 月 1GB 内存 20GB SSD 空间 1TB 虚拟空间(主机)
- 一个粗的环字一个窄的燕字是什么成语?全球[VPS测评]
- THINKPHP5 在 php5.4 版本以上伪静态重写报错→No in全球[VPS测评]
- YYYHost洛杉矶高防CN2GIA1G内存8m带宽外加100g防御 月全球[VPS测评]
- 宝塔面板免费版如何开启 waf 防火墙全球[VPS测评]
- RFCHOST香港CMI线路VPS九折优惠,$8.99起/月,500Mb香港VPS[主机]
- 推荐! Virmach → 1C 512M RAM 15G SSD 25全球[VPS测评]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
- ERP库存管理系统HTML5模板 - DreamsPOS全球[VPS测评]
- 私有云架设之优选拓普龙S465-24高密度存储服务器机箱评测解析全球[VPS测评]
- 亿速云11.11上云狂欢节活动:2核4G国内/香港云服务器,价格999元香港VPS[主机]
- 好云:国内云服务器2核2G5M优惠,仅350元/年;香港/海外云服务器,香港VPS[主机]
- 2022年黄河流域跨境电商博览会将于8月26日至28日在青岛西海岸新区举全球[VPS测评]
- 95idc怎么样?香港云主机1核2G2M仅50元/月;香港物理服务器8核香港VPS[主机]
- 狸猫云:香港高速CN2VPS,200元/年-香港高速GIA云服务器促销活香港VPS[主机]
- 美国信贷市场的痛苦将不亚于上世纪70年代的通胀时期美国VPS[主机]
- 港网科技怎么样?国内BGP云主机,2核2G5M带宽仅377.46元/年全球[VPS测评]
- 企鹅小屋:1核/512MB/10GB SSD空间/500GB流量/KVM虚拟空间(主机)
- 硅云:香港免备案云服务器仅232元/年;香港2核2G2M仅482元/年香港VPS[主机]
- 空空云怎么样?香港CN2-GIA VPS,带宽2M,月付15元香港VPS[主机]
- 六六云:1核/1GB/20GB硬盘/500GB流量/30Mbps/KVM香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-21430.html











