Skip to content

Commit

Permalink
Merge pull request #1014 from jboolean/edge-redirect
Browse files Browse the repository at this point in the history
Add edge redirection for missing resolutions to full res
  • Loading branch information
jboolean authored Jul 17, 2024
2 parents d365a28 + 53cc622 commit 6e85a9f
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 6e85a9f

Please sign in to comment.