Skip to content

Commit

Permalink
refactor module to allow custom cache policy for static assets
Browse files Browse the repository at this point in the history
  • Loading branch information
andylamp committed Dec 8, 2023
1 parent 88d565c commit 91384ea
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ module "assets" {
region = local.aws_region
default_tags = var.default_tags

prefix = "${var.prefix}-assets"
assets_path = "${local.opennext_abs_path}/assets"
cache_path = "${local.opennext_abs_path}/cache"
server_function_role_arn = module.server_function.lambda_role.arn
prefix = "${var.prefix}-assets"
assets_path = "${local.opennext_abs_path}/assets"
cache_path = "${local.opennext_abs_path}/cache"
server_function_role_arn = module.server_function.lambda_role.arn
static_asset_cache_config = var.static_asset_cache_config
}


Expand Down
2 changes: 1 addition & 1 deletion modules/opennext-assets/s3.tf
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ resource "aws_s3_object" "assets" {
key = "assets/${each.value}"
source = "${var.assets_path}/${each.value}"
source_hash = filemd5("${var.assets_path}/${each.value}")
cache_control = length(regexall(".*(_next).*$", each.value)) > 0 ? "public,max-age=31536000,immutable" : "public,max-age=0,s-maxage=31536000,must-revalidate"
cache_control = length(regexall(".*(_next).*$", each.value)) > 0 ? "public,max-age=31536000,immutable" : var.static_asset_cache_config
content_type = lookup(local.content_type_lookup, split(".", each.value)[length(split(".", each.value)) - 1], "text/plain")
}

Expand Down
5 changes: 5 additions & 0 deletions modules/opennext-assets/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ variable "cache_path" {
description = "The path of the open-next cache"
}

variable "static_asset_cache_config" {
type = string
description = "Static asset cache config"
}

variable "server_function_role_arn" {
type = string
description = "The IAM role ARN of the Next.js server lambda function"
Expand Down
9 changes: 9 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ variable "evaluate_target_health" {
description = "Allow Route53 to determine whether to respond to DNS queries by checking the health of the record set"
}

/**
* OpenNext Assets variables
**/
variable "static_asset_cache_config" {
type = string
description = "Static asset cache config"
default = "public,max-age=0,s-maxage=31536000,must-revalidate"
}

/**
* OpenNext Variables
**/
Expand Down

0 comments on commit 91384ea

Please sign in to comment.