]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
transform/livereloadinject: Skip livereload.js injection if no tags found (note)
authorAndrii Chubatiuk <andrew.chubatiuk@gmail.com>
Sat, 13 Sep 2025 16:36:39 +0000 (19:36 +0300)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 2 Oct 2025 11:39:35 +0000 (13:39 +0200)
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>
testscripts/commands/server.txt
transform/livereloadinject/livereloadinject.go
transform/livereloadinject/livereloadinject_test.go

index 7f6afd8fd7bfaebaf3c1864767f2c79b0d336cb1..5b2a2544d1150d73050dcb92c54e6aaba5c1b5b7 100644 (file)
@@ -24,6 +24,7 @@ myenv = "theproduction"
 -- config/development/params.toml --
 myenv = "thedevelopment"
 -- layouts/index.html --
+<!DOCTYPE html>
 <body>
 Title: {{ .Title }}|BaseURL: {{ site.BaseURL }}|ServerPort: {{ site.ServerPort }}|myenv: {{ .Site.Params.myenv }}|Env: {{ hugo.Environment }}|IsServer: {{ hugo.IsServer }}|
 </body>
index 425d268b34afeda9553769873b8f2e84675cc060..55e69f9128217befae6edbf3094d9c2f3217f87a 100644 (file)
@@ -49,6 +49,15 @@ func New(baseURL *url.URL) transform.Transformer {
                        idx += len(ignoredSyntax.Find(b[idx:]))
                        idx += len(tag.Find(b[idx:]))
                }
+               if idx == 0 {
+                       // doctype is required for HTML5, we did not find it,
+                       // and neither did we find html or head tags, so
+                       // skip injection.
+                       // This allows us to render partial HTML documents to be used in
+                       // e.g. JS frameworks.
+                       ft.To().Write(b)
+                       return nil
+               }
 
                path := strings.TrimSuffix(baseURL.Path, "/")
 
index d406b9c4d3dadafa50ce713825caae0ae67ea750..5475fc5b6e97e21245cb9bc46edd20f831a713b6 100644 (file)
@@ -59,12 +59,12 @@ func TestLiveReloadInject(t *testing.T) {
                c.Assert(apply("<!doctype html>after"), qt.Equals, "<!doctype html>"+expectBase+"after")
        })
 
-       c.Run("Inject before other elements if all else omitted", func(c *qt.C) {
-               c.Assert(apply("<title>after</title>"), qt.Equals, expectBase+"<title>after</title>")
+       c.Run("Inject nothing if no doctype, html or head found", func(c *qt.C) {
+               c.Assert(apply("<title>after</title>"), qt.Equals, "<title>after</title>")
        })
 
-       c.Run("Inject before text content if all else omitted", func(c *qt.C) {
-               c.Assert(apply("after"), qt.Equals, expectBase+"after")
+       c.Run("Inject nothing if no tag found", func(c *qt.C) {
+               c.Assert(apply("after"), qt.Equals, "after")
        })
 
        c.Run("Inject after HeAd tag MiXed CaSe", func(c *qt.C) {