文章首次發表在: 博客支持PWA了~
使用hexo插件使博客支持pwa功能,目前我所知道的有兩種插件都可實現該功能css
全站支持HTTPS.(ps: 目前本人使用的是騰訊雲的免費證書)html
網站配置HTTPS, 已經有不少文章寫了配置過程, 本篇文章不在贅述git
npm i --save hexo-pwa
hexo的配置文件採用yml語言, 想要了解該語言的能夠去看看
在根目錄的_config.yml
中添加github
pwa: manifest: path: /manifest.json body: "name": "funnycoderstar" "short_name": "star" "theme_color": "rgba(203,7,83,0.86)" "background_color": "#FAFAFA" "display": "standalone" "Scope": "/" "start_url": "/" icons: - src: https://cdn.wangyaxing.cn/icon-144x144.png?v=1 sizes: 144x144 type: image/png - src: https://cdn.wangyaxing.cn/icon-128x128.png sizes: 128x128 type: image/png - src: https://cdn.wangyaxing.cn/icon-96x96.png sizes: 96x96 type: image/png serviceWorker: path: /sw.js preload: urls: - / posts: 5 opts: networkTimeoutSeconds: 5 routes: - pattern: !!js/regexp /hm.baidu.com/ strategy: networkOnly - pattern: !!js/regexp /.*\.(js|css|jpg|jpeg|png|gif)$/ strategy: cacheFirst - pattern: !!js/regexp /\// strategy: networkFirst priority: 5
參數含義能夠去hexo-pwa文檔中查看npm
manifest.json
和sw.js
這兩個文件放的位置要和配置中的路徑一致, 我是放在跟目錄的
sw.jsjson
importScripts('https://g.alicdn.com/kg/workbox/3.3.0/workbox-sw.js'); if (workbox) { workbox.setConfig({ modulePathPrefix: 'https://g.alicdn.com/kg/workbox/3.3.0/' }); workbox.precaching.precache(['/', '/index.html']); workbox.routing.registerRoute(new RegExp('^https?://wangyaxing.cn/?$'), workbox.strategies.networkFirst()); workbox.routing.registerRoute(new RegExp('.*.html'), workbox.strategies.networkFirst()); workbox.routing.registerRoute(new RegExp('.*.(?:js|css)'), workbox.strategies.staleWhileRevalidate()); workbox.routing.registerRoute(new RegExp('https://cdn.wangyaxing.cn/'), workbox.strategies.cacheFirst()); }
manifest.json瀏覽器
{ "name": "funnycoderstar", "short_name": "star", "theme_color": "rgba(203,7,83,0.86)", "background_color": "#FAFAFA", "display": "standalone", "Scope": "/", "start_url": "/", "icons": [ { "src": "/source/images/icons/icon-96x96.png", "sizes": "96x96", "type": "image/png" }, { "src": "/source/images/icons/icon-128x128.png", "sizes": "128x128", "type": "image/png" }, { "src": "/source/images/icons/icon-144x144.png", "sizes": "144x144", "type": "image/png" }, ], "splash_pages": null }
manifest生成地址: https://app-manifest.firebase...hexo
funnycoderstar
安裝到主屏(PWA)
在桌面上便可看到博客的小圖標app
感興趣的小夥伴能夠來個人博客體驗一下, 哈哈~~ https://.wangyaxing.cn/
使用基本與hexo-pwa相同, 下面簡述一下過程svg
npm i --save hexo-offline
# Offline ## Config passed to sw-precache ## https://github.com/JLHwung/hexo-offline offline: maximumFileSizeToCacheInBytes: 10485760 staticFileGlobs: - public/**/*.{js,html,css,png,jpg,jpeg,gif,svg,json,xml} stripPrefix: public verbose: true runtimeCaching: - urlPattern: /* handler: cacheFirst options: origin: cdn.example.com - urlPattern: /* handler: cacheFirst options: origin: cdn.another-example.org
manifest.json
manifest.json放到 source目錄下
manifest.json
next主題在 layout/_partials/head.swig
添加
<link rel="manifest" href="/manifest.json">
serviceWorker
是否已加載