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 dynamic file names for assets #18632

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ FOMANTIC_WORK_DIR := web_src/fomantic

WEBPACK_SOURCES := $(shell find web_src/js web_src/less -type f)
WEBPACK_CONFIGS := webpack.config.js
WEBPACK_DEST := public/js/index.js public/css/index.css
WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/img/webpack public/serviceworker.js
WEBPACK_DEST := public/js
WEBPACK_DEST_ENTRIES := public/js public/css public/fonts public/img/webpack $(wildcard public/serviceworker.*.js) public/assets-manifest.json

BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST))
Expand Down
95 changes: 95 additions & 0 deletions modules/public/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

//go:build !bindata
// +build !bindata

package public

import (
"io"
"io/fs"
"net/http"
"sync"
"time"

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

var (
manifest map[string]string
manifestName = "assets-manifest.json"
manifestMutex = &sync.Mutex{}
manifestModified time.Time
)

func readManifestFile(fs http.FileSystem) (http.File, fs.FileInfo, error) {
f, err := fs.Open(manifestName)
if err != nil {
return nil, nil, err
}

fi, err := f.Stat()
if err != nil {
return nil, nil, err
}

return f, fi, nil
}

func readManifest(fs http.FileSystem) map[string]string {
manifestMutex.Lock()
var assetMap map[string]string

f, fi, err := readManifestFile(fs)
if err != nil {
log.Error("[Static] Failed to open %q: %v", manifestName, err)
return assetMap
}
defer f.Close()

bytes, err := io.ReadAll(f)
if err != nil {
log.Error("[Static] Failed to read %q: %v", manifestName, err)
return assetMap
}

err = json.Unmarshal(bytes, &assetMap)
if err != nil {
log.Error("[Static] Failed to parse %q: %v", manifestName, err)
return assetMap
}

manifestModified = fi.ModTime()
manifestMutex.Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All returns need Unlock

return assetMap
}

// ResolveWithManifest turns /js/index.js into /js/index.5ed90373e37c.js using assets-manifest.json
func ResolveWithManifest(fs http.FileSystem, file string) string {
if len(manifest) == 0 {
manifest = readManifest(fs)
}

// in development, the manifest can frequently change, check and reload if necessary
if !setting.IsProd {
f, fi, err := readManifestFile(fs)
if err != nil {
log.Error("[Static] Failed to open %q: %v", manifestName, err)
} else {
defer f.Close()
}

if fi.ModTime().After(manifestModified) {
manifest = readManifest(fs)
}
}

if mappedFile, ok := manifest[file]; ok {
return mappedFile
}
return file
}
8 changes: 8 additions & 0 deletions modules/public/manifest_bindata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

//go:build bindata
// +build bindata

package public
13 changes: 9 additions & 4 deletions modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
}

// custom files
if opts.handle(resp, req, http.Dir(custPath), file) {
if opts.handle(resp, req, http.Dir(custPath), file, false) {
return
}

// internal files
if opts.handle(resp, req, fileSystem(opts.Directory), file) {
if opts.handle(resp, req, fileSystem(opts.Directory), file, true) {
return
}

Expand All @@ -101,9 +101,14 @@ func setWellKnownContentType(w http.ResponseWriter, file string) {
}
}

func (opts *Options) handle(w http.ResponseWriter, req *http.Request, fs http.FileSystem, file string) bool {
func (opts *Options) handle(w http.ResponseWriter, req *http.Request, fs http.FileSystem, file string, isInternal bool) bool {
// respect webpack assets manifest for internal files
if isInternal {
file = ResolveWithManifest(fs, path.Clean(file))
}

// use clean to keep the file is a valid path with no . or ..
f, err := fs.Open(path.Clean(file))
f, err := fs.Open(file)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: should path.Clean in all cases.

if err != nil {
if os.IsNotExist(err) {
return false
Expand Down
1 change: 0 additions & 1 deletion modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ func NewFuncMap() []template.FuncMap {
"DiffTypeToStr": DiffTypeToStr,
"DiffLineTypeToStr": DiffLineTypeToStr,
"ShortSha": base.ShortSha,
"MD5": base.EncodeMD5,
"ActionContent2Commits": ActionContent2Commits,
"PathEscape": url.PathEscape,
"PathEscapeSegments": util.PathEscapeSegments,
Expand Down
Loading