fix: resolve manifest.json 404 on nested routes by adding base tag and improving server fallback
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="icon" type="image/svg+xml" href="./logo.svg" />
|
||||
<link rel="manifest" href="./manifest.json" />
|
||||
|
||||
31
src/index.ts
31
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user