Easy '304 Not Modified' Responses

预计阅读时间: 2 分钟

ConditionalHeaders feature adds the ability to avoid sending content if the client already has the same content. It does so by checking the ETag or LastModified properties of the Resource or FinalContent that are sent and comparing these properties to what client indicates it is having. If the conditions allow it, the entire content is not sent and a “304 Not Modified” response is sent instead.

本特性在 io.ktor.features.ConditionalHeaders 类中定义,无需任何额外构件。

Configuration

You can install and use ConditionalHeaders without additional configuration:

install(ConditionalHeaders)

It also allows to configure a lambda to fetch a version list from the generated OutgoingContent passed as parameter of the lambda:

install(ConditionalHeaders) {
    version { content -> listOf(EntityTagVersion("tag1")) }
}

Extensibility

Version interface implementations are attached to the Resource instances, and you can return custom implementations with your own logic. Please note that FinalContent is only checked for ETag and LastModified headers.