aboutsummaryrefslogtreecommitdiffhomepage
path: root/transform
diff options
context:
space:
mode:
authorsatotake <[email protected]>2022-08-07 23:15:28 +0900
committerBjørn Erik Pedersen <[email protected]>2022-08-07 18:34:01 +0200
commitb017f7cb0136ee33f5d19749a15bf3365a991ac5 (patch)
tree622c40237802ce9426f0b0d49d4477d8fd111422 /transform
parent7fb28085aca42ff1771c5a9d0717530c9515fa5c (diff)
downloadhugo-b017f7cb0136ee33f5d19749a15bf3365a991ac5.tar.gz
hugo-b017f7cb0136ee33f5d19749a15bf3365a991ac5.zip
livereload: Inject script without head or body tag
Currently, Hugo does not inject `livereload` script if html does not contain `<head>` or `<body>`. This sometimes happens if you create new sites without `theme` and it is hard to catch the cause soon. This PR: * Inject livereload script even if html does not include `<head>`, `<body>`, or `<html>` - Modern browsers execute scripts even if they are outside `<html>` - Some js frameworks (confirmed with vite) inject HRM script without `<html>` tag * Append warning script to html if `<head>` or `<body>` is not in html * Fix bug that livereload cannot be appended to the tags with attrs Close #10105
Diffstat (limited to 'transform')
-rw-r--r--transform/livereloadinject/livereloadinject.go22
-rw-r--r--transform/livereloadinject/livereloadinject_test.go10
2 files changed, 26 insertions, 6 deletions
diff --git a/transform/livereloadinject/livereloadinject.go b/transform/livereloadinject/livereloadinject.go
index 32ed55f63..d57ba4d24 100644
--- a/transform/livereloadinject/livereloadinject.go
+++ b/transform/livereloadinject/livereloadinject.go
@@ -24,16 +24,24 @@ import (
"github.com/gohugoio/hugo/transform"
)
+const warnMessage = `"head" or "body" tag is required in html to append livereload script. ` +
+ "As a fallback, Hugo injects it somewhere but it might not work properly."
+
+var warnScript = fmt.Sprintf(`<script data-no-instant defer>console.warn('%s');</script>`, warnMessage)
+
type tag struct {
markup []byte
appendScript bool
+ warnRequired bool
}
var tags = []tag{
- {markup: []byte("<head>"), appendScript: true},
- {markup: []byte("<HEAD>"), appendScript: true},
+ {markup: []byte("<head"), appendScript: true},
+ {markup: []byte("<HEAD"), appendScript: true},
{markup: []byte("</body>")},
{markup: []byte("</BODY>")},
+ {markup: []byte("<html"), appendScript: true, warnRequired: true},
+ {markup: []byte("<HTML"), appendScript: true, warnRequired: true},
}
// New creates a function that can be used
@@ -64,15 +72,19 @@ func New(baseURL url.URL) transform.Transformer {
copy(c, b)
if idx == -1 {
- _, err := ft.To().Write(c)
- return err
+ idx = len(b)
+ match = tag{warnRequired: true}
}
script := []byte(fmt.Sprintf(`<script src="%s" data-no-instant defer></script>`, html.EscapeString(src)))
i := idx
if match.appendScript {
- i += len(match.markup)
+ i += bytes.Index(b[i:], []byte(">")) + 1
+ }
+
+ if match.warnRequired {
+ script = append(script, []byte(warnScript)...)
}
c = append(c[:i], append(script, c[i:]...)...)
diff --git a/transform/livereloadinject/livereloadinject_test.go b/transform/livereloadinject/livereloadinject_test.go
index b2ec4483a..d5cee79f8 100644
--- a/transform/livereloadinject/livereloadinject_test.go
+++ b/transform/livereloadinject/livereloadinject_test.go
@@ -58,7 +58,15 @@ func TestLiveReloadInject(t *testing.T) {
c.Assert(apply("foo</BODY>"), qt.Equals, "foo"+expectBase+"</BODY>")
})
+ c.Run("Html upper", func(c *qt.C) {
+ c.Assert(apply("<html>foo"), qt.Equals, "<html>"+expectBase+warnScript+"foo")
+ })
+
+ c.Run("Html upper with attr", func(c *qt.C) {
+ c.Assert(apply(`<html lang="en">foo`), qt.Equals, `<html lang="en">`+expectBase+warnScript+"foo")
+ })
+
c.Run("No match", func(c *qt.C) {
- c.Assert(apply("<h1>No match</h1>"), qt.Equals, "<h1>No match</h1>")
+ c.Assert(apply("<h1>No match</h1>"), qt.Equals, "<h1>No match</h1>"+expectBase+warnScript)
})
}