Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate asset path for different build #18348

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type Options struct {
CorsHandler func(http.Handler) http.Handler
}

// AssetsURLPathPrefix is the path prefix for static asset files
const AssetsURLPathPrefix = "/assets/"
// WebPublicDirName is the directory name for public(static) asset files. It may be set to the build time by `static.go`
var WebPublicDirName = "public-dynamic"
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

// AssetsHandlerFunc implements the static handler for serving custom or original assets.
func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
Expand Down
11 changes: 9 additions & 2 deletions modules/public/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package public
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"mime"
"net/http"
Expand All @@ -18,12 +19,13 @@ import (
"time"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
)

var assetModTime time.Time

// GlobalModTime provide a global mod time for embedded asset files
func GlobalModTime(filename string) time.Time {
return timeutil.GetExecutableModTime()
return assetModTime
}

func fileSystem(dir string) http.FileSystem {
Expand Down Expand Up @@ -91,3 +93,8 @@ func serveContent(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modt
http.ServeContent(w, req, fi.Name(), modtime, content)
return
}

func init() {
assetModTime = timeutil.GetExecutableModTime()
WebPublicDirName = fmt.Sprintf("public-%x", assetModTime.UnixMilli())
}
4 changes: 2 additions & 2 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/public"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/svg"
Expand Down Expand Up @@ -62,7 +63,7 @@ func NewFuncMap() []template.FuncMap {
return setting.AppSubURL
},
"AssetUrlPrefix": func() string {
return setting.StaticURLPrefix + "/assets"
return setting.StaticURLPrefix + "/" + public.WebPublicDirName
},
"AppUrl": func() string {
return setting.AppURL
Expand Down Expand Up @@ -146,7 +147,6 @@ func NewFuncMap() []template.FuncMap {
"DiffLineTypeToStr": DiffLineTypeToStr,
"Sha1": Sha1,
"ShortSha": base.ShortSha,
"MD5": base.EncodeMD5,
"ActionContent2Commits": ActionContent2Commits,
"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
Expand Down
4 changes: 2 additions & 2 deletions routers/install/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func Routes() *web.Route {
r.Use(middle)
}

r.Use(web.WrapWithPrefix(public.AssetsURLPathPrefix, public.AssetsHandlerFunc(&public.Options{
r.Use(web.WrapWithPrefix("/"+public.WebPublicDirName, public.AssetsHandlerFunc(&public.Options{
Directory: path.Join(setting.StaticRootPath, "public"),
Prefix: public.AssetsURLPathPrefix,
Prefix: "/" + public.WebPublicDirName,
}), "InstallAssetsHandler"))

r.Use(session.Sessioner(session.Options{
Expand Down
4 changes: 2 additions & 2 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func CorsHandler() func(next http.Handler) http.Handler {
func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
routes := web.NewRoute()

routes.Use(web.WrapWithPrefix(public.AssetsURLPathPrefix, public.AssetsHandlerFunc(&public.Options{
routes.Use(web.WrapWithPrefix("/"+public.WebPublicDirName, public.AssetsHandlerFunc(&public.Options{
Directory: path.Join(setting.StaticRootPath, "public"),
Prefix: public.AssetsURLPathPrefix,
Prefix: "/" + public.WebPublicDirName,
CorsHandler: CorsHandler(),
}), "AssetsHandler"))

Expand Down
2 changes: 1 addition & 1 deletion templates/base/footer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<script src='https://hcaptcha.com/1/api.js' async></script>
{{end}}
{{end}}
<script src="{{AssetUrlPrefix}}/js/index.js?v={{MD5 AppVer}}"></script>
<script src="{{AssetUrlPrefix}}/js/index.js"></script>
{{template "custom/footer" .}}
</body>
</html>
6 changes: 3 additions & 3 deletions templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</script>
<link rel="icon" href="{{AssetUrlPrefix}}/img/logo.svg" type="image/svg+xml">
<link rel="alternate icon" href="{{AssetUrlPrefix}}/img/favicon.png" type="image/png">
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/index.css?v={{MD5 AppVer}}">
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/index.css">
<noscript>
<style>
.dropdown:hover > .menu { display: block; }
Expand Down Expand Up @@ -104,10 +104,10 @@
<meta property="og:site_name" content="{{AppName}}" />
{{if .IsSigned }}
{{ if ne .SignedUser.Theme "gitea" }}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/theme-{{.SignedUser.Theme | PathEscape}}.css?v={{MD5 AppVer}}">
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/theme-{{.SignedUser.Theme | PathEscape}}.css">
{{end}}
{{else if ne DefaultTheme "gitea"}}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/theme-{{DefaultTheme | PathEscape}}.css?v={{MD5 AppVer}}">
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/theme-{{DefaultTheme | PathEscape}}.css">
{{end}}
{{template "custom/header" .}}
</head>
Expand Down
4 changes: 2 additions & 2 deletions templates/swagger/ui.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<head>
<meta charset="UTF-8">
<title>Gitea API</title>
<link href="{{AssetUrlPrefix}}/css/swagger.css?v={{MD5 AppVer}}" rel="stylesheet">
<link href="{{AssetUrlPrefix}}/css/swagger.css" rel="stylesheet">
</head>
<body>
<a class="swagger-back-link" href="{{AppUrl}}">{{svg "octicon-reply"}}{{.i18n.Tr "return_to_gitea"}}</a>
<div id="swagger-ui" data-source="{{AppUrl}}swagger.{{.APIJSONVersion}}.json"></div>
<script src="{{AssetUrlPrefix}}/js/swagger.js?v={{MD5 AppVer}}"></script>
<script src="{{AssetUrlPrefix}}/js/swagger.js"></script>
</body>
</html>