Skip to content

Commit 0a951be

Browse files
committed
Fix server rebuilds on editing content with Chinese terms
Fixes #14240
1 parent 9e7182e commit 0a951be

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

commands/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/fsnotify/fsnotify"
5050
"github.com/gohugoio/hugo/common/herrors"
5151
"github.com/gohugoio/hugo/common/hugo"
52+
"github.com/gohugoio/hugo/common/paths"
5253
"github.com/gohugoio/hugo/langs"
5354
"github.com/gohugoio/hugo/tpl/tplimpl"
5455

@@ -370,6 +371,12 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string
370371

371372
if f.c.fastRenderMode && f.c.errState.buildErr() == nil {
372373
if isNavigation(requestURI, r) {
374+
// See issue 14240.
375+
// Hugo escapes the URL paths when generating them,
376+
// that may not be the case when we receive it back from the browser.
377+
// PathEscape will escape if it is not already escaped.
378+
requestURI = paths.PathEscape(requestURI)
379+
373380
if !f.c.visitedURLs.Contains(requestURI) {
374381
// If not already on stack, re-render that single page.
375382
if err := f.c.partialReRender(requestURI); err != nil {

common/paths/path_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,25 @@ func BenchmarkAddLeadingSlash(b *testing.B) {
347347
}
348348
})
349349
}
350+
351+
func TestPathEscape(t *testing.T) {
352+
c := qt.New(t)
353+
354+
for _, this := range []struct {
355+
input string
356+
expected string
357+
}{
358+
{"/tags/欢迎", "/tags/%E6%AC%A2%E8%BF%8E"},
359+
{"/path with spaces", "/path%20with%20spaces"},
360+
{"/simple-path", "/simple-path"},
361+
{"/path/with/slash", "/path/with/slash"},
362+
{"/path/with special&chars", "/path/with%20special&chars"},
363+
} {
364+
for range 2 {
365+
in := this.input
366+
result := PathEscape(in)
367+
c.Assert(result, qt.Equals, this.expected, qt.Commentf("input: %q", this.input))
368+
in = result // test idempotency
369+
}
370+
}
371+
}

hugolib/rebuild_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,29 @@ tags: ["tag1"]
20512051
b.AssertFileContent("public/tags/index.html", "All. Tag1|$")
20522052
}
20532053

2054+
func TestRebuildPageWithChineseTag14240(t *testing.T) {
2055+
t.Parallel()
2056+
2057+
files := `
2058+
-- hugo.toml --
2059+
baseURL = "https://example.com"
2060+
disableLiveReload = true
2061+
-- layouts/all.html --
2062+
All. {{ range .Pages }}{{ .RelPermalink }}: {{ .Title }}|{{ end }}$
2063+
-- content/p1.md --
2064+
---
2065+
title: "P1"
2066+
tags: ["欢迎"]
2067+
---
2068+
2069+
`
2070+
b := TestRunning(t, files)
2071+
2072+
b.AssertFileContent("public/tags/欢迎/index.html", "All. /p1/: P1|$")
2073+
b.EditFileReplaceAll("content/p1.md", "P1", "P1 edit").Build()
2074+
b.AssertFileContent("public/tags/欢迎/index.html", "All. /p1/: P1 edit|$")
2075+
}
2076+
20542077
func TestRebuildEditNonReferencedResourceIssue13748(t *testing.T) {
20552078
t.Parallel()
20562079

main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ var commonTestScriptsParam = testscript.Params{
248248
// httpget checks that a HTTP resource's body matches (if it compiles as a regexp) or contains all of the strings given as arguments.
249249
"httpget": func(ts *testscript.TestScript, neg bool, args []string) {
250250
if len(args) < 2 {
251-
ts.Fatalf("usage: httpgrep URL STRING...")
251+
ts.Fatalf("usage: httpget URL STRING...")
252252
}
253253

254254
tryget := func() error {
@@ -375,7 +375,7 @@ var commonTestScriptsParam = testscript.Params{
375375
// The server will write a .ready file when ready.
376376
// We wait for that.
377377
readyFilename := ts.MkAbs(".ready")
378-
limit := time.Now().Add(5 * time.Second)
378+
limit := time.Now().Add(10 * time.Second)
379379
for {
380380
_, err := os.Stat(readyFilename)
381381
if err != nil {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Issue 14240.
2+
3+
hugo server &
4+
5+
waitServer
6+
7+
httpget ${HUGOTEST_BASEURL_0}tags/%E6%AC%A2%E8%BF%8E/ '_P1_'
8+
9+
replace $WORK/content/p1.md 'P1' 'P1 New'
10+
11+
httpget ${HUGOTEST_BASEURL_0}tags/%E6%AC%A2%E8%BF%8E/ '_P1 New_'
12+
13+
stopServer
14+
! stderr .
15+
16+
-- hugo.toml --
17+
baseURL = "https://example.com"
18+
disableLiveReload = true
19+
-- layouts/all.html --
20+
All. {{ range .Pages }}_{{ .Title }}_{{ end }}$
21+
-- content/p1.md --
22+
---
23+
title: "P1"
24+
tags: ["欢迎"]
25+
---

0 commit comments

Comments
 (0)