[frontend-gray] support grayKey from localStorage (#1395)

This commit is contained in:
mamba
2024-10-18 13:58:52 +08:00
committed by GitHub
parent c67f494b49
commit 11ff2d1d31
6 changed files with 115 additions and 43 deletions

View File

@@ -142,8 +142,22 @@ func IsPageRequest(fetchMode string, myPath string) bool {
// 首页Rewrite
func IndexRewrite(path, version string, matchRules map[string]string) string {
for prefix, rewrite := range matchRules {
// Create a slice of keys in matchRules and sort them by length in descending order
keys := make([]string, 0, len(matchRules))
for prefix := range matchRules {
keys = append(keys, prefix)
}
sort.Slice(keys, func(i, j int) bool {
if len(keys[i]) != len(keys[j]) {
return len(keys[i]) > len(keys[j]) // Sort by length
}
return keys[i] < keys[j] // Sort lexicographically
})
// Iterate over sorted keys to find the longest match
for _, prefix := range keys {
if strings.HasPrefix(path, prefix) {
rewrite := matchRules[prefix]
newPath := strings.Replace(rewrite, "{version}", version, -1)
return newPath
}