Don't use the baseURL /path as part of the resource cache key

As that prevents Hugo projects with sub paths in their `baseURL` to use themes with cached resources.

Fixes #9787
This commit is contained in:
Bjørn Erik Pedersen 2022-05-27 13:23:37 +02:00
parent 46a2ea6d0d
commit dd9eaf19fd
3 changed files with 83 additions and 21 deletions

View file

@ -41,6 +41,7 @@ func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuild
} }
if conf.NeedsOsFS { if conf.NeedsOsFS {
if !filepath.IsAbs(conf.WorkingDir) {
tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test") tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
c.Assert(err, qt.IsNil) c.Assert(err, qt.IsNil)
conf.WorkingDir = filepath.Join(tempDir, conf.WorkingDir) conf.WorkingDir = filepath.Join(tempDir, conf.WorkingDir)
@ -49,6 +50,7 @@ func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuild
} else { } else {
fmt.Println("\nUsing WorkingDir dir:", conf.WorkingDir) fmt.Println("\nUsing WorkingDir dir:", conf.WorkingDir)
} }
}
} else if conf.WorkingDir == "" { } else if conf.WorkingDir == "" {
conf.WorkingDir = helpers.FilePathSeparator conf.WorkingDir = helpers.FilePathSeparator
} }

View file

@ -20,6 +20,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"github.com/gohugoio/hugo/resources/internal" "github.com/gohugoio/hugo/resources/internal"
@ -267,8 +268,11 @@ func (l *genericResource) Data() any {
} }
func (l *genericResource) Key() string { func (l *genericResource) Key() string {
if l.spec.BasePath == "" {
return l.RelPermalink() return l.RelPermalink()
} }
return strings.TrimPrefix(l.RelPermalink(), l.spec.BasePath)
}
func (l *genericResource) MediaType() media.Type { func (l *genericResource) MediaType() media.Type {
return l.mediaType return l.mediaType

View file

@ -23,6 +23,7 @@ import (
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib"
) )
@ -55,6 +56,9 @@ h1 {
-- config.toml -- -- config.toml --
disablekinds = ['taxonomy', 'term', 'page'] disablekinds = ['taxonomy', 'term', 'page']
baseURL = "https://example.com"
[build]
useResourceCacheWhen = 'never'
-- content/p1.md -- -- content/p1.md --
-- data/hugo.toml -- -- data/hugo.toml --
slogan = "Hugo Rocks!" slogan = "Hugo Rocks!"
@ -99,6 +103,22 @@ func TestTransformPostCSS(t *testing.T) {
} }
c := qt.New(t) c := qt.New(t)
tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
c.Assert(err, qt.IsNil)
c.Cleanup(clean)
for _, s := range []string{"never", "always"} {
repl := strings.NewReplacer(
"https://example.com",
"https://example.com/foo",
"useResourceCacheWhen = 'never'",
fmt.Sprintf("useResourceCacheWhen = '%s'", s),
)
files := repl.Replace(postCSSIntegrationTestFiles)
fmt.Println("===>", s, files)
b := hugolib.NewIntegrationTestBuilder( b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{ hugolib.IntegrationTestConfig{
@ -106,20 +126,19 @@ func TestTransformPostCSS(t *testing.T) {
NeedsOsFS: true, NeedsOsFS: true,
NeedsNpmInstall: true, NeedsNpmInstall: true,
LogLevel: jww.LevelInfo, LogLevel: jww.LevelInfo,
TxtarString: postCSSIntegrationTestFiles, WorkingDir: tempDir,
TxtarString: files,
}).Build() }).Build()
b.AssertLogContains("Hugo Environment: production")
b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", b.Cfg.WorkingDir)))
b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", b.Cfg.WorkingDir)))
b.AssertFileContent("public/index.html", ` b.AssertFileContent("public/index.html", `
Styles RelPermalink: /css/styles.css Styles RelPermalink: /foo/css/styles.css
Styles Content: Len: 770917| Styles Content: Len: 770917|
`) `)
} }
}
// 9880 // 9880
func TestTransformPostCSSError(t *testing.T) { func TestTransformPostCSSError(t *testing.T) {
if !htesting.IsCI() { if !htesting.IsCI() {
@ -186,3 +205,40 @@ func TestTransformPostCSSImporSkipInlineImportsNotFound(t *testing.T) {
s.AssertFileContent("public/css/styles.css", `@import "components/doesnotexist.css";`) s.AssertFileContent("public/css/styles.css", `@import "components/doesnotexist.css";`)
} }
// Issue 9787
func TestTransformPostCSSResourceCacheWithPathInBaseURL(t *testing.T) {
if !htesting.IsCI() {
t.Skip("Skip long running test when running locally")
}
c := qt.New(t)
tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test")
c.Assert(err, qt.IsNil)
c.Cleanup(clean)
for i := 0; i < 2; i++ {
files := postCSSIntegrationTestFiles
if i == 1 {
files = strings.ReplaceAll(files, "https://example.com", "https://example.com/foo")
files = strings.ReplaceAll(files, "useResourceCacheWhen = 'never'", " useResourceCacheWhen = 'always'")
}
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: c,
NeedsOsFS: true,
NeedsNpmInstall: true,
LogLevel: jww.LevelInfo,
TxtarString: files,
WorkingDir: tempDir,
}).Build()
b.AssertFileContent("public/index.html", `
Styles Content: Len: 770917
`)
}
}