Return 404 when static fallback index is missing
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
92bedca342
commit
06785908e1
2 files changed
+19
-2
No files matched your search
@@ -404,4 +404,12 @@ describe("resolveStaticAssetReadTarget", () => {
|
||||
kind: "forbidden",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns not_found when spa fallback file is missing", () => {
|
||||
const tempDir = mkdtempSync(path.join(os.tmpdir(), "t3-static-missing-index-"));
|
||||
const result = resolveStaticAssetReadTarget("/missing.js", tempDir);
|
||||
expect(result).toEqual({
|
||||
kind: "not_found",
|
||||
});
|
||||
});
|
||||
});
|
||||
+11
-2
@@ -433,7 +433,11 @@ export function resolveStaticAssetReadTarget(
|
||||
requestUrl: string | undefined,
|
||||
distRoot: string,
|
||||
resolvedRealDistRoot?: string,
|
||||
): { kind: "file"; filePath: string } | { kind: "forbidden" } | { kind: "bad_request" } {
|
||||
):
|
||||
| { kind: "file"; filePath: string }
|
||||
| { kind: "forbidden" }
|
||||
| { kind: "bad_request" }
|
||||
| { kind: "not_found" } {
|
||||
const normalizedDistRoot = path.resolve(distRoot);
|
||||
const realDistRoot =
|
||||
resolvedRealDistRoot ??
|
||||
@@ -461,7 +465,7 @@ export function resolveStaticAssetReadTarget(
|
||||
const indexPath = path.join(normalizedDistRoot, "index.html");
|
||||
const indexFile = resolveSafeFilePathInDist(indexPath, realDistRoot);
|
||||
if (indexFile.kind !== "file") {
|
||||
return { kind: "bad_request" };
|
||||
return indexFile.kind === "forbidden" ? { kind: "forbidden" } : { kind: "not_found" };
|
||||
}
|
||||
|
||||
return indexFile;
|
||||
@@ -494,6 +498,11 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
response.end("Forbidden");
|
||||
return;
|
||||
}
|
||||
if (resolvedPath.kind === "not_found") {
|
||||
response.statusCode = 404;
|
||||
response.end("Not found");
|
||||
return;
|
||||
}
|
||||
|
||||
const targetPath = resolvedPath.filePath;
|
||||
fs.readFile(targetPath, (error, content) => {
|
||||
|
||||
Reference in new issue
Block a user