Reproducing the vite-plugin-pwa 2 MiB Precache Failure: Splitting Doesn't Fix It, Excluding Does

A 60-line script that reproduces vite-plugin-pwa's 'won't be precached' build failure with pinned versions, then shows what two scoped fixes actually put in the service worker. The surprise: moving the big module into its own chunk changes nothing, because the precache counts every chunk.

December 06, 2026
Bob
7 min read

Last week a translation PR to ActivityWatch’s web UI turned CI red because one JavaScript chunk grew past 2 MiB. I wrote up that incident with logs and the three fixes the project considered. This post is the part that write-up lacked: a script you can run in an empty directory to see the failure, and to see what each fix changes in the generated service worker.

Everything below was run on 2026-12-06 in a fresh temporary directory (Node 22.23.4, npm 10.9.4) with these pinned versions: [email protected], [email protected], [email protected], [email protected].

The reproduction

The script generates a deterministic ~2.2 MB big-locale.js (a fixed-seed pseudo-random message table, no network), a one-line app that loads it with a dynamic import(), and one vite.config.js with three modes selected by MODE: fail (defaults), raise (a scoped cap increase), and lazy (exclude that one chunk from the precache and cache it at runtime). Then it builds each mode and prints the build status and the URLs in the generated service worker.

#!/usr/bin/env bash
# Reproduces the vite-plugin-pwa precache size limit with pinned versions.
set -euo pipefail
mkdir -p repro && cd repro

cat > package.json <<'JSON'
{
  "name": "vite-pwa-precache-limit-repro",
  "private": true,
  "type": "module",
  "devDependencies": {
    "vite": "8.4.1",
    "vite-plugin-pwa": "2.0.0",
    "workbox-build": "7.4.1",
    "workbox-window": "7.4.1"
  }
}
JSON

cat > index.html <<'HTML'
<!doctype html>
<html><body><div id="app"></div><script type="module" src="/main.js"></script></body></html>
HTML

# The app loads its big locale data with a dynamic import, so Vite emits it as its own chunk.
cat > main.js <<'JS'
import('./big-locale.js').then(({ messages }) => {
  document.getElementById('app').textContent = Object.keys(messages).length + ' strings'
})
JS

# ~2.3 MB of messages, deterministic (fixed-seed LCG, no network).
node -e '
let s = 1; const rnd = () => (s = (s * 1664525 + 1013904223) >>> 0) / 2 ** 32;
const word = () => Array.from({ length: 4 + Math.floor(rnd() * 8) }, () => String.fromCharCode(97 + Math.floor(rnd() * 26))).join("");
const words = Array.from({ length: 5000 }, word);
const messages = {};
for (let i = 0; i < 20000; i++) messages["k" + i] = Array.from({ length: 12 }, () => words[Math.floor(rnd() * 5000)]).join(" ");
require("fs").writeFileSync("big-locale.js", "export const messages = " + JSON.stringify(messages) + "\n");
'

cat > vite.config.js <<'JS'
import { defineConfig } from 'vite'
import { VitePWA } from 'vite-plugin-pwa'

// MODE selects the variant: fail | raise | lazy
const workbox = {
  fail: {},
  // Raise the cap just above the measured entry (3 MiB), not to Infinity.
  raise: { maximumFileSizeToCacheInBytes: 3 * 1024 * 1024 },
  // Keep the default cap, drop that one chunk from the precache by name,
  // and cache it the first time it is requested.
  lazy: {
    globIgnores: ['**/big-locale-*.js'],
    runtimeCaching: [{
      urlPattern: /\/assets\/big-locale-.*\.js$/,
      handler: 'CacheFirst',
      options: { cacheName: 'locales' },
    }],
  },
}[process.env.MODE ?? 'fail']

export default defineConfig({
  plugins: [VitePWA({ registerType: 'autoUpdate', workbox })],
})
JS

npm install --no-audit --no-fund --loglevel=error

for MODE in fail raise lazy; do
  echo "=== MODE=$MODE ==="
  rm -rf dist
  status=0
  MODE=$MODE npx vite build > "build-$MODE.log" 2>&1 || status=$?
  grep -E 'is [0-9.]+ MB|precache +[0-9]' "build-$MODE.log" || true
  echo "build exit: $status"
  if [ -f dist/sw.js ]; then grep -o 'url:"[^"]*"' dist/sw.js | sed 's/^/  precached: /'; fi
done

Run it with bash repro.sh in an empty directory. It exited 0 and printed this (the npm install line trimmed to its summary):

=== MODE=fail ===
  - assets/big-locale-PI6QnzVu.js is 2.19 MB, and won't be precached.
build exit: 1
  precached: url:"registerSW.js"
  precached: url:"index.html"
  precached: url:"assets/index-B2woEJMK.js"
  precached: url:"manifest.webmanifest"
=== MODE=raise ===
precache  5 entries (2140.95 KiB)
build exit: 0
  precached: url:"registerSW.js"
  precached: url:"index.html"
  precached: url:"assets/index-B2woEJMK.js"
  precached: url:"assets/big-locale-PI6QnzVu.js"
  precached: url:"manifest.webmanifest"
=== MODE=lazy ===
precache  4 entries (2.91 KiB)
build exit: 0
  precached: url:"registerSW.js"
  precached: url:"index.html"
  precached: url:"assets/index-B2woEJMK.js"
  precached: url:"manifest.webmanifest"

The full failing message from build-fail.log is the one the aw-webui CI showed: Configure "workbox.maximumFileSizeToCacheInBytes" to change the limit: the default value is 2 MiB. The limit is 2 MiB, 2,097,152 bytes. The plugin reports sizes in decimal MB, so 2.19 MB is just over it.

What this shows

1. Code splitting alone does not fix it. In the fail run the oversized module is already its own chunk, because the app loads it with import(). The build still fails. Workbox’s precache list is every emitted asset its glob matches (here the .js chunks, index.html and the manifest, as the sw.js listings show), not just the entry chunk. A split chunk that is still over the cap is still over the cap. A split helps only if it brings the pieces under the limit (the vendor split that fixed aw-webui did) or if you also keep the chunk out of the precache.

2. The build exits 1 but still writes a service worker. After the fail build, dist/sw.js exists and lists four entries without the big chunk. The error is raised after generation. A deploy script that ignores the exit code would ship a service worker that quietly omits the file. Check the exit status.

3. The two scoped fixes make different promises.

  raise (cap 3 MiB) lazy (globIgnores + runtimeCaching)
Precache size 2140.95 KiB, 5 entries 2.91 KiB, 4 entries
First visit downloads everything, including the 2.19 MB chunk, at service-worker install only the shell; the chunk loads when the app first asks for it
Offline after install the whole app works the shell works; the big chunk works only if it was fetched once before going offline
Cost of the next growth build fails again at 3 MiB none for that chunk

raise keeps the “install once, works offline everywhere” property and pays for it with a larger install on every device, including users who never touch the data. lazy keeps the install small and gives up offline-first for that one module: a user who goes offline before the chunk has been requested once gets a failed import. The CacheFirst entry is safe here only because Vite hashes the filename, so an updated chunk has a new URL.

I verified the build outcomes and the service-worker contents above. I did not run either variant in a browser with the network disabled, so the offline behaviour in the table follows from the generated precache lists and Workbox’s documented strategies, not from a recorded offline session. Check it in your own app before relying on it.

Which one to pick

  • If the chunk is mostly data that every user needs offline (a dictionary, a map), raise the cap to a number just above the measured size, not Infinity, and accept the larger install.
  • If it is data most users never open (other languages, admin screens), use the lazy pattern: globIgnores by chunk name plus a runtimeCaching rule. Use a regular expression for the chunk name, not a hard-coded hash.
  • If the entry chunk is big because of vendor libraries, split those first (manualChunks, or the equivalent code-splitting option in your bundler version). That brought aw-webui’s entry chunk from 2,100 kB to 1,004 kB with the default cap untouched.

In aw-webui the vendor split and a per-locale dynamic import both landed; the cap was left at its default. The earlier write-up has the PR links and measurements.