The following files have been updated / added:
public/img/icons/android-chrome-192x192.png
public/img/icons/android-chrome-512x512.png
public/img/icons/apple-touch-icon-120x120.png
...
public/img/icons/favicon-32x32.png
public/img/icons/msapplication-icon-144x144.png
public/img/icons/mstile-150x150.png
public/img/icons/safari-pinned-tab.svg
public/robots.txt
src/registerServiceWorker.js
package-lock.json
package.json
src/main.js
git status输出:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: package-lock.json
modified: package.json
modified: src/main.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
public/img/
public/robots.txt
src/registerServiceWorker.js
no changes added to commit (use "git add" and/or "git commit -a")
Service worker has been registered.
registerServiceWorker.js:20 New content is downloading.
logger.mjs:44 workbox Precaching 7 files.
logger.mjs:44 View newly precached URLs.
registerServiceWorker.js:17 Content has been cached for offline use.
registerServiceWorker.js:8 App is being served from cache by a service worker.
For more details, visit https://goo.gl/AFskqB
再次打开,会输出成功加载
workbox Precaching is responding to: /css/app.b706d8fd.css
logger.mjs:44 workbox Precaching is responding to: /css/chunk-vendors.bb30aab5.css
logger.mjs:44 workbox Precaching is responding to: /js/app.23a81c05.js
logger.mjs:44 workbox Precaching is responding to: /js/chunk-vendors.57affefc.js
app.23a81c05.js?__WB_REVISION__=1542843adc3336277dad:1 App is being served from cache by a service worker.
For more details, visit https://goo.gl/AFskqB
logger.mjs:44 workbox Precaching is responding to: /manifest.json
app.23a81c05.js?__WB_REVISION__=1542843adc3336277dad:1 Service worker has been registered.
View Post on Blog
什么是 PWA
其全称为 Progressive Web Apps,可赋予网页原生 App 的各种优点。本人水平有限,不再赘述。对我而言最重要的是本地存储 + 离线可看,还有消息推送。
为什么 PWA
当然是不想学 Android 和 iOS 啦!学会前端一下搞定桌面、Android、iOS 三端,岂不美哉!
如何开始
这里就不说怎么安装 vue-cli 了。这里以 vue-cli-4 为例。
如果是已有的项目,如下,记得先 commit,Vue 会改动代码
直接新建 PWA 项目(该方法已过时):
看看加了什么
运行
vue add pwa
后,会输出修改的文件。如果直接新建项目并且 Git 配置正确,Vue 会自动初始化提交。这时再添加 PWA,可使用git status
查看。输出略有不同输出:
git status
输出:更新了
package-lock.json
,package.json
没说的加了很多图片是为了适应不同端显示。为什么很矫情地用不同的名字呢?因为不同浏览器很矫情地用不同的图标。。
robots.txt
,为了通过 LightHouse 的检测 vuejs/vue-cli#1715registerServiceWorker.js
,注册 Service Worker,并监听生命周期的事件。注意它不是 Service Worker 哦,只是注册者而已。main.js
,导入了./registerServiceWorker
配置 PWA
在项目根目录下新建
vue.config.js
进行配置配置以文档为准
最简单的情况下,可考虑如下配置:
名字和颜色涉及添加至桌面的应用名,及桌面进入的启动页面的长相。
workboxPluginMode: 'GenerateSW'
就是自动生成 Service Worker,也是默认操作。具体要求就如workboxOptions
。这里根据默认,一股脑 precache 了所有东西,可以达到离线可看的目的。如果部署环境不在网站根目录,还需加上:
比下面的更优(马上看到为什么):
本地测试
可以使用
browser-sync
作为本地服务器。如果成功配置,Chrome 导航栏会出现$\oplus$字样,console 会输出成功缓存。注意仅在 localhost 会有哦
再次打开,会输出成功加载
然后呢
恭喜你有了 PWA 的基本配置,像正常一样开发 Vue 应用,初级阶段也没什么问题。