Skip to content

Commit

Permalink
add index.html function
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbass committed Apr 16, 2024
1 parent 5e22936 commit 67911c4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/deploy-frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ jobs:
run: cd frontend && yarn install

- name: Build project
run: cd frontend && yarn build --mode ${{inputs.build_environment}}
run: |
cp /build/frontend/.env.staging.example /build/frontend/.env.staging
cd frontend && yarn build --mode ${{inputs.build_environment}}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
Expand Down
29 changes: 25 additions & 4 deletions tofu/modules/services/frontend/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ data "aws_secretsmanager_secret_version" "x_allow_value" {
resource "aws_cloudfront_distribution" "appointment" {
comment = "appointment ${var.environment} frontend"
enabled = true
default_root_object = "index.html"
//default_root_object = "index.html"

aliases = ["${var.environment}.appointment.day"]

Expand Down Expand Up @@ -197,15 +197,36 @@ resource "aws_cloudfront_function" "rewrite_api" {
request.uri = request.uri.replace(apiPath, "");
}
// Remove the index.html default root object added by Cloudfront
if (request.uri.endsWith('index.html')) {
request.uri = request.uri.replace('index.html', "");
}
//if (request.uri.endsWith('index.html')) {
// request.uri = request.uri.replace('index.html', "");
//}
// else carry on like normal.
return request;
}
EOT
}

resource "aws_cloudfront_function" "add_index" {
name = "${var.name_prefix}-add-index"
runtime = "cloudfront-js-2.0"
code = <<EOT
async function handler(event) {
const request = event.request;
const uri = request.uri;
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (uri == '') {
request.uri += '/index.html';
}
return request;
}
EOT
}
resource "aws_s3_bucket" "request_logs" {
bucket = local.log_bucket
force_destroy = true
Expand Down

0 comments on commit 67911c4

Please sign in to comment.