To enable offline support, you must configure the service worker's precache manifest. This manifest tells the service worker which application resources to download and store in cache storage for network request interception when the application is offline.
By default, vite-plugin-pwa (via workbox-build) only includes css, js, and html resources in the precache manifest. The plugin traverses your build output folder (typically dist) to find these files.
If you need to include additional resource types (such as images or fonts), you must add them to the globPatterns array within your configuration. The location of this setting depends on your chosen strategy:
- For the default
generateSW strategy, add globPatterns under the workbox key. - For the
injectManifest strategy, add globPatterns under the injectManifest key.
import { VitePWA } from 'vite-plugin-pwa'
export default defineConfig({
plugins: [
VitePWA({
registerType: 'autoUpdate',
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg}']
}
})
]
})