mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix: preserve prompt text on network error and allow offline typing (#276)
* fix: preserve prompt text on network error and allow offline typing * fix: preserve prompt on network error, swallow 524 to avoid duplicate sends * feat: add service worker precaching with app shell and runtime cache strategies * fix: copy frontend/plugins into Docker build context * fix: improve service worker update checks with visibility and focus handlers * fix: add connect timeout to event stream to recover from failed connections * fix: remove optimistic message insert, keep prompt in input until server confirms
This commit is contained in:
1 parent
a7d0c13f9a
commit
3e617c2df8
18 files changed
+569
-57
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { readdir, readFile, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { Plugin } from "vite";
|
||||
|
||||
const SW_FILENAME = "sw.js";
|
||||
const PRECACHE_DIR_PREFIXES = ["assets/"];
|
||||
const PRECACHE_ROOT_FILES = ["index.html", "manifest.json", "favicon.svg"];
|
||||
|
||||
async function collectFiles(dir: string, base = dir): Promise<string[]> {
|
||||
const entries = await readdir(dir, { withFileTypes: true });
|
||||
const files = await Promise.all(
|
||||
entries.map(async (entry) => {
|
||||
const absolute = path.join(dir, entry.name);
|
||||
if (entry.isDirectory()) return collectFiles(absolute, base);
|
||||
return [path.relative(base, absolute)];
|
||||
})
|
||||
);
|
||||
return files.flat();
|
||||
}
|
||||
|
||||
function toUrl(relativePath: string): string {
|
||||
return "/" + relativePath.split(path.sep).join("/");
|
||||
}
|
||||
|
||||
function isPrecacheTarget(relativePath: string): boolean {
|
||||
const url = toUrl(relativePath);
|
||||
if (url === `/${SW_FILENAME}` || url.endsWith(".map")) return false;
|
||||
if (PRECACHE_ROOT_FILES.includes(relativePath)) return true;
|
||||
return PRECACHE_DIR_PREFIXES.some((prefix) => url.startsWith(`/${prefix}`));
|
||||
}
|
||||
|
||||
export function swPrecacheManifest(): Plugin {
|
||||
let outDir = "";
|
||||
|
||||
return {
|
||||
name: "sw-precache-manifest",
|
||||
apply: "build",
|
||||
configResolved(config) {
|
||||
outDir = path.resolve(config.root, config.build.outDir);
|
||||
},
|
||||
async closeBundle() {
|
||||
const swPath = path.join(outDir, SW_FILENAME);
|
||||
let swSource: string;
|
||||
try {
|
||||
swSource = await readFile(swPath, "utf-8");
|
||||
} catch {
|
||||
throw new Error(`[sw-precache-manifest] ${SW_FILENAME} not found in ${outDir}`);
|
||||
}
|
||||
|
||||
const relativePaths = (await collectFiles(outDir)).filter(isPrecacheTarget).sort();
|
||||
|
||||
const hash = createHash("sha256");
|
||||
const urls: string[] = [];
|
||||
for (const relativePath of relativePaths) {
|
||||
const content = await readFile(path.join(outDir, relativePath));
|
||||
const url = toUrl(relativePath);
|
||||
urls.push(url);
|
||||
hash.update(url);
|
||||
hash.update(content);
|
||||
}
|
||||
|
||||
if (!urls.includes("/index.html")) {
|
||||
throw new Error("[sw-precache-manifest] index.html missing from build output");
|
||||
}
|
||||
|
||||
const buildHash = hash.digest("hex").slice(0, 16);
|
||||
const header =
|
||||
`self.__SW_BUILD_HASH__=${JSON.stringify(buildHash)};` +
|
||||
`self.__SW_PRECACHE__=${JSON.stringify(urls)};\n`;
|
||||
|
||||
await writeFile(swPath, header + swSource);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user