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
This commit is contained in:
Bjørn Erik Pedersen
2024-05-02 10:35:43 +02:00
parent 6dfeb9f038
commit d02f0622b4
6 changed files with 3903 additions and 47 deletions

View File

@@ -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.
@@ -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{
@@ -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