Skip to content

Commit

Permalink
Add edge redirection for missing resolutions to full res
Browse files Browse the repository at this point in the history
As a temporary measure while I need to use 720w urls but not all images have a 720w.
  • Loading branch information
jboolean committed Jul 17, 2024
1 parent ef031db commit 53cc622
Show file tree
Hide file tree
Showing 6 changed files with 11,085 additions and 0 deletions.
10 changes: 10 additions & 0 deletions edge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
.build

secrets.*.yml
tsoa-build
1 change: 1 addition & 0 deletions edge/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
35 changes: 35 additions & 0 deletions edge/imageOriginResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CloudFrontResponseHandler } from "aws-lambda";

export const handler: CloudFrontResponseHandler = async (event) => {
// Extract the request and response from the event object
const request = event.Records[0].cf.request;
const response = event.Records[0].cf.response;

// Get the requested URI
const requestedUri = request.uri;

if (response.status === "404") {
const match = requestedUri.match(/^\/(\d+)\-(\w+)\/(.*)$/);

// If they requested a specific width that does not exist, redirect to full width
if (match) {
const [, _width, format, rootKey] = match;
return {
status: "302",
statusDescription: "Found",
headers: {
...response.headers,
location: [
{
key: "Location",
value: `/${format}/${rootKey}`,
},
],
},
};
}
}

// Return the original response if no redirect is needed
return response;
};
Loading

0 comments on commit 53cc622

Please sign in to comment.