【发布时间】:2020-01-18 14:09:33
【问题描述】:
为 Ktor 提供的静态图像发回缓存标头的正确方法是什么?
我有以下 Ktor 设置:
在我的main:
embeddedServer(
Netty,
watchPaths = listOf("module"),
module = Application::module,
port = if (ENV.env == LOCAL) {
8080
} else {
80
}
).apply {
start(wait = true)
}
然后在main之外:
fun Application.module() {
if (ENV.env != LOCAL) {
install(ForwardedHeaderSupport)
install(XForwardedHeaderSupport)
install(HttpsRedirect)
}
install(CachingHeaders) {
options { outgoingContent ->
when (outgoingContent.contentType?.withoutParameters()) {
ContentType.Image.Any -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 30 * 24 * 60 * 60))
else -> null
}
}
}
install(Compression) {
gzip {
priority = 1.0
}
deflate {
priority = 10.0
minimumSize(1024) // condition
}
}
routing {
static("/js/") {
resources("/js/")
}
static("/css/") {
resources("/css/")
}
static("/favicons") {
resources("/favicons/")
}
static("/img/") {
resources("/static/img/")
resources("/static/images/")
resources("/background/")
resources("/logos/")
resources("/icons/")
}
}
}
但是图像返回时没有缓存标头,有什么想法吗?
更新:
将ContentType.Image.Any 更改为ContentType.Image.JPEG 似乎可行。查看Image的源代码,它似乎映射到ContentType(image, *) 但根本不匹配任何图像类型。
install(CachingHeaders) {
options { outgoingContent ->
when (outgoingContent.contentType?.withoutParameters()) {
ContentType.Image.JPEG -> CachingOptions(
cacheControl = CacheControl.MaxAge(
mustRevalidate = false,
maxAgeSeconds = 30 * 24 * 60 * 60,
visibility = CacheControl.Visibility.Public
)
)
else -> null
}
}
}
同时提交了一个错误: https://github.com/ktorio/ktor/issues/1366
【问题讨论】: