Skip to content

Commit 54a8f0c

Browse files
jmooringbep
authored andcommitted
resources: Use different cache key when copying resources
Closes #10412 Closes #12310
1 parent 38e05bd commit 54a8f0c

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

resources/resource_factories/create/create.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func New(rs *resources.Spec) *Client {
5757

5858
// Copy copies r to the new targetPath.
5959
func (c *Client) Copy(r resource.Resource, targetPath string) (resource.Resource, error) {
60-
key := dynacache.CleanKey(targetPath)
60+
key := dynacache.CleanKey(targetPath) + "__copy"
6161
return c.rs.ResourceCache.GetOrCreate(key, func() (resource.Resource, error) {
6262
return resources.Copy(r, targetPath), nil
6363
})
@@ -66,7 +66,7 @@ func (c *Client) Copy(r resource.Resource, targetPath string) (resource.Resource
6666
// Get creates a new Resource by opening the given pathname in the assets filesystem.
6767
func (c *Client) Get(pathname string) (resource.Resource, error) {
6868
pathname = path.Clean(pathname)
69-
key := dynacache.CleanKey(pathname)
69+
key := dynacache.CleanKey(pathname) + "__get"
7070

7171
return c.rs.ResourceCache.GetOrCreate(key, func() (resource.Resource, error) {
7272
// The resource file will not be read before it gets used (e.g. in .Content),

resources/resources_integration_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,50 @@ eventDate: 2023-11-01T07:00:00-08:00
227227

228228
b.AssertFileContent("public/index.html", "2023-11|p9|p8|p7|2023-10|p6|p5|p4|2023-09|p3|p2|p1|")
229229
}
230+
231+
// Issue 10412
232+
func TestImageTransformThenCopy(t *testing.T) {
233+
t.Parallel()
234+
235+
files := `
236+
-- hugo.toml --
237+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
238+
-- assets/pixel.png --
239+
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
240+
-- layouts/index.html --
241+
{{- with resources.Get "pixel.png" }}
242+
{{- with .Resize "200x" | resources.Copy "pixel.png" }}
243+
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">|{{ .Key }}
244+
{{- end }}
245+
{{- end }}
246+
`
247+
248+
b := hugolib.Test(t, files)
249+
250+
b.AssertFileExists("public/pixel.png", true)
251+
b.AssertFileContent("public/index.html",
252+
`<img src="/pixel.png" width="200" height="200">|/pixel.png`,
253+
)
254+
}
255+
256+
// Issue 12310
257+
func TestUseDifferentCacheKeyForResourceCopy(t *testing.T) {
258+
t.Parallel()
259+
260+
files := `
261+
-- hugo.toml --
262+
disableKinds = ['page','section','rss','sitemap','taxonomy','term']
263+
-- assets/a.txt --
264+
This was assets/a.txt
265+
-- layouts/index.html --
266+
{{ $nilResource := resources.Get "/p1/b.txt" }}
267+
{{ $r := resources.Get "a.txt" }}
268+
{{ $r = resources.Copy "/p1/b.txt" $r }}
269+
{{ $r.RelPermalink }}
270+
`
271+
272+
b, err := hugolib.TestE(t, files)
273+
274+
b.Assert(err, qt.IsNil)
275+
b.AssertFileContent("public/p1/b.txt", "This was assets/a.txt")
276+
}

0 commit comments

Comments
 (0)