Skip to content

Latest commit

 

History

History
38 lines (26 loc) · 2.75 KB

conditional_headers.md

File metadata and controls

38 lines (26 loc) · 2.75 KB

Required dependencies: io.ktor:%artifact_name%

The ConditionalHeaders plugin 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. Note that for static files Ktor appends the Last-Modified header automatically after installing ConditionalHeaders.
  • 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. You can specify the Etag value when configuring ConditionalHeaders.

Add dependencies {id="add_dependencies"}

Install %plugin_name% {id="install_plugin"}

Configure headers {id="configure"}

To configure %plugin_name%, you need to call the version function inside the install block. This function provides access to a list of resource versions for a given ApplicationCall and OutgoingContent. You can specify the required versions by using the EntityTagVersion and LastModifiedVersion class objects.

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

{src="snippets/conditional-headers/src/main/kotlin/com/example/Application.kt" lines="16-27"}

You can find the full example here: conditional-headers.