From c4de7e4e6297c8653400b753ed81372c7cbeae04 Mon Sep 17 00:00:00 2001 From: bipproduction Date: Mon, 9 Feb 2026 12:32:28 +0800 Subject: [PATCH] fix: resolve manifest.json 404 on nested routes by adding base tag and improving server fallback --- src/index.html | 1 + src/index.ts | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/index.html b/src/index.html index 41d5541..b430e8d 100644 --- a/src/index.html +++ b/src/index.html @@ -3,6 +3,7 @@ + diff --git a/src/index.ts b/src/index.ts index 0220ce2..6a6bd72 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,12 +15,13 @@ if (!isProduction) { const { createVite } = await import("./vite"); const vite = await createVite(); - // Serve PWA/TWA assets in dev - app.get("/manifest.json", () => Bun.file("src/manifest.json")); - app.get("/sw.js", () => Bun.file("src/sw.js")); - app.get("/.well-known/assetlinks.json", () => - Bun.file("src/.well-known/assetlinks.json"), - ); + // Serve PWA/TWA assets in dev (root and nested path support) + const servePwaAsset = (srcPath: string) => () => Bun.file(srcPath); + app.get("/manifest.json", servePwaAsset("src/manifest.json")); + app.get("**/manifest.json", servePwaAsset("src/manifest.json")); + app.get("/sw.js", servePwaAsset("src/sw.js")); + app.get("**/sw.js", servePwaAsset("src/sw.js")); + app.get("/.well-known/assetlinks.json", servePwaAsset("src/.well-known/assetlinks.json")); app.post("/__open-in-editor", ({ body }) => { const { relativePath, lineNumber, columnNumber } = body as { @@ -156,13 +157,23 @@ if (!isProduction) { } } - // 2. If not found and looks like an asset (has extension), try root of dist + // 2. If not found and looks like an asset (has extension), try root of dist or src if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) { if (pathname.includes(".") && !pathname.endsWith("/")) { const filename = path.basename(pathname); - const fallbackPath = path.join("dist", filename); - if (fs.existsSync(fallbackPath) && fs.statSync(fallbackPath).isFile()) { - filePath = fallbackPath; + + // Try root of dist + const fallbackDistPath = path.join("dist", filename); + if (fs.existsSync(fallbackDistPath) && fs.statSync(fallbackDistPath).isFile()) { + filePath = fallbackDistPath; + } + // Special handling for PWA files in src + else if (filename === "manifest.json" || filename === "sw.js" || pathname.includes("assetlinks.json")) { + const srcFilename = pathname.includes("assetlinks.json") ? ".well-known/assetlinks.json" : filename; + const fallbackSrcPath = path.join("src", srcFilename); + if (fs.existsSync(fallbackSrcPath) && fs.statSync(fallbackSrcPath).isFile()) { + filePath = fallbackSrcPath; + } } } }