Skip to content

Commit 7fd6762

Browse files
AndrewChubatiukbep
andcommitted
transform/livereloadinject: Skip livereload.js injection if no tags found (note)
This change is mainly motivated to support sites built by HTML fragments with e.g. a JS framework. Now we don't inject the script if we don't find any of `doctype` (the only one required by the HTML 5 spec), `html` or `head`. Co-authored-by: bep <bjorn.erik.pedersen@gmail.com>
1 parent 584f052 commit 7fd6762

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

‎testscripts/commands/server.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ myenv = "theproduction"
2424
-- config/development/params.toml --
2525
myenv = "thedevelopment"
2626
-- layouts/index.html --
27+
<!DOCTYPE html>
2728
<body>
2829
Title: {{ .Title }}|BaseURL: {{ site.BaseURL }}|ServerPort: {{ site.ServerPort }}|myenv: {{ .Site.Params.myenv }}|Env: {{ hugo.Environment }}|IsServer: {{ hugo.IsServer }}|
2930
</body>

‎transform/livereloadinject/livereloadinject.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ func New(baseURL *url.URL) transform.Transformer {
4949
idx += len(ignoredSyntax.Find(b[idx:]))
5050
idx += len(tag.Find(b[idx:]))
5151
}
52+
if idx == 0 {
53+
// doctype is required for HTML5, we did not find it,
54+
// and neither did we find html or head tags, so
55+
// skip injection.
56+
// This allows us to render partial HTML documents to be used in
57+
// e.g. JS frameworks.
58+
ft.To().Write(b)
59+
return nil
60+
}
5261

5362
path := strings.TrimSuffix(baseURL.Path, "/")
5463

‎transform/livereloadinject/livereloadinject_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func TestLiveReloadInject(t *testing.T) {
5959
c.Assert(apply("<!doctype html>after"), qt.Equals, "<!doctype html>"+expectBase+"after")
6060
})
6161

62-
c.Run("Inject before other elements if all else omitted", func(c *qt.C) {
63-
c.Assert(apply("<title>after</title>"), qt.Equals, expectBase+"<title>after</title>")
62+
c.Run("Inject nothing if no doctype, html or head found", func(c *qt.C) {
63+
c.Assert(apply("<title>after</title>"), qt.Equals, "<title>after</title>")
6464
})
6565

66-
c.Run("Inject before text content if all else omitted", func(c *qt.C) {
67-
c.Assert(apply("after"), qt.Equals, expectBase+"after")
66+
c.Run("Inject nothing if no tag found", func(c *qt.C) {
67+
c.Assert(apply("after"), qt.Equals, "after")
6868
})
6969

7070
c.Run("Inject after HeAd tag MiXed CaSe", func(c *qt.C) {

0 commit comments

Comments
 (0)