aboutsummaryrefslogtreecommitdiffhomepage
path: root/transform
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-10-29 18:03:36 +0100
committerBjørn Erik Pedersen <[email protected]>2023-10-29 18:37:05 +0100
commit8f60c0c1ec11c3c47ed7e3eff121ba1f1976f755 (patch)
tree6bf1f0f4c45b1cd33e73491792a91ce842bd16b6 /transform
parent9dc608084b73f04c146351cbe8d1b2f41058e8ba (diff)
downloadhugo-8f60c0c1ec11c3c47ed7e3eff121ba1f1976f755.tar.gz
hugo-8f60c0c1ec11c3c47ed7e3eff121ba1f1976f755.zip
livereloadinject: Save some allocations
As suggested by @DominoPivot ``` name old time/op new time/op delta LiveReloadInject-10 1.11µs ± 2% 1.04µs ± 2% -6.45% (p=0.029 n=4+4) name old alloc/op new alloc/op delta LiveReloadInject-10 1.23kB ± 0% 0.89kB ± 0% -27.69% (p=0.029 n=4+4) name old allocs/op new allocs/op delta LiveReloadInject-10 12.0 ± 0% 10.0 ± 0% -16.67% (p=0.029 n=4+4) ```
Diffstat (limited to 'transform')
-rw-r--r--transform/livereloadinject/livereloadinject.go22
1 files changed, 12 insertions, 10 deletions
diff --git a/transform/livereloadinject/livereloadinject.go b/transform/livereloadinject/livereloadinject.go
index 43c03348d..1e21a92e6 100644
--- a/transform/livereloadinject/livereloadinject.go
+++ b/transform/livereloadinject/livereloadinject.go
@@ -25,12 +25,14 @@ import (
"github.com/gohugoio/hugo/transform"
)
-var ignoredSyntax = regexp.MustCompile(`(?s)^(?:\s+|<!--.*?-->|<\?.*?\?>)*`)
-var tagsBeforeHead = []*regexp.Regexp{
- regexp.MustCompile(`(?is)^<!doctype\s[^>]*>`),
- regexp.MustCompile(`(?is)^<html(?:\s[^>]*)?>`),
- regexp.MustCompile(`(?is)^<head(?:\s[^>]*)?>`),
-}
+var (
+ ignoredSyntax = regexp.MustCompile(`(?s)^(?:\s+|<!--.*?-->|<\?.*?\?>)*`)
+ tagsBeforeHead = []*regexp.Regexp{
+ regexp.MustCompile(`(?is)^<!doctype\s[^>]*>`),
+ regexp.MustCompile(`(?is)^<html(?:\s[^>]*)?>`),
+ regexp.MustCompile(`(?is)^<head(?:\s[^>]*)?>`),
+ }
+)
// New creates a function that can be used to inject a script tag for
// the livereload JavaScript at the start of an HTML document's head.
@@ -54,12 +56,12 @@ func New(baseURL url.URL) transform.Transformer {
src += "&port=" + baseURL.Port()
src += "&path=" + strings.TrimPrefix(path+"/livereload", "/")
- c := make([]byte, len(b))
- copy(c, b)
-
script := []byte(fmt.Sprintf(`<script src="%s" data-no-instant defer></script>`, html.EscapeString(src)))
- c = append(c[:idx], append(script, c[idx:]...)...)
+ c := make([]byte, len(b)+len(script))
+ copy(c, b[:idx])
+ copy(c[idx:], script)
+ copy(c[idx+len(script):], b[idx:])
if _, err := ft.To().Write(c); err != nil {
loggers.Log().Warnf("Failed to inject LiveReload script:", err)