Skip to content

Commit d02f062

Browse files
committed
livereload: Improve the livereload script build and update to v4.0.2
This script has very infrequent updates, but just copy pasting the minified source creates some potential trust issues. This JS will now be pulled from a Git version and both the unminified and minified version gets written to disk. This way it should be easier to reason about changes in the future. To upgrade, change the commit hash and run `mage generate`. Closes #12451 Closes #6290
1 parent 6dfeb9f commit d02f062

File tree

6 files changed

+3903
-47
lines changed

6 files changed

+3903
-47
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal
3+
navigation to another content page.
4+
*/
5+
function HugoReload() {}
6+
7+
HugoReload.identifier = 'hugoReloader';
8+
HugoReload.version = '0.9';
9+
10+
HugoReload.prototype.reload = function (path, options) {
11+
var prefix = '__hugo_navigate';
12+
13+
if (path.lastIndexOf(prefix, 0) !== 0) {
14+
return false;
15+
}
16+
17+
path = path.substring(prefix.length);
18+
19+
var portChanged = options.overrideURL && options.overrideURL != window.location.port;
20+
21+
if (!portChanged && window.location.pathname === path) {
22+
window.location.reload();
23+
} else {
24+
if (portChanged) {
25+
window.location = location.protocol + '//' + location.hostname + ':' + options.overrideURL + path;
26+
} else {
27+
window.location.pathname = path;
28+
}
29+
}
30+
31+
return true;
32+
};
33+
34+
LiveReload.addPlugin(HugoReload);

livereload/gen/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//go:generate go run main.go
2+
package main
3+
4+
import (
5+
_ "embed"
6+
"fmt"
7+
"io"
8+
"log"
9+
"net/http"
10+
"os"
11+
12+
"github.com/evanw/esbuild/pkg/api"
13+
)
14+
15+
//go:embed livereload-hugo-plugin.js
16+
var livereloadHugoPluginJS string
17+
18+
func main() {
19+
// 4.0.2
20+
// To upgrade to a new version, change to the commit hash of the version you want to upgrade to
21+
// then run mage generate from the root.
22+
const liveReloadCommit = "d803a41804d2d71e0814c4e9e3233e78991024d9"
23+
liveReloadSourceURL := fmt.Sprintf("https://raw.githubusercontent.com/livereload/livereload-js/%s/dist/livereload.js", liveReloadCommit)
24+
25+
func() {
26+
resp, err := http.Get(liveReloadSourceURL)
27+
must(err)
28+
defer resp.Body.Close()
29+
30+
b, err := io.ReadAll(resp.Body)
31+
must(err)
32+
33+
// Write the unminified livereload.js file.
34+
err = os.WriteFile("../livereload.js", b, 0o644)
35+
must(err)
36+
37+
// Bundle and minify with ESBuild.
38+
result := api.Build(api.BuildOptions{
39+
Stdin: &api.StdinOptions{
40+
Contents: string(b) + livereloadHugoPluginJS,
41+
},
42+
Outfile: "../livereload.min.js",
43+
Bundle: true,
44+
Target: api.ES2015,
45+
Write: true,
46+
MinifyWhitespace: true,
47+
MinifyIdentifiers: true,
48+
MinifySyntax: true,
49+
})
50+
51+
if len(result.Errors) > 0 {
52+
log.Fatal(result.Errors)
53+
}
54+
}()
55+
}
56+
57+
func must(err error) {
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
}

livereload/livereload.go

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Hugo Authors. All rights reserved.
1+
// Copyright 2024 The Hugo Authors. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@ import (
5050
)
5151

5252
// Prefix to signal to LiveReload that we need to navigate to another path.
53+
// Do not change this.
5354
const hugoNavigatePrefix = "__hugo_navigate"
5455

5556
var upgrader = &websocket.Upgrader{
@@ -144,48 +145,8 @@ func ServeJS(w http.ResponseWriter, r *http.Request) {
144145
}
145146

146147
func liveReloadJS() []byte {
147-
return []byte(livereloadJS + hugoLiveReloadPlugin)
148+
return []byte(livereloadJS)
148149
}
149150

150-
var (
151-
// This is a patched version, see https://github.com/livereload/livereload-js/pull/84
152-
//go:embed livereload.js
153-
livereloadJS string
154-
hugoLiveReloadPlugin = fmt.Sprintf(`
155-
/*
156-
Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal
157-
navigation to another content page.
158-
*/
159-
160-
function HugoReload() {}
161-
162-
HugoReload.identifier = 'hugoReloader';
163-
HugoReload.version = '0.9';
164-
165-
HugoReload.prototype.reload = function(path, options) {
166-
var prefix = %q;
167-
168-
if (path.lastIndexOf(prefix, 0) !== 0) {
169-
return false
170-
}
171-
172-
path = path.substring(prefix.length);
173-
174-
var portChanged = options.overrideURL && options.overrideURL != window.location.port
175-
176-
if (!portChanged && window.location.pathname === path) {
177-
window.location.reload();
178-
} else {
179-
if (portChanged) {
180-
window.location = location.protocol + "//" + location.hostname + ":" + options.overrideURL + path;
181-
} else {
182-
window.location.pathname = path;
183-
}
184-
}
185-
186-
return true;
187-
};
188-
189-
LiveReload.addPlugin(HugoReload)
190-
`, hugoNavigatePrefix)
191-
)
151+
//go:embed livereload.min.js
152+
var livereloadJS string

livereload/livereload.js

Lines changed: 3795 additions & 1 deletion
Large diffs are not rendered by default.

livereload/livereload.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

magefile.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ func flagEnv() map[string]string {
7979
// Generate autogen packages
8080
func Generate() error {
8181
generatorPackages := []string{
82-
//"tpl/tplimpl/embedded/generate",
83-
//"resources/page/generate",
82+
"livereload/gen",
8483
}
8584

8685
for _, pkg := range generatorPackages {

0 commit comments

Comments
 (0)