fix(favicon): correct icon cache key in FindIcon method

This commit is contained in:
yusing
2025-12-15 14:31:16 +08:00
parent da13b3b66d
commit 2d73dde9ff

View File

@@ -145,6 +145,11 @@ func fetchIcon(ctx context.Context, filename string) (FetchResult, error) {
return FetchResultWithErrorf(http.StatusNotFound, "no icon found") return FetchResultWithErrorf(http.StatusNotFound, "no icon found")
} }
type contextValue struct {
r httpRoute
uri string
}
func FindIcon(ctx context.Context, r route, uri string) (FetchResult, error) { func FindIcon(ctx context.Context, r route, uri string) (FetchResult, error) {
for _, ref := range r.References() { for _, ref := range r.References() {
result, err := fetchIcon(ctx, sanitizeName(ref)) result, err := fetchIcon(ctx, sanitizeName(ref))
@@ -154,14 +159,14 @@ func FindIcon(ctx context.Context, r route, uri string) (FetchResult, error) {
} }
if r, ok := r.(httpRoute); ok { if r, ok := r.(httpRoute); ok {
// fallback to parse html // fallback to parse html
return findIconSlowCached(context.WithValue(ctx, "route", r), uri) return findIconSlowCached(context.WithValue(ctx, "route", contextValue{r: r, uri: uri}), r.Key())
} }
return FetchResultWithErrorf(http.StatusNotFound, "no icon found") return FetchResultWithErrorf(http.StatusNotFound, "no icon found")
} }
var findIconSlowCached = cache.NewKeyFunc(func(ctx context.Context, key string) (FetchResult, error) { var findIconSlowCached = cache.NewKeyFunc(func(ctx context.Context, key string) (FetchResult, error) {
r := ctx.Value("route").(httpRoute) v := ctx.Value("route").(contextValue)
return findIconSlow(ctx, r, key, nil) return findIconSlow(ctx, v.r, v.uri, nil)
}).WithMaxEntries(200).Build() // no retries, no ttl }).WithMaxEntries(200).Build() // no retries, no ttl
func findIconSlow(ctx context.Context, r httpRoute, uri string, stack []string) (FetchResult, error) { func findIconSlow(ctx context.Context, r httpRoute, uri string, stack []string) (FetchResult, error) {