Skip to content

Commit

Permalink
Add 404 fallback option (kotx#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisa authored Sep 21, 2022
1 parent 8dc6903 commit 7fae74b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Env {
CACHE_CONTROL?: string,
PATH_PREFIX?: string
INDEX_FILE?: string
NOTFOUND_FILE?: string
}

type ParsedRange = { offset: number, length: number } | { suffix: number };
Expand Down Expand Up @@ -132,31 +133,46 @@ export default {
? await env.R2_BUCKET.head(path)
: ((file && hasBody(file)) ? file : await env.R2_BUCKET.get(path, { range }));

let notFound: boolean = false;

if (file === null) {
return new Response("File Not Found", { status: 404 });
if (env.NOTFOUND_FILE && env.NOTFOUND_FILE != "") {
notFound = true;
path = env.NOTFOUND_FILE;
file = request.method === "HEAD"
? await env.R2_BUCKET.head(path)
: await env.R2_BUCKET.get(path);
}

// if its still null, either 404 is disabled or that file wasn't found either
// this isn't an else because then there would have to be two of theem
if (file == null) {
return new Response("File Not Found", { status: 404 });
}
}

response = new Response((hasBody(file) && file.size !== 0) ? file.body : null, {
status: range ? 206 : 200,
status: notFound ? 404 : (range ? 206 : 200),
headers: {
"accept-ranges": "bytes",
"access-control-allow-origin": env.ALLOWED_ORIGINS || "",

"etag": file.httpEtag,
"cache-control": file.httpMetadata?.cacheControl ?? (env.CACHE_CONTROL || ""),
"etag": notFound ? "" : file.httpEtag,
// if the 404 file has a custom cache control, we respect it
"cache-control": file.httpMetadata?.cacheControl ?? (notFound ? "" : env.CACHE_CONTROL || ""),
"expires": file.httpMetadata?.cacheExpiry?.toUTCString() ?? "",
"last-modified": file.uploaded.toUTCString(),
"last-modified": notFound ? "" : file.uploaded.toUTCString(),

"content-encoding": file.httpMetadata?.contentEncoding ?? "",
"content-type": file.httpMetadata?.contentType ?? "application/octet-stream",
"content-language": file.httpMetadata?.contentLanguage ?? "",
"content-disposition": file.httpMetadata?.contentDisposition ?? "",
"content-range": range ? getRangeHeader(range, file.size) : "",
"content-length": (range ? (rangeHasLength(range) ? range.length : range.suffix) : file.size).toString()
"content-range": (range && !notFound ? getRangeHeader(range, file.size) : ""),
"content-length": (range && !notFound ? (rangeHasLength(range) ? range.length : range.suffix) : file.size).toString()
}
});

if (request.method === "GET" && !range && isCachingEnabled)
if (request.method === "GET" && !range && isCachingEnabled && !notFound)
ctx.waitUntil(cache.put(request, response.clone()));
}

Expand Down
7 changes: 7 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ PATH_PREFIX = ""
#INDEX_FILE = ""
INDEX_FILE = "index.html"

# 404 file to use for the bucket when a file is not found.
# Incurs an additional read operation for 404 requests.
# Set to "" to disable 404 behaviour.
# Relative to the root of the bucket.
NOTFOUND_FILE = ""
#NOT_FOUND_FILE = "404.html"

[[r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "kot" # Set this to your R2 bucket name. Required
Expand Down

0 comments on commit 7fae74b

Please sign in to comment.