add ETag and cache-control headers for 200 responses (#231)

* add etag and cache control headers for 200 responses

* fixup eTag -> ETag
This commit is contained in:
Blayne Chard
2023-08-08 15:11:42 +12:00
committed by GitHub
parent 0528e12ec9
commit d1ea859443

View File

@@ -14,6 +14,7 @@ import {
import { pmtiles_path, tile_path, tileJSON } from "../../shared/index"; import { pmtiles_path, tile_path, tileJSON } from "../../shared/index";
import zlib from "zlib"; import zlib from "zlib";
import { createHash } from "crypto"
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { NodeHttpHandler } from "@aws-sdk/node-http-handler"; import { NodeHttpHandler } from "@aws-sdk/node-http-handler";
@@ -206,6 +207,9 @@ export const handlerRaw = async (
data = tilePostprocess(data, header.tileType); data = tilePostprocess(data, header.tileType);
} }
headers["Cache-Control"] = `public, max-age=${process.env.CACHE_MAX_AGE || 86400}`;
headers["ETag"] = `"${createHash("sha256").update(Buffer.from(data)).digest("hex")}"`
if (is_api_gateway) { if (is_api_gateway) {
// this is wasted work, but we need to force API Gateway to interpret the Lambda response as binary // this is wasted work, but we need to force API Gateway to interpret the Lambda response as binary
// without depending on clients sending matching Accept: headers in the request. // without depending on clients sending matching Accept: headers in the request.
@@ -235,7 +239,6 @@ export const handlerRaw = async (
} }
throw e; throw e;
} }
return apiResp(404, "Invalid URL", false, headers);
}; };
export const handler = async ( export const handler = async (