mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-01 05:12:13 +00:00
Replace webpack with Vite 8 as the frontend bundler. Frontend build is around 3-4 times faster than before. Will work on all platforms including riscv64 (via wasm). `iife.js` is a classic render-blocking script in `<head>` (handles web components/early DOM setup). `index.js` is loaded as a `type="module"` script in the footer. All other JS chunks are also module scripts (supported in all browsers since 2018). Entry filenames are content-hashed (e.g. `index.C6Z2MRVQ.js`) and resolved at runtime via the Vite manifest, eliminating the `?v=` cache busting (which was unreliable in some scenarios like vscode dev build). Replaces: https://github.com/go-gitea/gitea/pull/36896 Fixes: https://github.com/go-gitea/gitea/issues/17793 Signed-off-by: silverwind <me@silverwind.io> Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Claude (Opus 4.6) <noreply@anthropic.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
169 lines
5.8 KiB
Go
169 lines
5.8 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package public
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/web/routing"
|
|
)
|
|
|
|
const viteDevPortFile = "public/assets/.vite/dev-port"
|
|
|
|
var viteDevProxy atomic.Pointer[httputil.ReverseProxy]
|
|
|
|
func getViteDevProxy() *httputil.ReverseProxy {
|
|
if proxy := viteDevProxy.Load(); proxy != nil {
|
|
return proxy
|
|
}
|
|
|
|
portFile := filepath.Join(setting.StaticRootPath, viteDevPortFile)
|
|
data, err := os.ReadFile(portFile)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
port := strings.TrimSpace(string(data))
|
|
if port == "" {
|
|
return nil
|
|
}
|
|
|
|
target, err := url.Parse("http://localhost:" + port)
|
|
if err != nil {
|
|
log.Error("Failed to parse Vite dev server URL: %v", err)
|
|
return nil
|
|
}
|
|
|
|
// there is a strange error log (from Golang's HTTP package)
|
|
// 2026/03/28 19:50:13 modules/log/misc.go:72:(*loggerToWriter).Write() [I] Unsolicited response received on idle HTTP channel starting with "HTTP/1.1 400 Bad Request\r\n\r\n"; err=<nil>
|
|
// maybe it is caused by that the Vite dev server doesn't support keep-alive connections? or different keep-alive timeouts?
|
|
transport := &http.Transport{
|
|
IdleConnTimeout: 5 * time.Second,
|
|
ResponseHeaderTimeout: 5 * time.Second,
|
|
}
|
|
log.Info("Proxying Vite dev server requests to %s", target)
|
|
proxy := &httputil.ReverseProxy{
|
|
Transport: transport,
|
|
Rewrite: func(r *httputil.ProxyRequest) {
|
|
r.SetURL(target)
|
|
r.Out.Host = target.Host
|
|
},
|
|
ModifyResponse: func(resp *http.Response) error {
|
|
// add a header to indicate the Vite dev server port,
|
|
// make developers know that this request is proxied to Vite dev server and which port it is
|
|
resp.Header.Add("X-Gitea-Vite-Port", port)
|
|
return nil
|
|
},
|
|
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
log.Error("Error proxying to Vite dev server: %v", err)
|
|
http.Error(w, "Error proxying to Vite dev server: "+err.Error(), http.StatusBadGateway)
|
|
},
|
|
}
|
|
viteDevProxy.Store(proxy)
|
|
return proxy
|
|
}
|
|
|
|
// ViteDevMiddleware proxies matching requests to the Vite dev server.
|
|
// It is registered as middleware in non-production mode and lazily discovers
|
|
// the Vite dev server port from the port file written by the viteDevServerPortPlugin.
|
|
// It is needed because there are container-based development, only Gitea web server's port is exposed.
|
|
func ViteDevMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
if !isViteDevRequest(req) {
|
|
next.ServeHTTP(resp, req)
|
|
return
|
|
}
|
|
proxy := getViteDevProxy()
|
|
if proxy == nil {
|
|
next.ServeHTTP(resp, req)
|
|
return
|
|
}
|
|
routing.MarkLongPolling(resp, req)
|
|
proxy.ServeHTTP(resp, req)
|
|
})
|
|
}
|
|
|
|
// isViteDevMode returns true if the Vite dev server port file exists.
|
|
// In production mode, the result is cached after the first check.
|
|
func isViteDevMode() bool {
|
|
if setting.IsProd {
|
|
return false
|
|
}
|
|
portFile := filepath.Join(setting.StaticRootPath, viteDevPortFile)
|
|
_, err := os.Stat(portFile)
|
|
return err == nil
|
|
}
|
|
|
|
func viteDevSourceURL(name string) string {
|
|
if !isViteDevMode() {
|
|
return ""
|
|
}
|
|
if strings.HasPrefix(name, "css/theme-") {
|
|
// Only redirect built-in themes to Vite source; custom themes are served from custom/public/assets/css/
|
|
themeFile := strings.TrimPrefix(name, "css/")
|
|
srcPath := filepath.Join(setting.StaticRootPath, "web_src/css/themes", themeFile)
|
|
if _, err := os.Stat(srcPath); err == nil {
|
|
return setting.AppSubURL + "/web_src/css/themes/" + themeFile
|
|
}
|
|
return ""
|
|
}
|
|
if strings.HasPrefix(name, "css/") {
|
|
return setting.AppSubURL + "/web_src/" + name
|
|
}
|
|
if name == "js/eventsource.sharedworker.js" {
|
|
return setting.AppSubURL + "/web_src/js/features/eventsource.sharedworker.ts"
|
|
}
|
|
if name == "js/iife.js" {
|
|
return setting.AppSubURL + "/web_src/js/__vite_iife.js"
|
|
}
|
|
if name == "js/index.js" {
|
|
return setting.AppSubURL + "/web_src/js/index.ts"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// isViteDevRequest returns true if the request should be proxied to the Vite dev server.
|
|
// Ref: Vite source packages/vite/src/node/constants.ts and packages/vite/src/shared/constants.ts
|
|
func isViteDevRequest(req *http.Request) bool {
|
|
if req.Header.Get("Upgrade") == "websocket" {
|
|
wsProtocol := req.Header.Get("Sec-WebSocket-Protocol")
|
|
return wsProtocol == "vite-hmr" || wsProtocol == "vite-ping"
|
|
}
|
|
path := req.URL.Path
|
|
|
|
// vite internal requests
|
|
if strings.HasPrefix(path, "/@vite/") /* HMR client */ ||
|
|
strings.HasPrefix(path, "/@fs/") /* out-of-root file access, see vite.config.ts: fs.allow */ ||
|
|
strings.HasPrefix(path, "/@id/") /* virtual modules */ {
|
|
return true
|
|
}
|
|
|
|
// local source requests (VITE-DEV-SERVER-SECURITY: don't serve sensitive files outside the allowed paths)
|
|
if strings.HasPrefix(path, "/node_modules/") ||
|
|
strings.HasPrefix(path, "/public/assets/") ||
|
|
strings.HasPrefix(path, "/web_src/") {
|
|
return true
|
|
}
|
|
|
|
// Vite uses a path relative to project root and adds "?import" to non-JS/CSS asset imports:
|
|
// - {WebSite}/public/assets/... (e.g. SVG icons from "{RepoRoot}/public/assets/img/svg/")
|
|
// - {WebSite}/assets/emoji.json: it is an exception for the frontend assets, it is imported by JS code, but:
|
|
// - KEEP IN MIND: all static frontend assets are served from "{AssetFS}/assets" to "{WebSite}/assets" by Gitea Web Server
|
|
// - "{AssetFS}" is a layered filesystem from "{RepoRoot}/public" or embedded assets, and user's custom files in "{CustomPath}/public"
|
|
// - "{RepoRoot}/assets/emoji.json" just happens to have the dir name "assets", it is not related to frontend assets
|
|
// - BAD DESIGN: indeed it is a "conflicted and polluted name" sample
|
|
if path == "/assets/emoji.json" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|