shithub: hugo

Download patch

ref: 786f72302f65580ca8d1df2132a7756584539ea0
parent: 0cae1cf8286b128295057d076c479c929f0febb3
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Jul 29 10:26:45 EDT 2018

Fix image cache eviction for sites with subdir in baseURL

Fixes #5006

--- a/hugolib/page_collections.go
+++ b/hugolib/page_collections.go
@@ -322,7 +322,7 @@
 		first := page.Resources[0]
 		dir := path.Dir(first.RelPermalink())
 		dir = strings.TrimPrefix(dir, page.LanguagePrefix())
-		// This is done to keep the memory usage in check when doing live reloads.
+		dir = strings.TrimPrefix(dir, page.s.BaseURL.Path())
 		page.s.ResourceSpec.DeleteCacheByPrefix(dir)
 	}
 }
--- a/resource/image_cache.go
+++ b/resource/image_cache.go
@@ -33,7 +33,7 @@
 
 func (c *imageCache) isInCache(key string) bool {
 	c.mu.RLock()
-	_, found := c.store[key]
+	_, found := c.store[c.normalizeKey(key)]
 	c.mu.RUnlock()
 	return found
 }
@@ -41,11 +41,22 @@
 func (c *imageCache) deleteByPrefix(prefix string) {
 	c.mu.Lock()
 	defer c.mu.Unlock()
+	prefix = c.normalizeKey(prefix)
 	for k := range c.store {
 		if strings.HasPrefix(k, prefix) {
 			delete(c.store, k)
 		}
 	}
+}
+
+func (c *imageCache) normalizeKey(key string) string {
+	// It is a path with Unix style slashes and it always starts with a leading slash.
+	key = filepath.ToSlash(key)
+	if !strings.HasPrefix(key, "/") {
+		key = "/" + key
+	}
+
+	return key
 }
 
 func (c *imageCache) clear() {