import fs from 'node:fs'; import path from 'node:path'; import { createHash } from 'node:crypto'; // Vite's dependency graph defines the complete mobile allowlist. Desktop // entries, arbitrary files and future chunks are not implicitly exposed. const root = path.resolve('dist-client'), manifest = JSON.parse(fs.readFileSync(path.join(root, '.vite/manifest.json'), 'utf8')); const seen = new Set(), assets = new Set(); function visit(name) { if (seen.has(name)) return; seen.add(name); const entry = manifest[name]; if (!entry) throw new Error('Mobile build dependency is missing: ' + name); for (const file of [entry.file, ...(entry.css ?? []), ...(entry.assets ?? [])]) { if (!/^assets\/[A-Za-z0-9_.-]+\.(js|css)$/.test(file)) throw new Error('Unexpected mobile asset: ' + file); if (!fs.statSync(path.join(root, file)).isFile()) throw new Error('Mobile asset is not a file: ' + file); assets.add('/' + file); } for (const imported of [...(entry.imports ?? []), ...(entry.dynamicImports ?? [])]) visit(imported); } visit('mobile.html'); const list = [...assets].sort(); fs.writeFileSync(path.join(root, 'mobile-assets.json'), JSON.stringify(list, null, 2) + '\n'); // Reuse our existing raster mark for iPhone's home-screen icon; no generated // media or network dependency is needed for this product asset. const icon = fs.readFileSync(path.join(root, 'workbench-icon.png')); fs.writeFileSync(path.join(root, 'mobile-touch-icon.png'), icon); const version = createHash('sha256').update(fs.readFileSync(path.join(root, 'mobile.html'))).update(JSON.stringify(list)).digest('hex').slice(0, 20); const files = ['/', '/mobile.webmanifest', '/mobile-icon.svg', '/mobile-touch-icon.png', ...list]; const worker = `const CACHE = 'workbench-mobile-${version}'; const FILES = ${JSON.stringify(files)}; self.addEventListener('install', event => event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(FILES)).then(() => self.skipWaiting()))); self.addEventListener('activate', event => event.waitUntil(caches.keys().then(names => Promise.all(names.filter(name => name.startsWith('workbench-mobile-') && name !== CACHE).map(name => caches.delete(name)))).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 || url.search || !FILES.includes(url.pathname)) return; event.respondWith(fetch(event.request).then(response => { if (response.ok) { const copy = response.clone(); event.waitUntil(caches.open(CACHE).then(cache => cache.put(event.request, copy))); } return response; }).catch(async () => (await caches.match(event.request)) || Response.error())); }); `; fs.writeFileSync(path.join(root, 'mobile-sw.js'), worker); console.log('Mobile shell built: ' + list.length + ' allowed assets; API and project data are never cached.');