npx quasar mode add pwa
.
└── src-pwa/
├── register-service-worker-dev.js # 应用代码**管理**Service Worker
└── custom-service-worker.js # 可选的自定义的service worker文件
// quasar.config.js
sourceFiles: {
pwaRegisterServiceWorker: 'src-pwa/register-service-worker',
pwaServiceWorker: 'src-pwa/custom-service-worker',
pwaManifestFile: 'src-pwa/manifest.json',
}
$ quasar dev -m pwa
# ..或者更长的形式:
$ quasar dev --mode pwa
# 使用一个特定的Quasar模式:
$ quasar dev -m pwa -t ios
$ quasar build -m pwa
# ..或者更长的形式:
$ quasar build --mode pwa
# 使用一个特定的Quasar模式:
$ quasar build -m pwa -t ios
这里放一个如何测试PWA,以及确定它是否成功的一篇文章PWA-H5 Web App优化探索之路(Service Worker,Lighthouse)
测试我们的PWA配置是否成功
页面打开以后,我没有点击切换页面,我发现此时就是一次性缓存了所有的静态资源
import { register } from 'register-service-worker'
// The ready(), registered(), cached(), updatefound() and updated()
// events passes a ServiceWorkerRegistration instance in their arguments.
// ServiceWorkerRegistration: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
register(process.env.SERVICE_WORKER_FILE, {
// The registrationOptions object will be passed as the second argument
// to ServiceWorkerContainer.register()
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#Parameter
// registrationOptions: { scope: './' },
ready (/* registration */) {
// console.log('Service worker is active.')
},
registered (/* registration */) {
// console.log('Service worker has been registered.')
},
cached (/* registration */) {
// console.log('Content has been cached for offline use.')
},
updatefound (/* registration */) {
// console.log('New content is downloading.')
},
updated (/* registration */) {
// console.log('New content is available; please refresh.')
},
offline () {
// console.log('No internet connection found. App is running in offline mode.')
},
error (/* err */) {
// console.error('Error during service worker registration:', err)
}
})
欢迎大家指出文章需要改正之处~
学无止境,合作共赢