fix(frontend): resolve Vite assets by manifest source path (#37836)

In dev mode `/api/swagger` returned HTTP 500 (`Failed to locate local
path for managed asset URI: css/swagger.css`): the backend synthesised
asset keys from the Vite entry name instead of reading the manifest,
which only worked by coincidence and broke once a source file name
diverged from its entry name.

This keys the manifest by its source path (e.g. `web_src/js/index.ts`)
and resolves entries directly — hashed `file` in prod, dev-server source
in dev. A new `AssetCSSLinks` helper renders a JS entry's stylesheet
`<link>` tags from the manifest (the entry's CSS plus the CSS of its
statically-imported chunks).

Fixes: https://github.com/go-gitea/gitea/issues/37830
Fixes: https://github.com/go-gitea/gitea/pull/37832
Fixes: https://github.com/go-gitea/gitea/pull/37876
Signed-off-by: silverwind <me@silverwind.io>
Co-authored-by: prakhar0x01 <prakharporwal2004@gmail.com>
Co-authored-by: Nicolas <bircni@icloud.com>
Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
silverwind
2026-05-28 06:14:52 +00:00
committed by GitHub
co-authored by prakhar0x01 Nicolas Claude Giteabot
parent db04bcb31a
commit 52fef74291
15 changed files with 122 additions and 128 deletions
+30 -34
View File
@@ -4,6 +4,7 @@
package public
import (
"html/template"
"testing"
"time"
@@ -22,59 +23,54 @@ func TestViteManifest(t *testing.T) {
"name": "index",
"src": "web_src/js/index.ts",
"isEntry": true,
"css": ["css/index.B3zrQPqD.css"]
"imports": ["_shared.AaAaAaAa.js"],
"css": ["css/index.B3zrQPqD.css", "css/index-extra.CcCcCcCc.css"]
},
"_shared.AaAaAaAa.js": {
"file": "js/shared.AaAaAaAa.js",
"name": "shared",
"css": ["css/shared.BbBbBbBb.css"]
},
"web_src/css/themes/theme-gitea-dark.css": {
"file": "css/theme-gitea-dark.CyAaQnn5.css",
"name": "theme-gitea-dark",
"src": "web_src/css/themes/theme-gitea-dark.css",
"isEntry": true
},
"web_src/js/features/eventsource.sharedworker.ts": {
"file": "js/eventsource.sharedworker.Dug1twio.js",
"name": "eventsource.sharedworker",
"src": "web_src/js/features/eventsource.sharedworker.ts",
"isEntry": true
},
"_chunk.js": {
"file": "js/chunk.abc123.js",
"name": "chunk"
}
}`
t.Run("EmptyManifest", func(t *testing.T) {
storeManifestFromBytes([]byte(``), 0, time.Now())
assert.Equal(t, "/assets/js/index.js", AssetURI("js/index.js"))
assert.Equal(t, "/assets/css/theme-gitea-dark.css", AssetURI("css/theme-gitea-dark.css"))
assert.Equal(t, "", AssetNameFromHashedPath("css/no-such-file.css"))
// not in manifest -> custom theme fallback
assert.Equal(t, "/assets/css/theme-gitea-dark.css", AssetURI("web_src/css/themes/theme-gitea-dark.css"))
assert.Empty(t, entryStyleURLs("web_src/js/index.ts", "web_src/css/index.css"))
assert.Empty(t, AssetNameFromHashedPath("css/no-such-file.css"))
})
t.Run("ParseManifest", func(t *testing.T) {
storeManifestFromBytes([]byte(testManifest), 0, time.Now())
paths, names := manifestData.Load().paths, manifestData.Load().names
// JS entries
assert.Equal(t, "js/index.C6Z2MRVQ.js", paths["js/index.js"])
assert.Equal(t, "js/eventsource.sharedworker.Dug1twio.js", paths["js/eventsource.sharedworker.js"])
// assets are addressed by their source path (the manifest key)
assert.Equal(t, "/assets/js/index.C6Z2MRVQ.js", AssetURI("web_src/js/index.ts"))
assert.Equal(t, "/assets/css/theme-gitea-dark.CyAaQnn5.css", AssetURI("web_src/css/themes/theme-gitea-dark.css"))
// Associated CSS from JS entries
assert.Equal(t, "css/index.B3zrQPqD.css", paths["css/index.css"])
// custom theme not in the manifest falls back to the static asset location
assert.Equal(t, "/assets/css/theme-custom.css", AssetURI("web_src/css/themes/theme-custom.css"))
// CSS-only entries
assert.Equal(t, "css/theme-gitea-dark.CyAaQnn5.css", paths["css/theme-gitea-dark.css"])
// a JS entry's stylesheets: all of the entry's own CSS plus the CSS of statically-imported chunks
assert.Equal(t, []string{
"/assets/css/index.B3zrQPqD.css",
"/assets/css/index-extra.CcCcCcCc.css",
"/assets/css/shared.BbBbBbBb.css",
}, entryStyleURLs("web_src/js/index.ts", "web_src/css/index.css"))
assert.Equal(t, template.HTML(
`<link rel="stylesheet" href="/assets/css/index.B3zrQPqD.css">`+
`<link rel="stylesheet" href="/assets/css/index-extra.CcCcCcCc.css">`+
`<link rel="stylesheet" href="/assets/css/shared.BbBbBbBb.css">`,
), AssetCSSLinks("web_src/js/index.ts", "web_src/css/index.css"))
// Non-entry chunks should not be included
assert.Empty(t, paths["js/chunk.js"])
// Names: hashed path -> entry name
assert.Equal(t, "index", names["js/index.C6Z2MRVQ.js"])
assert.Equal(t, "index", names["css/index.B3zrQPqD.css"])
assert.Equal(t, "theme-gitea-dark", names["css/theme-gitea-dark.CyAaQnn5.css"])
assert.Equal(t, "eventsource.sharedworker", names["js/eventsource.sharedworker.Dug1twio.js"])
// Test Asset related functions
assert.Equal(t, "/assets/js/index.C6Z2MRVQ.js", AssetURI("js/index.js"))
assert.Equal(t, "/assets/css/theme-gitea-dark.CyAaQnn5.css", AssetURI("css/theme-gitea-dark.css"))
// hashed output file -> entry name
assert.Equal(t, "theme-gitea-dark", AssetNameFromHashedPath("css/theme-gitea-dark.CyAaQnn5.css"))
assert.Empty(t, AssetNameFromHashedPath("css/no-such-file.css"))
})
}