From 98d0c342f3c9697a6c7c5421966ca8dae6b65bd6 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 7 Sep 2023 15:12:11 +0800 Subject: [PATCH] refactor: set cache-control for bundle resources of plugin (#4557) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /milestone 2.10.x /area core #### What this PR does / why we need it: 为插件捆绑资源设置 cache-control 以优化静态资源加载 如获取插件 bundle.js 会自动携带参数缓存时间为 7 天,当有插件停止或新增时 v 参数会变化浏览器则使用新的 key 缓存静态资源,旧的 key 将在一天内失效 ``` /apis/api.console.halo.run/v1alpha1/plugins/-/bundle.js?v=6c5956f37e7207ab1c0f2f2340f51a101f46b748233992d73729415cd58f3587 ``` #### Which issue(s) this PR fixes: Fixes #4543 #### Does this PR introduce a user-facing change? ```release-note 为插件捆绑资源设置 cache-control 以优化静态资源加载 ``` --- .../run/halo/app/core/extension/endpoint/PluginEndpoint.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/application/src/main/java/run/halo/app/core/extension/endpoint/PluginEndpoint.java b/application/src/main/java/run/halo/app/core/extension/endpoint/PluginEndpoint.java index bda4443b0..995f8d33c 100644 --- a/application/src/main/java/run/halo/app/core/extension/endpoint/PluginEndpoint.java +++ b/application/src/main/java/run/halo/app/core/extension/endpoint/PluginEndpoint.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.function.Predicate; import lombok.AllArgsConstructor; @@ -39,6 +40,7 @@ import org.springdoc.webflux.core.fn.SpringdocRouteBuilder; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.dao.OptimisticLockingFailureException; import org.springframework.data.domain.Sort; +import org.springframework.http.CacheControl; import org.springframework.http.MediaType; import org.springframework.http.codec.multipart.FilePart; import org.springframework.http.codec.multipart.FormFieldPart; @@ -70,6 +72,7 @@ import run.halo.app.plugin.PluginNotFoundException; @Component @AllArgsConstructor public class PluginEndpoint implements CustomEndpoint { + private static final CacheControl MAX_CACHE_CONTROL = CacheControl.maxAge(365, TimeUnit.DAYS); private final ReactiveExtensionClient client; @@ -247,6 +250,7 @@ public class PluginEndpoint implements CustomEndpoint { return pluginService.uglifyJsBundle() .defaultIfEmpty("") .flatMap(bundle -> ServerResponse.ok() + .cacheControl(MAX_CACHE_CONTROL) .contentType(MediaType.valueOf("text/javascript")) .bodyValue(bundle) ); @@ -263,6 +267,7 @@ public class PluginEndpoint implements CustomEndpoint { } return pluginService.uglifyCssBundle() .flatMap(bundle -> ServerResponse.ok() + .cacheControl(MAX_CACHE_CONTROL) .contentType(MediaType.valueOf("text/css")) .bodyValue(bundle) );