214 lines
9.4 KiB
TypeScript
214 lines
9.4 KiB
TypeScript
import { defineConfig } from "vite";
|
|
import react from "@vitejs/plugin-react";
|
|
import { VitePWA } from "vite-plugin-pwa";
|
|
import path from "node:path";
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
|
|
// Product branding, overridable at build time so the same bundle can ship as
|
|
// the homelab's viewer or as the public Tokunia demo
|
|
// (`VITE_APP_TITLE=… VITE_APP_SHORT_NAME=… npm run build`).
|
|
const TITLE = process.env.VITE_APP_TITLE || "Claude Agent — context";
|
|
// The manifest name has always been the longer form; a branded build overrides
|
|
// both with the same string.
|
|
const APP_NAME = process.env.VITE_APP_TITLE || "Claude Agent — context viewer";
|
|
const SHORT_NAME = process.env.VITE_APP_SHORT_NAME || "ai-agent";
|
|
const DESCRIPTION =
|
|
process.env.VITE_APP_DESCRIPTION ||
|
|
"Viewer/editor for the homelab's CLAUDE.md files and skills.";
|
|
|
|
// The static sandbox page's content hash: frames load
|
|
// `/sandbox-frame.html?v=<hash>&p=<pool slot>`, which the backend serves
|
|
// `immutable` when `v` matches its own hash of the same file — so a recycled
|
|
// pool slot's URL is a cache hit forever (lib/sandboxFrame.ts).
|
|
const SANDBOX_FRAME_HASH = crypto
|
|
.createHash("sha256")
|
|
.update(fs.readFileSync(path.resolve(__dirname, "public/sandbox-frame.html")))
|
|
.digest("hex")
|
|
.slice(0, 12);
|
|
|
|
export default defineConfig({
|
|
// Build-time id used to bust the persisted React Query (IndexedDB) cache on
|
|
// every deploy, so a changed API response shape can't resurrect stale entries.
|
|
define: {
|
|
__BUILD_ID__: JSON.stringify(String(Date.now())),
|
|
__SANDBOX_FRAME_HASH__: JSON.stringify(SANDBOX_FRAME_HASH),
|
|
},
|
|
resolve: {
|
|
alias: { "@": path.resolve(__dirname, "src") },
|
|
},
|
|
server: { host: true, port: 5180 },
|
|
// es2022: the PWA's browsers (Chrome 89+, Safari 15+) all have it (noVNC,
|
|
// which needed its top-level await, left with the lab-chrome plugin).
|
|
build: {
|
|
target: "es2022",
|
|
rollupOptions: {
|
|
output: {
|
|
manualChunks(id) {
|
|
if (!id.includes("node_modules")) return;
|
|
if (
|
|
id.includes("/react-dom/") ||
|
|
id.includes("/react-router") ||
|
|
id.includes("/@remix-run/router/") ||
|
|
id.includes("/scheduler/") ||
|
|
/\/node_modules\/react\//.test(id)
|
|
) {
|
|
return "vendor-react";
|
|
}
|
|
if (id.includes("/@tanstack/") || id.includes("/idb-keyval/")) {
|
|
return "vendor-query";
|
|
}
|
|
if (
|
|
id.includes("/react-markdown/") ||
|
|
id.includes("/remark-") ||
|
|
id.includes("/rehype-") ||
|
|
id.includes("/mdast-") ||
|
|
id.includes("/micromark") ||
|
|
id.includes("/unist-") ||
|
|
id.includes("/hast-") ||
|
|
id.includes("/unified/") ||
|
|
id.includes("/vfile")
|
|
) {
|
|
return "vendor-markdown";
|
|
}
|
|
// No vendor-prism chunk needed: since @gabvdl/ui 0.49.1 the file
|
|
// editor's extra grammars are vendored functions of
|
|
// prism-react-renderer's Prism (no global, no prismjs import), so
|
|
// chunking can't reorder them. Before that, a /prismjs/ chunk ran
|
|
// the grammars before globalThis.Prism was set — a blank app.
|
|
if (id.includes("/@gabvdl/pet-pet/") || id.includes("/pet-pet/")) {
|
|
return "vendor-pet";
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
plugins: [
|
|
react(),
|
|
// index.html ships the default <title> and home-screen name; a branded
|
|
// build rewrites them here so the served HTML and the manifest below
|
|
// always agree.
|
|
{
|
|
name: "app-title",
|
|
transformIndexHtml: (html) =>
|
|
html
|
|
.replace(/<title>[^<]*<\/title>/, `<title>${TITLE}</title>`)
|
|
.replace(
|
|
/(<meta name="apple-mobile-web-app-title" content=")[^"]*(")/,
|
|
`$1${SHORT_NAME}$2`,
|
|
),
|
|
},
|
|
VitePWA({
|
|
// The generated worker calls skipWaiting()/clientsClaim(), so a new build
|
|
// always activates and takes over the open page's fetches. What it never
|
|
// does is reload that page — src/pwa.ts does, once it sees the handover.
|
|
registerType: "autoUpdate",
|
|
// We register the worker ourselves from src/pwa.ts so we can poll for
|
|
// updates; suppress the <script src="/registerSW.js"> the plugin injects.
|
|
injectRegister: false,
|
|
// Emit `crossorigin="use-credentials"` on <link rel="manifest">. The
|
|
// browser fetches the manifest anonymously by default, so behind Authelia
|
|
// it was answered with the 302 to the portal and then killed by CORS —
|
|
// the manifest never loaded at all, on any page view. Sending the session
|
|
// cookie with it makes the request a plain authenticated 200.
|
|
useCredentials: true,
|
|
// The PNGs are what phones put on the home screen: iOS takes neither an
|
|
// SVG nor the manifest's icons (only apple-touch-icon.png), and Android
|
|
// wants a 192 + 512 raster and a full-bleed maskable one to shape itself.
|
|
includeAssets: ["icon.svg", "apple-touch-icon.png"],
|
|
manifest: {
|
|
name: APP_NAME,
|
|
short_name: SHORT_NAME,
|
|
description: DESCRIPTION,
|
|
theme_color: "#0a0a0a",
|
|
background_color: "#0a0a0a",
|
|
display: "standalone",
|
|
orientation: "portrait",
|
|
start_url: "/",
|
|
icons: [
|
|
{ src: "/icon.svg", sizes: "any", type: "image/svg+xml", purpose: "any" },
|
|
{ src: "/icon-192.png", sizes: "192x192", type: "image/png", purpose: "any" },
|
|
{ src: "/icon-512.png", sizes: "512x512", type: "image/png", purpose: "any" },
|
|
{ src: "/icon-maskable-512.png", sizes: "512x512", type: "image/png", purpose: "maskable" },
|
|
],
|
|
// Puts the installed app in Android's share sheet: a share is POSTed
|
|
// to /share, which the service worker answers itself
|
|
// (public/share-sw.js) and hands to the composer
|
|
// (useShareTarget). Android refreshes an installed app's manifest
|
|
// on its own schedule, so a change here can take a day to show up
|
|
// (reinstalling forces it).
|
|
share_target: {
|
|
action: "/share",
|
|
method: "POST",
|
|
enctype: "multipart/form-data",
|
|
params: {
|
|
title: "title",
|
|
text: "text",
|
|
url: "url",
|
|
files: [{ name: "files", accept: ["*/*"] }],
|
|
},
|
|
},
|
|
},
|
|
workbox: {
|
|
// The plugin only implies these for `injectRegister: "auto"`, and we
|
|
// register the worker ourselves. Without them a new build installs but
|
|
// parks in "waiting" forever, and nothing ever serves the new assets.
|
|
skipWaiting: true,
|
|
clientsClaim: true,
|
|
// Web Push (public/push-sw.js): draws the notification a push carries
|
|
// and answers an ask from its buttons. Precached like any .js, so a
|
|
// change to it changes sw.js and ships as a normal update.
|
|
// The share target (public/share-sw.js): stashes a share POSTed to
|
|
// /share and sends the app to `/?share=<id>`.
|
|
importScripts: ["/push-sw.js", "/share-sw.js"],
|
|
navigateFallback: "/index.html",
|
|
// `?reauth=` is the re-authentication navigation src/api.ts makes when
|
|
// the Authelia session expires: it MUST reach Traefik to be answered
|
|
// with the 302 to the portal. Served from the precache instead, it
|
|
// reboots the logged-out shell and loops. Workbox tests these against
|
|
// `pathname + search`, so a query-string pattern is matched.
|
|
// /oauth/*, /mcp and /.well-known/* are the MCP server's (backend
|
|
// mcpserver/): an app's sign-in navigates to /oauth/authorize, which
|
|
// must reach the backend, not boot the shell.
|
|
navigateFallbackDenylist: [
|
|
/^\/api\//,
|
|
/[?&]reauth=/,
|
|
/^\/oauth\//,
|
|
/^\/mcp(\/|$)/,
|
|
/^\/\.well-known\//,
|
|
// The sandboxed frame's page (public/sandbox-frame.html): a frame
|
|
// navigation that got the shell instead would boot the app inside
|
|
// the form's frame. Precached like any .html; this is for a worker
|
|
// whose precache doesn't have it yet.
|
|
/^\/sandbox-frame\.html/,
|
|
],
|
|
// Drop precaches from previous builds so an upgrade can't serve a mix of
|
|
// old and new hashed chunks.
|
|
cleanupOutdatedCaches: true,
|
|
// Cache the API bundle so the shell still has data on a cold offline start.
|
|
// `/api/events` is exempt: it is an endless SSE stream, and NetworkFirst
|
|
// would try to read the whole body into the cache. That read never
|
|
// finishes, which keeps the old worker alive and wedges every future
|
|
// update in "waiting" forever. Let it go straight to the network.
|
|
// `/api/health` too: it is the reachability probe (src/lib/reachability),
|
|
// which must fail when the lab doesn't answer — and its cache-busting
|
|
// query would evict the useful entries.
|
|
runtimeCaching: [
|
|
{
|
|
urlPattern: ({ url }) =>
|
|
url.pathname.startsWith("/api/") &&
|
|
url.pathname !== "/api/events" &&
|
|
url.pathname !== "/api/health",
|
|
handler: "NetworkFirst",
|
|
options: {
|
|
cacheName: "ai-agent-api",
|
|
networkTimeoutSeconds: 5,
|
|
expiration: { maxEntries: 32, maxAgeSeconds: 60 * 60 * 24 * 30 },
|
|
},
|
|
},
|
|
],
|
|
},
|
|
}),
|
|
],
|
|
});
|