hexo博客簡單支持PWA

文章首次發表在: 博客支持PWA了~

前言

使用hexo插件使博客支持pwa功能,目前我所知道的有兩種插件都可實現該功能css

前提(HTTPS)

全站支持HTTPS.(ps: 目前本人使用的是騰訊雲的免費證書)html

網站配置HTTPS, 已經有不少文章寫了配置過程, 本篇文章不在贅述git

使用hexo-pwa

1.安裝hexo-pwa

npm i --save hexo-pwa

2.修改配置文件

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

3.添加manifest.jsonsw.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

4.將funnycoderstar安裝到主屏(PWA)

  1. 地址欄輸入: Chrome://flags
  2. 搜索並啓用如下項目: Desktop PWAs(桌面PWAs)、App Banners(應用橫幅)、Experimental App Banners(實驗性應用橫幅)
  3. 重啓瀏覽器使修改的設置生效
  4. 點擊地址欄最右邊按鈕
  5. 選擇"安裝 funnycoderstar"

image

在桌面上便可看到博客的小圖標app

image

感興趣的小夥伴能夠來個人博客體驗一下, 哈哈~~ https://.wangyaxing.cn/

使用hexo-offline

使用基本與hexo-pwa相同, 下面簡述一下過程svg

1. 安裝

npm i --save hexo-offline

2. 修改配置文件

# 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

3. 添加manifest.json

manifest.json放到 source目錄下

4. 引入manifest.json

next主題在 layout/_partials/head.swig 添加

<link rel="manifest" href="/manifest.json">

注意

  • 文件放置位置必定要與配置文件中寫的路徑一致
  • 圖標大小必定按照PWA的嚴格要求
  • 打開 Chrome Dev Tools的Application一欄中進行調試

image

  • 還能夠經過觀察network中的請求是否有小齒輪標誌來檢測serviceWorker是否已加載

image

參考

相關文章
相關標籤/搜索