Controlling cache headers

Estimated reading time: 1 minute

The CachingOptions feature adds the ability to send the headers Cache-Control and Expires used by clients and proxies to cache requests in an easy way.

This feature is defined in the class io.ktor.features.CachingHeaders and no additional artifacts are required.

The basic feature is installed just like many others, but for it to do something, you have to define options blocks transforming outputContent to CachingOptions using for example


This feature adds an options configuration method that allows you to define code to optionally select a CachingOptions from a provided outgoingContent: OutgoingContent. You can, for example, use the Content-Type of the outgoing message to determine which Cache-Control to use:

install(CachingHeaders) {
    options { outgoingContent ->
        when (outgoingContent.contentType?.withoutParameters()) {
            ContentType.Text.CSS -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 24 * 60 * 60))
            else -> null