ConditionalHeaders

val ConditionalHeaders: <ERROR CLASS><ConditionalHeadersConfig>

A plugin that avoids sending the body of content if it has not changed since the last request. This is achieved by using the following headers:

  • The Last-Modified response header contains a resource modification time. For example, if the client request contains the If-Modified-Since value, Ktor will send a full response only if a resource has been modified after the given date.

  • The Etag response header is an identifier for a specific resource version. For instance, if the client request contains the If-None-Match value, Ktor won't send a full response in case this value matches the Etag.

The code snippet below shows how to add a Etag and Last-Modified headers for CSS:

install(ConditionalHeaders) {
version { call, outgoingContent ->
when (outgoingContent.contentType?.withoutParameters()) {
ContentType.Text.CSS -> listOf(EntityTagVersion("abc123"), LastModifiedVersion(Date(1646387527500)))
else -> emptyList()
}
}
}

You can learn more from Conditional headers.