Skip to content

Commit b4fce47

Browse files
authored
Merge pull request Wei-Shaw#1116 from wucm667/fix/inject-site-title-in-html
fix: 直接访问或刷新页面时浏览器标签页显示自定义站点名称
2 parents e7780cd + 6028efd commit b4fce47

3 files changed

Lines changed: 107 additions & 1 deletion

File tree

backend/internal/web/embed_on.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,37 @@ func (s *FrontendServer) injectSettings(settingsJSON []byte) []byte {
180180

181181
// Inject before </head>
182182
headClose := []byte("</head>")
183-
return bytes.Replace(s.baseHTML, headClose, append(script, headClose...), 1)
183+
result := bytes.Replace(s.baseHTML, headClose, append(script, headClose...), 1)
184+
185+
// Replace <title> with custom site name so the browser tab shows it immediately
186+
result = injectSiteTitle(result, settingsJSON)
187+
188+
return result
189+
}
190+
191+
// injectSiteTitle replaces the static <title> in HTML with the configured site name.
192+
// This ensures the browser tab shows the correct title before JS executes.
193+
func injectSiteTitle(html, settingsJSON []byte) []byte {
194+
var cfg struct {
195+
SiteName string `json:"site_name"`
196+
}
197+
if err := json.Unmarshal(settingsJSON, &cfg); err != nil || cfg.SiteName == "" {
198+
return html
199+
}
200+
201+
// Find and replace the existing <title>...</title>
202+
titleStart := bytes.Index(html, []byte("<title>"))
203+
titleEnd := bytes.Index(html, []byte("</title>"))
204+
if titleStart == -1 || titleEnd == -1 || titleEnd <= titleStart {
205+
return html
206+
}
207+
208+
newTitle := []byte("<title>" + cfg.SiteName + " - AI API Gateway</title>")
209+
var buf bytes.Buffer
210+
buf.Write(html[:titleStart])
211+
buf.Write(newTitle)
212+
buf.Write(html[titleEnd+len("</title>"):])
213+
return buf.Bytes()
184214
}
185215

186216
// replaceNoncePlaceholder replaces the nonce placeholder with actual nonce value

backend/internal/web/embed_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,78 @@ func init() {
2020
gin.SetMode(gin.TestMode)
2121
}
2222

23+
func TestInjectSiteTitle(t *testing.T) {
24+
t.Run("replaces_title_with_site_name", func(t *testing.T) {
25+
html := []byte(`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`)
26+
settingsJSON := []byte(`{"site_name":"MyCustomSite"}`)
27+
28+
result := injectSiteTitle(html, settingsJSON)
29+
30+
assert.Contains(t, string(result), "<title>MyCustomSite - AI API Gateway</title>")
31+
assert.NotContains(t, string(result), "Sub2API")
32+
})
33+
34+
t.Run("returns_unchanged_when_site_name_empty", func(t *testing.T) {
35+
html := []byte(`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`)
36+
settingsJSON := []byte(`{"site_name":""}`)
37+
38+
result := injectSiteTitle(html, settingsJSON)
39+
40+
assert.Equal(t, string(html), string(result))
41+
})
42+
43+
t.Run("returns_unchanged_when_site_name_missing", func(t *testing.T) {
44+
html := []byte(`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`)
45+
settingsJSON := []byte(`{"other_field":"value"}`)
46+
47+
result := injectSiteTitle(html, settingsJSON)
48+
49+
assert.Equal(t, string(html), string(result))
50+
})
51+
52+
t.Run("returns_unchanged_when_invalid_json", func(t *testing.T) {
53+
html := []byte(`<html><head><title>Sub2API - AI API Gateway</title></head><body></body></html>`)
54+
settingsJSON := []byte(`{invalid json}`)
55+
56+
result := injectSiteTitle(html, settingsJSON)
57+
58+
assert.Equal(t, string(html), string(result))
59+
})
60+
61+
t.Run("returns_unchanged_when_no_title_tag", func(t *testing.T) {
62+
html := []byte(`<html><head></head><body></body></html>`)
63+
settingsJSON := []byte(`{"site_name":"MyCustomSite"}`)
64+
65+
result := injectSiteTitle(html, settingsJSON)
66+
67+
assert.Equal(t, string(html), string(result))
68+
})
69+
70+
t.Run("returns_unchanged_when_title_has_attributes", func(t *testing.T) {
71+
// The function looks for "<title>" literally, so attributes are not supported
72+
// This is acceptable since index.html uses plain <title> without attributes
73+
html := []byte(`<html><head><title lang="en">Sub2API</title></head><body></body></html>`)
74+
settingsJSON := []byte(`{"site_name":"NewSite"}`)
75+
76+
result := injectSiteTitle(html, settingsJSON)
77+
78+
// Should return unchanged since <title> with attributes is not matched
79+
assert.Equal(t, string(html), string(result))
80+
})
81+
82+
t.Run("preserves_rest_of_html", func(t *testing.T) {
83+
html := []byte(`<html><head><meta charset="UTF-8"><title>Sub2API</title><script src="app.js"></script></head><body><div id="app"></div></body></html>`)
84+
settingsJSON := []byte(`{"site_name":"TestSite"}`)
85+
86+
result := injectSiteTitle(html, settingsJSON)
87+
88+
assert.Contains(t, string(result), `<meta charset="UTF-8">`)
89+
assert.Contains(t, string(result), `<script src="app.js"></script>`)
90+
assert.Contains(t, string(result), `<div id="app"></div>`)
91+
assert.Contains(t, string(result), "<title>TestSite - AI API Gateway</title>")
92+
})
93+
}
94+
2395
func TestReplaceNoncePlaceholder(t *testing.T) {
2496
t.Run("replaces_single_placeholder", func(t *testing.T) {
2597
html := []byte(`<script nonce="__CSP_NONCE_VALUE__">console.log('test');</script>`)

frontend/src/App.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RouterView, useRouter, useRoute } from 'vue-router'
33
import { onMounted, onBeforeUnmount, watch } from 'vue'
44
import Toast from '@/components/common/Toast.vue'
55
import NavigationProgress from '@/components/common/NavigationProgress.vue'
6+
import { resolveDocumentTitle } from '@/router/title'
67
import AnnouncementPopup from '@/components/common/AnnouncementPopup.vue'
78
import { useAppStore, useAuthStore, useSubscriptionStore, useAnnouncementStore } from '@/stores'
89
import { getSetupStatus } from '@/api/setup'
@@ -104,6 +105,9 @@ onMounted(async () => {
104105
105106
// Load public settings into appStore (will be cached for other components)
106107
await appStore.fetchPublicSettings()
108+
109+
// Re-resolve document title now that siteName is available
110+
document.title = resolveDocumentTitle(route.meta.title, appStore.siteName, route.meta.titleKey as string)
107111
})
108112
</script>
109113

0 commit comments

Comments
 (0)