]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources: Use different cache key when copying resources
authorJoe Mooring <joe.mooring@veriphor.com>
Mon, 25 Mar 2024 21:17:57 +0000 (14:17 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 27 Mar 2024 08:59:59 +0000 (09:59 +0100)
Closes #10412
Closes #12310

resources/resource_factories/create/create.go
resources/resources_integration_test.go

index 061ecf1e7f036a3a950fb09a16ed36a95594848f..4725cf390b3d9a15dfd5010e3783cede524ba978 100644 (file)
@@ -57,7 +57,7 @@ func New(rs *resources.Spec) *Client {
 
 // Copy copies r to the new targetPath.
 func (c *Client) Copy(r resource.Resource, targetPath string) (resource.Resource, error) {
-       key := dynacache.CleanKey(targetPath)
+       key := dynacache.CleanKey(targetPath) + "__copy"
        return c.rs.ResourceCache.GetOrCreate(key, func() (resource.Resource, error) {
                return resources.Copy(r, targetPath), nil
        })
@@ -66,7 +66,7 @@ func (c *Client) Copy(r resource.Resource, targetPath string) (resource.Resource
 // Get creates a new Resource by opening the given pathname in the assets filesystem.
 func (c *Client) Get(pathname string) (resource.Resource, error) {
        pathname = path.Clean(pathname)
-       key := dynacache.CleanKey(pathname)
+       key := dynacache.CleanKey(pathname) + "__get"
 
        return c.rs.ResourceCache.GetOrCreate(key, func() (resource.Resource, error) {
                // The resource file will not be read before it gets used (e.g. in .Content),
index a0b35196a66696d531614c216925ecf395aeb4f8..ba580b4dce1a9a265382903d491987e8ae06264b 100644 (file)
@@ -227,3 +227,50 @@ eventDate: 2023-11-01T07:00:00-08:00
 
        b.AssertFileContent("public/index.html", "2023-11|p9|p8|p7|2023-10|p6|p5|p4|2023-09|p3|p2|p1|")
 }
+
+// Issue 10412
+func TestImageTransformThenCopy(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ['page','rss','section','sitemap','taxonomy','term']
+-- assets/pixel.png --
+iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
+-- layouts/index.html --
+{{- with resources.Get "pixel.png" }}
+  {{- with .Resize "200x" | resources.Copy "pixel.png" }}
+    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">|{{ .Key }}
+  {{- end }}
+{{- end }}
+`
+
+       b := hugolib.Test(t, files)
+
+       b.AssertFileExists("public/pixel.png", true)
+       b.AssertFileContent("public/index.html",
+               `<img src="/pixel.png" width="200" height="200">|/pixel.png`,
+       )
+}
+
+// Issue 12310
+func TestUseDifferentCacheKeyForResourceCopy(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ['page','section','rss','sitemap','taxonomy','term']
+-- assets/a.txt --
+This was assets/a.txt
+-- layouts/index.html --
+{{ $nilResource := resources.Get "/p1/b.txt" }}
+{{ $r := resources.Get "a.txt" }}
+{{ $r = resources.Copy "/p1/b.txt" $r }}
+{{ $r.RelPermalink }}
+`
+
+       b, err := hugolib.TestE(t, files)
+
+       b.Assert(err, qt.IsNil)
+       b.AssertFileContent("public/p1/b.txt", "This was assets/a.txt")
+}