Package Exports
- vite-plugin-pwa
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vite-plugin-pwa) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Features
- Generate Service Worker with Offline support (via Workbox)
- Auto inject Web App Manifest
- Prompt for new content refreshing
- Automatic reload when new content available
- Advanced (injectManifest)
- Static assets handling
- WIP: Icons generation for different dimensions
Usage
npm i vite-plugin-pwa -D # yarn add vite-plugin-pwa -D
Add it to vite.config.js
// vite.config.js
import { VitePWA } from 'vite-plugin-pwa'
export default {
plugins: [
VitePWA()
]
}
Configuration
Simple (generateSW)
VitePWA({
manifest: {
// content of manifest
},
workbox: {
// workbox options for generateSW
}
})
Prompt for new content
// main.ts
import { registerSW } from 'virtual:pwa-register'
const updateSW = registerSW({
onNeedRefresh() {
// show a prompt to user
},
onOfflineReady() {
// show a ready to work offline to user
},
})
// when user clicked the "refresh" button
updateSW()
// the page will reload and the up-to-date content will be served.
You can find an example written in Vue 3: ReloadPrompt.vue.
Automatic reload when new content available
With this option, once the service worker detects new content available, then it will update caches and will reload all browser windows/tabs with the application opened automatically to take the control.
The disadvantage of using this option is that the user can lose data in other browser windows / tabs in which the application is open and is filling in a form.
If your application has forms, it is recommended that you change the behavior to use default prompt
option to allow
the user decide when to update the content of the application.
Configuration
With this option, the plugin will force workbox.clientsClaim
and workbox.skipWaiting
to true
.
VitePWA({
registerType: 'autoUpdate',
manifest: {
// content of manifest
},
workbox: {
// workbox options for generateSW
}
})
Runtime
// main.ts
import { registerSW } from 'virtual:pwa-register'
const updateSW = registerSW({
onOfflineReady() {
// show a ready to work offline to user
},
})
Advanced (injectManifest)
You will need to include workbox-*
dependencies as dev dependencies
.
// sw.js
import { precacheAndRoute } from 'workbox-precaching'
// self.__WB_MANIFEST is default injection point
precacheAndRoute(self.__WB_MANIFEST)
// vite.config.js
VitePWA({
strategies: 'injectManifest',
manifest: {
// content of manifest
}
})
You can use Typescript to build your service worker, you can find an example written for a Vue 3 project:
sw.ts.
To resolve service worker types, just add WebWorker
to lib entry on your tsconfig.json
file, for example:
"lib": ["ESNext", "DOM", "WebWorker"],
Static assets handling
By default, all icons on PWA Manifest
option found under Vite's publicDir
option directory, will be included
in the service worker precache. You can disable this option using includeManifestIcons: false
.
You can also add another static assets such as favicon
, svg
and font
files using includeAssets
option.
The includeAssets
option will be resolved using fast-glob
found under Vite's publicDir
option directory, and so
you can use regular expressions to include those assets, for example: includeAssets: ['fonts/*.ttf', 'images/*.png']
.
You don't need to configure PWA Manifest icons
on includeAssets
option.
You can find an example written for a Vue 3 here.
If you need to include other assets that are not under Vite's publicDir
option directory, you can use the
globPatterns
parameter of workbox
or injectManifest
plugin options:
VitePWA({
workbox: {
globPatterns: [],
// ...
},
// or for injectManifest strategy
injectManifest: {
globPatterns: [],
// ...
}
})
Full config
Check out the type declaration src/types.ts and the following links for more details.
IDE errors 'Cannot find module' (ts2307)
If your TypeScript build step or IDE complain about not being able to find modules or type definitions on imports, add the following to the compilerOptions.types
array of your tsconfig.json
:
// tsconfig.json
{
"compilerOptions": {
"types": [
"vite-plugin-pwa/client"
]
}
}
Testing service worker
Since this plugin will not generate the service worker on development
, you can test it on local following these steps:
- add
serve
script to yourpackage.json
if not before:
"serve": "vite preview"
- build your app and run
serve
:npm run build && npm run serve
.
Sponsors
This project is part of my Sponsor Program
License
MIT License © 2020 Anthony Fu