ZZZ Digital Lab

Back to HushBrief

vite.config.ts

import { defineConfig } from 'vite';
export default defineConfig({
  // Rehearsals never load private environment files. Only the owner's explicit
  // live commands opt in to .env.local; no credentials are needed for checks.
  envDir: process.env.HUSHBRIEF_LIVE === '1' ? '.' : false,
  server: { proxy: { '/api/fake-cloud': 'http://127.0.0.1:8788' } },
  preview: { proxy: { '/api/fake-cloud': 'http://127.0.0.1:8788' } },
  build: { target: 'es2022' },
  plugins: [{
    name: 'offline-app-shell',
    generateBundle(_options, bundle) {
      const assets = ['/', '/index.html', ...Object.keys(bundle).filter(name => /\.(?:js|css)$/.test(name)).map(name => '/' + name)];
      const version = Object.keys(bundle).filter(name => name.startsWith('assets/')).join('|');
      this.emitFile({ type: 'asset', fileName: 'sw.js', source: `
const CACHE='hushbrief-'+${JSON.stringify(version)};
const ASSETS=${JSON.stringify(assets)};
self.addEventListener('install',event=>{event.waitUntil(caches.open(CACHE).then(cache=>cache.addAll(ASSETS)).then(()=>self.skipWaiting()))});
self.addEventListener('activate',event=>{event.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k.startsWith('hushbrief-')&&k!==CACHE).map(k=>caches.delete(k)))).then(()=>self.clients.claim()))});
self.addEventListener('fetch',event=>{
 const url=new URL(event.request.url);
 if(event.request.method!=='GET'||url.origin!==self.location.origin||!ASSETS.includes(url.pathname))return;
 event.respondWith(caches.open(CACHE).then(async cache=>(await cache.match(event.request,{ignoreVary:true}))||fetch(event.request)));
});
` });
    }
  }]
});