Skip to content

Commit 361b4c3

Browse files
committed
Add generation for new URL format
1 parent a75f3a6 commit 361b4c3

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,28 @@ exports.handler = async (event) => {
6868
const imageBuffer = fs.readFileSync(imagePath);
6969
const etag = crypto.createHash("md5").update(imageBuffer).digest("hex");
7070

71+
// Try to extract the 10-digit timestamp from the URL
72+
// if it ends with -${timestamp}.jpg
73+
const match = event.rawPath.match(/-(\d{10})\.\w+$/);
74+
75+
const isTimestamped = !!match;
76+
const cacheControl = isTimestamped
77+
? "public, max-age=31536000, immutable" // New URL containing a timestamp that will never change
78+
: "public, max-age=86400"; // Legacy URL without a timestamp
79+
80+
// Use extracted timestamp if available, else use current time
81+
const lastModified = isTimestamped
82+
? new Date(parseInt(match[1], 10) * 1000).toUTCString()
83+
: new Date().toUTCString();
84+
7185
return {
7286
statusCode: 200,
7387
body: fs.readFileSync(imagePath, { encoding: "base64" }),
7488
headers: {
7589
"Content-Type": "image/jpg",
76-
"Cache-Control": "public, max-age=86400", // One day
77-
"Last-Modified": new Date().toUTCString()
90+
"Cache-Control": cacheControl,
91+
"Last-Modified": lastModified,
92+
"Etag": `W/"${etag}"`
7893
},
7994
isBase64Encoded: true,
8095
};

0 commit comments

Comments
 (0)