Skip to content

Instantly share code, notes, and snippets.

@bouroo
Last active September 10, 2024 14:17
Show Gist options
  • Save bouroo/bee08a818c7c7a31830623f5c71d4c72 to your computer and use it in GitHub Desktop.
Save bouroo/bee08a818c7c7a31830623f5c71d4c72 to your computer and use it in GitHub Desktop.
AWS CloudFront function for static web app on Amazon S3
async function handler(event) {
const request = event.request;
const uri = request.uri;
// Extract pathname and check conditions
const hostnameWithPath = uri.split('//').pop();
const hasQuery = uri.indexOf('?') !== -1;
const hasExtension = uri.indexOf('.') !== -1 && uri.lastIndexOf('.') > uri.lastIndexOf('/');
const pathEndWithSlash = uri.charAt(uri.length - 1) === '/';
const isRootUrl = hostnameWithPath.indexOf('/') === -1 || hostnameWithPath.indexOf('/') === (hostnameWithPath.length - 1);
// Handle search query
if (hasQuery) {
const idx = uri.lastIndexOf('?');
request.uri = uri.substring(0, idx) + '.html' + uri.substring(idx);
}
// Handle root url
else if (isRootUrl) {
request.uri = uri.replace(/\/$/, '') + '/index.html';
}
// Handle path ending with a slash (but not the root url)
else if (pathEndWithSlash) {
request.uri = uri.slice(0, -1) + '.html';
}
// Handle default case for paths without an extension
else if (!hasExtension) {
request.uri += '.html';
}
return request;
}
export { handler };
import { handler } from './rewrite.js';
const testCases = [
// Test case 1: URL with query string (should append .html before the query)
{
input: {
"request": {
"uri": "https://localhost.cloudfront.net/login?token=eiei"
}
},
expected: {
"request": {
"uri": "https://localhost.cloudfront.net/login.html?token=eiei"
}
}
},
// Test case 2: Root URL (should append /index.html)
{
input: {
"request": {
"uri": "https://localhost.cloudfront.net/"
}
},
expected: {
"request": {
"uri": "https://localhost.cloudfront.net/index.html"
}
}
},
// Test case 3: URL ending with a slash (should replace with .html)
{
input: {
"request": {
"uri": "https://localhost.cloudfront.net/test/login/"
}
},
expected: {
"request": {
"uri": "https://localhost.cloudfront.net/test/login.html"
}
}
},
// Test case 4: URL without an extension (should append .html)
{
input: {
"request": {
"uri": "https://localhost.cloudfront.net/test/login"
}
},
expected: {
"request": {
"uri": "https://localhost.cloudfront.net/test/login.html"
}
}
},
// Test case 5: URL with an extension (should remain unchanged)
{
input: {
"request": {
"uri": "https://localhost.cloudfront.net/test/img.jpg"
}
},
expected: {
"request": {
"uri": "https://localhost.cloudfront.net/test/img.jpg"
}
}
},
// Test case 6: URL without protocol (to check how it handles)
{
input: {
"request": {
"uri": "https://localhost.cloudfront.net/test"
}
},
expected: {
"request": {
"uri": "https://localhost.cloudfront.net/test.html"
}
}
},
];
// Run tests
(async () => {
for (const testCase of testCases) {
const result = await handler(testCase.input);
console.log(`Input: ${JSON.stringify(testCase.input.request)}`);
console.log(`Expected: ${JSON.stringify(testCase.expected.request)}`);
console.log(`Result: ${JSON.stringify(result)}`);
console.log(`Passed: ${result.uri === testCase.expected.request.uri}`);
console.log('---');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment