Skip to content

Commit

Permalink
Fix resource content multihost
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 18, 2024
1 parent bcee8de commit d77b110
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 21 deletions.
16 changes: 16 additions & 0 deletions hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,15 +1381,31 @@ func (sa *sitePagesAssembler) assembleResources() error {
}
relPath := rs.path.BaseRel(ps.m.pathInfo)
baseTarget := targetPaths.SubResourceBaseTarget
var targetBasePaths []string
if ps.s.Conf.IsMultihost() {
// In multihost we need to publish to the lang sub folder,
// and also to all languages that does not have this bundled resource.
targetBasePaths = []string{ps.Lang()}
// TODO(bep) improve.
var sources resourceSources
if v, ok := sa.pageMap.treeResources.GetRaw(resourceKey); ok {
sources = v.(resourceSources)
for i, s := range sources {
if s == nil {
targetBasePaths = append(targetBasePaths, ps.s.h.Sites[i].Lang())
}
}
}
baseTarget = targetPaths.SubResourceBaseLink

}

rd := resources.ResourceSourceDescriptor{
OpenReadSeekCloser: rs.opener,
Path: rs.path,
GroupIdentity: rs.path,
TargetPath: relPath,
TargetBasePaths: targetBasePaths,
BasePathRelPermalink: targetPaths.SubResourceBaseLink,
BasePathTargetPath: baseTarget,
Name: relPath,
Expand Down
2 changes: 0 additions & 2 deletions hugolib/filesystems/basefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,6 @@ func TestStaticFsMultiHost(t *testing.T) {
conf := testconfig.GetTestConfig(afs, v)
fs := hugofs.NewFrom(afs, conf.BaseConfig())

fmt.Println("IS", conf.IsMultihost())

p, err := paths.New(fs, conf)
c.Assert(err, qt.IsNil)
bfs, err := filesystems.NewBase(p, nil)
Expand Down
2 changes: 2 additions & 0 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,15 @@ func (p *pageState) initPage() error {

func (p *pageState) renderResources() error {
var initErr error

p.resourcesPublishInit.Do(func() {
for _, r := range p.Resources() {
if _, ok := r.(page.Page); ok {
// Pages gets rendered with the owning page but we count them here.
p.s.PathSpec.ProcessingStats.Incr(&p.s.PathSpec.ProcessingStats.Pages)
continue
}

src, ok := r.(resource.Source)
if !ok {
initErr = fmt.Errorf("resource %T does not support resource.Source", src)
Expand Down
45 changes: 28 additions & 17 deletions hugolib/pagebundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,44 +525,55 @@ Bundled page: {{ .RelPermalink}}|Len resources: {{ len .Resources }}|
b.AssertFileExists("public/nn/section/mybundle/index.html", false)
}

func TestPageBundlerResourcePerLanguage(t *testing.T) {
func TestPageBundlerResourcePerLanguageMultihostMinify(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = "https://example.com"
disableKinds = ["taxonomy", "term"]
defaultContentLanguage = "en"
defaultContentLanguageInSubDir = true
[languages]
[languages.nn]
weight = 2
[languages.en]
baseURL = "https://example.en"
weight = 1
-- content/section/mybundle/index.md --
contentDir = "content/en"
[languages.fr]
baseURL = "https://example.fr"
weight = 2
contentDir = "content/fr"
-- content/en/section/mybundle/index.md --
---
title: "Mybundle en"
---
-- content/section/mybundle/index.nn.md --
-- content/fr/section/mybundle/index.md --
---
title: "Mybundle nn"
title: "Mybundle fr"
---
-- content/section/mybundle/data.txt --
Data en
-- content/section/mybundle/data.nn.txt --
Data nn
-- content/en/section/mybundle/styles.css --
.body {
color: english;
}
-- content/fr/section/mybundle/styles.css --
.body {
color: french;
}
-- layouts/_default/single.html --
{{ $data := .Resources.GetMatch "data*" }}
{{ .Lang }}: {{ $data.Content}}|
{{ $data := .Resources.GetMatch "styles*" | minify }}
{{ .Lang }}: {{ $data.Content}}|{{ $data.RelPermalink }}|
`
b := Test(t, files)

b.AssertFileContent("public/en/section/mybundle/index.html",
"en: Data en|",
b.AssertFileContent("public/fr/section/mybundle/index.html",
"fr: .body{color:french}|/section/mybundle/styles.min.css|",
)
b.AssertFileContent("public/nn/section/mybundle/index.html",
"nn: Data nn|",

b.AssertFileContent("public/en/section/mybundle/index.html",
"en: .body{color:english}|/section/mybundle/styles.min.css|",
)

b.AssertFileContent("public/en/section/mybundle/styles.min.css", ".body{color:english}")
b.AssertFileContent("public/fr/section/mybundle/styles.min.css", ".body{color:french}")
}

// #6208
Expand Down
12 changes: 10 additions & 2 deletions resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,18 @@ func (l *genericResource) Data() any {

func (l *genericResource) Key() string {
basePath := l.spec.Cfg.BaseURL().BasePathNoTrailingSlash
var key string
if basePath == "" {
return l.RelPermalink()
key = l.RelPermalink()
} else {
key = strings.TrimPrefix(l.RelPermalink(), basePath)
}

if l.spec.Cfg.IsMultihost() {
key = l.spec.Lang() + key
}
return strings.TrimPrefix(l.RelPermalink(), basePath)

return key
}

func (l *genericResource) MediaType() media.Type {
Expand Down

0 comments on commit d77b110

Please sign in to comment.