Skip to content
jshacks.dev
Tooling

Add PWA support to Vite

Add installable PWA support to a Vite app with vite-plugin-pwa, which writes the web app manifest and a service worker that precaches built files.

1 min read
Summarize in AI

vite-plugin-pwa generates the manifest and a Workbox service worker from your build output.

Shell
npm install -D vite-plugin-pwa
vite.config.jsJavaScript
import { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';

export default defineConfig({
  plugins: [
    VitePWA({
      registerType: 'autoUpdate',
      manifest: {
        name: 'Example App',
        short_name: 'Example',
        theme_color: '#0f172a',
        icons: [
          { src: 'icon-192.png', sizes: '192x192', type: 'image/png' },
          { src: 'icon-512.png', sizes: '512x512', type: 'image/png' },
        ],
      },
    }),
  ],
});
Text
PWA v1.0.0
mode      generateSW
precache  8 entries (142.31 KiB)
files generated
  dist/sw.js
  dist/workbox-5ffe50d4.js
  dist/manifest.webmanifest

The plugin lists every built file in the service worker's precache, so the app opens offline after the first visit. autoUpdate swaps in a new worker when you deploy. Browsers install a PWA only over HTTPS or on localhost. See the vite-plugin-pwa guide.

Precaching a large app costs the visitor a big first download. Exclude heavy assets with workbox.globIgnores. Version numbers and sizes in the output are an example.

Also inBrowser
Taggedvitepwa
Share

More snippets

Tooling

Create a Vite project from a template

Create a Vite project from a template with npm create vite, choose the vanilla-ts or react-ts template and see which files it generates.

1 min read
APIs

Fetch JSON and handle HTTP errors

Fetch JSON in the browser or Node.js 24 and throw on 404 or 500 yourself, because fetch only rejects on network failure and does not check the status.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.