Skip to content

Commit

Permalink
added immutable to cache control
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismana committed Jul 12, 2024
1 parent 3ad6b0d commit ff58294
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion source/image-handler/src/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ export class ImageRequest {

return imageRequestInfo;
} catch (error) {
logger.warn('Error occurred while setting up the image request. Error: ', error);
if (error.code && error.code !== 'NoSuchKey') {
logger.warn('Error occurred while setting up the image request. Error: ', error);
}

throw error;
}
Expand Down
14 changes: 12 additions & 2 deletions source/image-handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<ImageHandl

let headers = getResponseHeaders(false);
headers['Content-Type'] = imageRequestInfo.contentType;

if (imageRequestInfo.expires) {
// eslint-disable-next-line dot-notation
headers['Expires'] = new Date(imageRequestInfo.expires).toUTCString();
let seconds_until_expiry = Math.min(
31536000,
Math.floor((imageRequestInfo.expires.getTime() - Date.now()) / 1000),
);
headers['Cache-Control'] = 'max-age=' + seconds_until_expiry + ', immutable';
} else {
headers['Cache-Control'] = imageRequestInfo.cacheControl + ', immutable';
}
if (imageRequestInfo.lastModified) {
headers['Last-Modified'] = new Date(imageRequestInfo.lastModified).toUTCString();
}
headers['Cache-Control'] = imageRequestInfo.cacheControl;

// Apply the custom headers overwriting any that may need overwriting
if (imageRequestInfo.headers) {
headers = { ...headers, ...imageRequestInfo.headers };
Expand All @@ -70,7 +78,9 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<ImageHandl
body: processedRequest,
};
} catch (error) {
logger.warn('Error occurred during image processing', { error });
if (error.code && error.code !== 'NoSuchKey') {
logger.warn('Error occurred during image processing', { error });
}
const { statusCode, body } = getErrorResponse(error);
return {
statusCode,
Expand Down
2 changes: 1 addition & 1 deletion source/image-handler/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('index', () => {
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Content-Type': 'image/jpeg',
'Cache-Control': 'max-age=31536000',
'Cache-Control': 'max-age=31536000, immutable',
Expires: undefined,
'Last-Modified': undefined,
},
Expand Down

0 comments on commit ff58294

Please sign in to comment.