Client Caching (Policy)
This concern allows to instruct clients to cache responses generated
by the server for some duration by adding the Expires HTTP header.
The following example will cause the static website to be cached for one week:
val website = StaticWebsite.from(ResourceTree.fromDirectory("./website"))
.add(ClientCache.policy().duration(7.days))
Host.create()
.handler(website)
.run()The duration is a kotlin.time.Duration - duration(7) is a shortcut for a number of days.
The header is added to successful (200 OK) responses to GET requests.
If needed, a predicate can be passed to specify the content that should be cached more explicitly:
val policy = ClientCache.policy()
.duration(7.days)
.predicate { _, response -> response.content?.type != ContentType.TextHtml } // do not cache HTML pages