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:
Last-Modifiedresponse header contains a resource modification time. For example, if the client request contains the
If-Modified-Sincevalue, 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-Modifiedheader automatically after installing
Etagresponse header is an identifier for a specific resource version. For instance, if the client request contains the
If-None-Matchvalue, Ktor won't send a full response in case this value matches the
Etag. You can specify the
Etagvalue when configuring
ConditionalHeaders, you need to include the
ktor-server-conditional-headers artifact in the build script:
... inside the
... inside the explicitly defined
module, which is an extension function of the
ConditionalHeaders plugin can also be installed to specific routes. This might be useful if you need different
ConditionalHeaders configurations for different application resources.
ConditionalHeaders, you need to call the version function inside the
install block. This function provides access to a list of resource versions for a given
OutgoingContent. You can specify the required versions by using the EntityTagVersion and LastModifiedVersion class objects.
The code snippet below shows how to add a
Last-Modified headers for CSS:
You can find the full example here: conditional-headers.