Add CloudFront Function for directory index routing

- Create CloudFront Function to rewrite directory requests to index.html
- Fix admin login page routing issue (/upadaj -> /upadaj/index.html)
- Attach function to CloudFront distribution default cache behavior
- Enables proper routing for all admin pages without .html extension
This commit is contained in:
DaX
2025-10-31 02:54:46 +01:00
parent d3e001707b
commit 6bc1c8d16d
2 changed files with 31 additions and 0 deletions

View File

@@ -40,6 +40,15 @@ resource "aws_cloudfront_origin_access_control" "frontend" {
signing_protocol = "sigv4"
}
# CloudFront Function for directory index rewrites
resource "aws_cloudfront_function" "index_rewrite" {
name = "${var.app_name}-index-rewrite"
runtime = "cloudfront-js-1.0"
comment = "Rewrite directory requests to index.html"
publish = true
code = file("${path.module}/cloudfront-function.js")
}
# CloudFront distribution
resource "aws_cloudfront_distribution" "frontend" {
enabled = true
@@ -74,6 +83,11 @@ resource "aws_cloudfront_distribution" "frontend" {
default_ttl = 3600 # 1 hour
max_ttl = 86400 # 24 hours
compress = true
function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.index_rewrite.arn
}
}
# Custom error responses for SPA routing

View File

@@ -0,0 +1,17 @@
function handler(event) {
var request = event.request;
var uri = request.uri;
// Check whether the URI is missing a file extension
if (!uri.includes('.')) {
// Check if URI ends with /
if (uri.endsWith('/')) {
request.uri += 'index.html';
} else {
// Add /index.html to directory paths
request.uri += '/index.html';
}
}
return request;
}