From 0be992a1e26f61a182113266a2eb34f77b87f9b4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 15 Oct 2019 06:05:57 +0800 Subject: [PATCH] Make static resouces web browser cache time customized on app.ini (#8442) * make static resouces web browser cache time customized on app.ini * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md Co-Authored-By: zeripath * Update custom/conf/app.ini.sample Co-Authored-By: Antoine GIRARD * fix docs --- custom/conf/app.ini.sample | 2 ++ docs/content/doc/advanced/config-cheat-sheet.en-us.md | 1 + docs/content/doc/advanced/config-cheat-sheet.zh-cn.md | 1 + modules/setting/setting.go | 2 ++ routers/routes/routes.go | 8 ++++---- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 79d9960052..442ac4b5bb 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -243,6 +243,8 @@ LFS_CONTENT_PATH = data/lfs LFS_JWT_SECRET = ; LFS authentication validity period (in time.Duration), pushes taking longer than this may fail. LFS_HTTP_AUTH_EXPIRY = 20m +; Static resources, includes resources on custom/, public/ and all uploaded avatars web browser cache time, default is 6h +STATIC_CACHE_TIME = 6h ; Define allowed algorithms and their minimum key length (use -1 to disable a type) [ssh.minimum_key_sizes] diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 100bb229ee..6313e705d4 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -140,6 +140,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. - `CERT_FILE`: **custom/https/cert.pem**: Cert file path used for HTTPS. - `KEY_FILE`: **custom/https/key.pem**: Key file path used for HTTPS. - `STATIC_ROOT_PATH`: **./**: Upper level of template and static files path. +- `STATIC_CACHE_TIME`: **6h**: Web browser cache time for static resources on `custom/`, `public/` and all uploaded avatars. - `ENABLE_GZIP`: **false**: Enables application-level GZIP support. - `LANDING_PAGE`: **home**: Landing page for unauthenticated users \[home, explore\]. - `LFS_START_SERVER`: **false**: Enables git-lfs support. diff --git a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md index ab73e2059e..a0e33c6370 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md +++ b/docs/content/doc/advanced/config-cheat-sheet.zh-cn.md @@ -65,6 +65,7 @@ menu: - `CERT_FILE`: 启用HTTPS的证书文件。 - `KEY_FILE`: 启用HTTPS的密钥文件。 - `STATIC_ROOT_PATH`: 存放模板和静态文件的根目录,默认是 Gitea 的根目录。 +- `STATIC_CACHE_TIME`: **6h**: 静态资源文件,包括 `custom/`, `public/` 和所有上传的头像的浏览器缓存时间。 - `ENABLE_GZIP`: 启用应用级别的 GZIP 压缩。 - `LANDING_PAGE`: 未登录用户的默认页面,可选 `home` 或 `explore`。 - `LFS_START_SERVER`: 是否启用 git-lfs 支持. 可以为 `true` 或 `false`, 默认是 `false`。 diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 278ed4b107..629a89766f 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -87,6 +87,7 @@ var ( CertFile string KeyFile string StaticRootPath string + StaticCacheTime time.Duration EnableGzip bool LandingPageURL LandingPage UnixSocketPermission uint32 @@ -607,6 +608,7 @@ func NewContext() { OfflineMode = sec.Key("OFFLINE_MODE").MustBool() DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool() StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(AppWorkPath) + StaticCacheTime = sec.Key("STATIC_CACHE_TIME").MustDuration(6 * time.Hour) AppDataPath = sec.Key("APP_DATA_PATH").MustString(path.Join(AppWorkPath, "data")) EnableGzip = sec.Key("ENABLE_GZIP").MustBool() EnablePprof = sec.Key("ENABLE_PPROF").MustBool(false) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 8dfcdb9c9b..0db0af43f0 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -139,14 +139,14 @@ func NewMacaron() *macaron.Macaron { m.Use(public.Custom( &public.Options{ SkipLogging: setting.DisableRouterLog, - ExpiresAfter: time.Hour * 6, + ExpiresAfter: setting.StaticCacheTime, }, )) m.Use(public.Static( &public.Options{ Directory: path.Join(setting.StaticRootPath, "public"), SkipLogging: setting.DisableRouterLog, - ExpiresAfter: time.Hour * 6, + ExpiresAfter: setting.StaticCacheTime, }, )) m.Use(public.StaticHandler( @@ -154,7 +154,7 @@ func NewMacaron() *macaron.Macaron { &public.Options{ Prefix: "avatars", SkipLogging: setting.DisableRouterLog, - ExpiresAfter: time.Hour * 6, + ExpiresAfter: setting.StaticCacheTime, }, )) m.Use(public.StaticHandler( @@ -162,7 +162,7 @@ func NewMacaron() *macaron.Macaron { &public.Options{ Prefix: "repo-avatars", SkipLogging: setting.DisableRouterLog, - ExpiresAfter: time.Hour * 6, + ExpiresAfter: setting.StaticCacheTime, }, ))