Files
DorianandClaude Opus 4.6 74939e995d perf: cache Kokoro TTS model in service worker
Add CacheFirst runtimeCaching rules for Hugging Face model requests
(huggingface.co and cdn-lfs) with 90-day expiration. Add
getKokoroProgress() export to tts.ts for tracking download progress.
Add TTS model download progress bar overlay in FightViewer.vue that
shows during model loading and auto-hides when complete.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 20:49:00 +00:00

104 lines
3.8 KiB
TypeScript

// Bundle audit (March 2026):
// 1. kokoro-js worker chunk: 2,208 KB — lazy, loaded only inside TTS Web Worker
// 2. FightViewer chunk: 487 KB — lazy, includes kaplay + FightScene + sounds + sprites
// 3. Shared vendor chunk: 281 KB — Vue internals, nostr-tools shared code
// 4. Main entry chunk: 119 KB — Vue runtime + router
// 5. FightPage chunk: 39 KB — lazy, fight page UI
// kokoro-js is NOT in main bundle. kaplay is tree-shaken into FightViewer only.
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
import { VitePWA } from 'vite-plugin-pwa'
export default defineConfig({
plugins: [
vue(),
tailwindcss(),
VitePWA({
registerType: 'autoUpdate',
includeAssets: ['icon.svg'],
manifest: {
name: 'BOTFIGHTS',
short_name: 'BOTFIGHTS',
description: 'A safe place to hash it out',
theme_color: '#0a0a0a',
background_color: '#0a0a0a',
display: 'standalone',
orientation: 'any',
start_url: '/',
icons: [
{ src: '/icon.svg', sizes: 'any', type: 'image/svg+xml', purpose: 'any' },
{ src: '/icon-192.png', sizes: '192x192', type: 'image/png' },
{ src: '/icon-512.png', sizes: '512x512', type: 'image/png' },
{ src: '/icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' },
],
},
workbox: {
globPatterns: ['**/*.{js,css,html,svg,png,woff,woff2}'],
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024, // 5MB — kokoro TTS chunk is ~2.2MB
cleanupOutdatedCaches: true,
skipWaiting: true,
clientsClaim: true,
navigateFallback: '/index.html',
navigateFallbackDenylist: [/^\/api\//],
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i,
handler: 'CacheFirst',
options: { cacheName: 'google-fonts-css', expiration: { maxEntries: 10, maxAgeSeconds: 60 * 60 * 24 * 365 } },
},
{
urlPattern: /^https:\/\/fonts\.gstatic\.com\/.*/i,
handler: 'CacheFirst',
options: { cacheName: 'google-fonts-webfont', expiration: { maxEntries: 30, maxAgeSeconds: 60 * 60 * 24 * 365 } },
},
{
urlPattern: /\/api\/.*/i,
handler: 'NetworkOnly',
method: 'GET',
options: { cacheName: 'api-cache' },
},
{
urlPattern: /^https:\/\/huggingface\.co\/.*Kokoro.*\.onnx/i,
handler: 'CacheFirst',
options: { cacheName: 'kokoro-model', expiration: { maxEntries: 10, maxAgeSeconds: 60 * 60 * 24 * 90 } },
},
{
urlPattern: /^https:\/\/cdn-lfs.*\.huggingface\.co\/.*/i,
handler: 'CacheFirst',
options: { cacheName: 'kokoro-model-lfs', expiration: { maxEntries: 20, maxAgeSeconds: 60 * 60 * 24 * 90 } },
},
],
},
}),
],
worker: {
format: 'es', // Required for dynamic import('kokoro-js') inside the TTS worker
},
optimizeDeps: {
// Pre-bundle kokoro-js on first startup so Vite doesn't stall mid-page-load
// discovering it as a new dep. The 4.8MB bundle is cached in node_modules/.vite/deps.
include: ['kokoro-js'],
},
server: {
port: 9101,
headers: {
// Enable SharedArrayBuffer for onnxruntime WASM threads.
// Without these, Kokoro TTS runs single-threaded on the main thread,
// blocking the UI for seconds on every TTS generation.
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'credentialless',
},
proxy: {
'/api': {
target: 'http://localhost:9100',
changeOrigin: true,
},
'/ws': {
target: 'ws://localhost:9100',
ws: true,
},
},
},
})