import { existsSync } from 'node:fs' import { join } from 'node:path' import react from '@vitejs/plugin-react' import compression from 'compression' import { defineConfig, type Connect, type Plugin } from 'vite' // `vite preview` is what serves the built site, so the two things a web // server would be set up to do are done here. const servePreview: Plugin = { name: 'serve-preview', configurePreviewServer(server) { const assets = join(server.config.root, server.config.build.outDir) // Typed for Express, and at home in any Node server. server.middlewares.use(compression() as Connect.NextHandleFunction) // A built file carries a hash of its contents in its name, so a browser // can keep it for good and never ask again. Only for files that are // there: a name from an older build must not be remembered as missing. server.middlewares.use((req, res, next) => { const path = (req.url ?? '').split('?')[0] if (path.startsWith('/assets/') && !path.includes('..') && existsSync(join(assets, path))) { res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') } next() }) }, } // https://vite.dev/config/ export default defineConfig({ plugins: [react(), servePreview], preview: { host: true, port: 5173, allowedHosts: ['the-sunken-reach.crosscode.co.za'], }, })