Skip to content

Commit

Permalink
livereload: Improve the livereload script build and update to v4.0.2
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed May 10, 2024
1 parent 6dfeb9f commit d02f062
Show file tree
Hide file tree
Showing 6 changed files with 3,903 additions and 47 deletions.
34 changes: 34 additions & 0 deletions livereload/gen/livereload-hugo-plugin.js
@@ -0,0 +1,34 @@
/*
Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal
navigation to another content page.
*/
function HugoReload() {}

HugoReload.identifier = 'hugoReloader';
HugoReload.version = '0.9';

HugoReload.prototype.reload = function (path, options) {
var prefix = '__hugo_navigate';

if (path.lastIndexOf(prefix, 0) !== 0) {
return false;
}

path = path.substring(prefix.length);

var portChanged = options.overrideURL && options.overrideURL != window.location.port;

if (!portChanged && window.location.pathname === path) {
window.location.reload();
} else {
if (portChanged) {
window.location = location.protocol + '//' + location.hostname + ':' + options.overrideURL + path;
} else {
window.location.pathname = path;
}
}

return true;
};

LiveReload.addPlugin(HugoReload);
61 changes: 61 additions & 0 deletions livereload/gen/main.go
@@ -0,0 +1,61 @@
//go:generate go run main.go
package main

import (
_ "embed"
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/evanw/esbuild/pkg/api"
)

//go:embed livereload-hugo-plugin.js
var livereloadHugoPluginJS string

func main() {
// 4.0.2
// To upgrade to a new version, change to the commit hash of the version you want to upgrade to
// then run mage generate from the root.
const liveReloadCommit = "d803a41804d2d71e0814c4e9e3233e78991024d9"
liveReloadSourceURL := fmt.Sprintf("https://raw.githubusercontent.com/livereload/livereload-js/%s/dist/livereload.js", liveReloadCommit)

func() {
resp, err := http.Get(liveReloadSourceURL)
must(err)
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
must(err)

// Write the unminified livereload.js file.
err = os.WriteFile("../livereload.js", b, 0o644)
must(err)

// Bundle and minify with ESBuild.
result := api.Build(api.BuildOptions{
Stdin: &api.StdinOptions{
Contents: string(b) + livereloadHugoPluginJS,
},
Outfile: "../livereload.min.js",
Bundle: true,
Target: api.ES2015,
Write: true,
MinifyWhitespace: true,
MinifyIdentifiers: true,
MinifySyntax: true,
})

if len(result.Errors) > 0 {
log.Fatal(result.Errors)
}
}()
}

func must(err error) {
if err != nil {
log.Fatal(err)
}
}
49 changes: 5 additions & 44 deletions livereload/livereload.go
@@ -1,4 +1,4 @@
// Copyright 2015 The Hugo Authors. All rights reserved.
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@ import (
)

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

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

func liveReloadJS() []byte {
return []byte(livereloadJS + hugoLiveReloadPlugin)
return []byte(livereloadJS)
}

var (
// This is a patched version, see https://github.com/livereload/livereload-js/pull/84
//go:embed livereload.js
livereloadJS string
hugoLiveReloadPlugin = fmt.Sprintf(`
/*
Hugo adds a specific prefix, "__hugo_navigate", to the path in certain situations to signal
navigation to another content page.
*/
function HugoReload() {}
HugoReload.identifier = 'hugoReloader';
HugoReload.version = '0.9';
HugoReload.prototype.reload = function(path, options) {
var prefix = %q;
if (path.lastIndexOf(prefix, 0) !== 0) {
return false
}
path = path.substring(prefix.length);
var portChanged = options.overrideURL && options.overrideURL != window.location.port
if (!portChanged && window.location.pathname === path) {
window.location.reload();
} else {
if (portChanged) {
window.location = location.protocol + "//" + location.hostname + ":" + options.overrideURL + path;
} else {
window.location.pathname = path;
}
}
return true;
};
LiveReload.addPlugin(HugoReload)
`, hugoNavigatePrefix)
)
//go:embed livereload.min.js
var livereloadJS string
3,796 changes: 3,795 additions & 1 deletion livereload/livereload.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions livereload/livereload.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions magefile.go
Expand Up @@ -79,8 +79,7 @@ func flagEnv() map[string]string {
// Generate autogen packages
func Generate() error {
generatorPackages := []string{
//"tpl/tplimpl/embedded/generate",
//"resources/page/generate",
"livereload/gen",
}

for _, pkg := range generatorPackages {
Expand Down

0 comments on commit d02f062

Please sign in to comment.