Skip to content

Commit 500d513

Browse files
committed
feat(cache): implement cache control for static UI resources
- Added Cache-Control headers to responses for static UI resources, setting a max-age of 1 day and requiring revalidation. - Updated routes in PlanRoutes to include cache control for HTML and SVG resources, improving performance and resource management.
1 parent c34474a commit 500d513

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

  • app/src/main/scala/io/github/datacatering/datacaterer/core/ui/plan

app/src/main/scala/io/github/datacatering/datacaterer/core/ui/plan/PlanRoutes.scala

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.apache.pekko.actor.typed.scaladsl.AskPattern.{Askable, schedulerFromA
1414
import org.apache.pekko.actor.typed.{ActorRef, ActorSystem}
1515
import org.apache.pekko.http.scaladsl.model.HttpMethods._
1616
import org.apache.pekko.http.scaladsl.model.headers._
17+
import org.apache.pekko.http.scaladsl.model.headers.CacheDirectives.{`max-age`, `must-revalidate`}
1718
import org.apache.pekko.http.scaladsl.model.{ContentType, HttpEntity, HttpResponse, MediaTypes, StatusCodes, HttpHeader}
1819
import org.apache.pekko.http.scaladsl.server.{Directives, ExceptionHandler, Route}
1920
import org.apache.pekko.util.Timeout
@@ -42,6 +43,9 @@ class PlanRoutes(
4243

4344
// Log CORS configuration at startup
4445
UiConfiguration.Cors.logConfiguration()
46+
47+
// Cache control for static UI resources (1 day max-age with must-revalidate)
48+
private val uiCacheControl = `Cache-Control`(`max-age`(86400), `must-revalidate`)
4549

4650
/**
4751
* CORS configuration to allow cross-origin requests.
@@ -172,35 +176,47 @@ class PlanRoutes(
172176
lazy val planRoutes: Route = corsHandler(concat(
173177
path("") {
174178
get {
175-
getFromResource("ui/index.html")
179+
respondWithHeader(uiCacheControl) {
180+
getFromResource("ui/index.html")
181+
}
176182
}
177183
},
178184
path("connection") {
179185
get {
180-
getFromResource("ui/connection/connection.html")
186+
respondWithHeader(uiCacheControl) {
187+
getFromResource("ui/connection/connection.html")
188+
}
181189
}
182190
},
183191
path("plan") {
184192
get {
185-
getFromResource("ui/plan/plan.html")
193+
respondWithHeader(uiCacheControl) {
194+
getFromResource("ui/plan/plan.html")
195+
}
186196
}
187197
},
188198
path("history") {
189199
get {
190-
getFromResource("ui/history/history.html")
200+
respondWithHeader(uiCacheControl) {
201+
getFromResource("ui/history/history.html")
202+
}
191203
}
192204
},
193205
path("ui" / Segments(1, 2)) { fileName =>
194206
val hasOnlyAlphanumericAndDash = fileName.forall(_.matches("[0-9a-z-]+(\\.(html|css|js))?"))
195207
if (hasOnlyAlphanumericAndDash) {
196-
getFromResource(s"ui/${fileName.mkString("/")}")
208+
respondWithHeader(uiCacheControl) {
209+
getFromResource(s"ui/${fileName.mkString("/")}")
210+
}
197211
} else {
198212
complete(HttpResponse(StatusCodes.BadRequest, entity = s"Unable to fetch resource for request"))
199213
}
200214
},
201215
path("data_catering_transparent.svg") {
202216
get {
203-
getFromResource("report/data_catering_transparent.svg")
217+
respondWithHeader(uiCacheControl) {
218+
getFromResource("report/data_catering_transparent.svg")
219+
}
204220
}
205221
},
206222
pathPrefix("run") {

0 commit comments

Comments
 (0)