2015-12-07 18:57:01 +00:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-05-16 21:49:27 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-16 21:49:27 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-16 21:49:27 +00:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-12-22 23:21:03 +00:00
|
|
|
//
|
|
|
|
// Contains an embedded version of livereload.js
|
|
|
|
//
|
|
|
|
// Copyright (c) 2010-2015 Andrey Tarantsov
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2014-05-16 21:49:27 +00:00
|
|
|
|
|
|
|
package livereload
|
|
|
|
|
|
|
|
import (
|
2017-06-26 19:34:16 +00:00
|
|
|
"fmt"
|
2017-11-12 09:03:56 +00:00
|
|
|
"net"
|
2014-05-16 21:49:27 +00:00
|
|
|
"net/http"
|
2017-11-12 09:03:56 +00:00
|
|
|
"net/url"
|
2016-12-28 03:14:43 +00:00
|
|
|
"path/filepath"
|
2014-05-16 21:49:27 +00:00
|
|
|
|
2022-08-01 09:42:26 +00:00
|
|
|
_ "embed"
|
|
|
|
|
2022-10-26 10:54:50 +00:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2014-05-16 21:49:27 +00:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2017-06-26 19:34:16 +00:00
|
|
|
// Prefix to signal to LiveReload that we need to navigate to another path.
|
|
|
|
const hugoNavigatePrefix = "__hugo_navigate"
|
|
|
|
|
2017-11-12 09:03:56 +00:00
|
|
|
var upgrader = &websocket.Upgrader{
|
|
|
|
// Hugo may potentially spin up multiple HTTP servers, so we need to exclude the
|
|
|
|
// port when checking the origin.
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
origin := r.Header["Origin"]
|
|
|
|
if len(origin) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
u, err := url.Parse(origin[0])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-05 06:26:16 +00:00
|
|
|
rHost := r.Host
|
|
|
|
// For Github codespace in browser #9936
|
|
|
|
if forwardedHost := r.Header.Get("X-Forwarded-Host"); forwardedHost != "" {
|
|
|
|
rHost = forwardedHost
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Host == rHost {
|
2017-12-16 15:41:52 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-12 09:03:56 +00:00
|
|
|
h1, _, err := net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h2, _, err := net.SplitHostPort(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return h1 == h2
|
|
|
|
},
|
2020-12-02 12:23:25 +00:00
|
|
|
ReadBufferSize: 1024, WriteBufferSize: 1024,
|
|
|
|
}
|
2014-05-16 21:49:27 +00:00
|
|
|
|
2016-03-24 13:05:08 +00:00
|
|
|
// Handler is a HandlerFunc handling the livereload
|
|
|
|
// Websocket interaction.
|
2014-05-16 21:49:27 +00:00
|
|
|
func Handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c := &connection{send: make(chan []byte, 256), ws: ws}
|
|
|
|
wsHub.register <- c
|
|
|
|
defer func() { wsHub.unregister <- c }()
|
|
|
|
go c.writer()
|
|
|
|
c.reader()
|
|
|
|
}
|
|
|
|
|
2016-03-24 13:05:08 +00:00
|
|
|
// Initialize starts the Websocket Hub handling live reloads.
|
2014-05-16 21:49:27 +00:00
|
|
|
func Initialize() {
|
|
|
|
go wsHub.run()
|
|
|
|
}
|
|
|
|
|
2016-03-24 13:05:08 +00:00
|
|
|
// ForceRefresh tells livereload to force a hard refresh.
|
2014-05-16 21:49:27 +00:00
|
|
|
func ForceRefresh() {
|
2014-09-10 10:21:22 +00:00
|
|
|
RefreshPath("/x.js")
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:34:16 +00:00
|
|
|
// NavigateToPath tells livereload to navigate to the given path.
|
|
|
|
// This translates to `window.location.href = path` in the client.
|
|
|
|
func NavigateToPath(path string) {
|
|
|
|
RefreshPath(hugoNavigatePrefix + path)
|
|
|
|
}
|
|
|
|
|
2017-11-12 09:03:56 +00:00
|
|
|
// NavigateToPathForPort is similar to NavigateToPath but will also
|
|
|
|
// set window.location.port to the given port value.
|
|
|
|
func NavigateToPathForPort(path string, port int) {
|
|
|
|
refreshPathForPort(hugoNavigatePrefix+path, port)
|
|
|
|
}
|
|
|
|
|
2016-03-24 13:05:08 +00:00
|
|
|
// RefreshPath tells livereload to refresh only the given path.
|
|
|
|
// If that path points to a CSS stylesheet or an image, only the changes
|
|
|
|
// will be updated in the browser, not the entire page.
|
2014-09-10 10:21:22 +00:00
|
|
|
func RefreshPath(s string) {
|
2017-11-12 09:03:56 +00:00
|
|
|
refreshPathForPort(s, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshPathForPort(s string, port int) {
|
2014-09-10 10:21:22 +00:00
|
|
|
// Tell livereload a file has changed - will force a hard refresh if not CSS or an image
|
2016-12-28 03:14:43 +00:00
|
|
|
urlPath := filepath.ToSlash(s)
|
2017-11-12 09:03:56 +00:00
|
|
|
portStr := ""
|
|
|
|
if port > 0 {
|
|
|
|
portStr = fmt.Sprintf(`, "overrideURL": %d`, port)
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf(`{"command":"reload","path":%q,"originalPath":"","liveCSS":true,"liveImg":true%s}`, urlPath, portStr)
|
|
|
|
wsHub.broadcast <- []byte(msg)
|
2014-05-16 21:49:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 17:11:12 +00:00
|
|
|
// ServeJS serves the livereload.js who's reference is injected into the page.
|
2014-05-16 21:49:27 +00:00
|
|
|
func ServeJS(w http.ResponseWriter, r *http.Request) {
|
2023-01-04 17:24:36 +00:00
|
|
|
w.Header().Set("Content-Type", media.Builtin.JavascriptType.Type)
|
2017-06-26 19:34:16 +00:00
|
|
|
w.Write(liveReloadJS())
|
2014-05-16 21:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 19:34:16 +00:00
|
|
|
func liveReloadJS() []byte {
|
|
|
|
return []byte(livereloadJS + hugoLiveReloadPlugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-07-22 11:22:04 +00:00
|
|
|
// This is a patched version, see https://github.com/livereload/livereload-js/pull/84
|
2022-08-01 09:42:26 +00:00
|
|
|
//go:embed livereload.js
|
2022-07-26 11:51:08 +00:00
|
|
|
livereloadJS string
|
2017-06-26 19:34:16 +00:00
|
|
|
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
|
|
|
|
}
|
2017-11-12 09:03:56 +00:00
|
|
|
|
2017-06-26 19:34:16 +00:00
|
|
|
path = path.substring(prefix.length);
|
2017-11-18 09:54:07 +00:00
|
|
|
|
|
|
|
var portChanged = options.overrideURL && options.overrideURL != window.location.port
|
2017-11-12 09:03:56 +00:00
|
|
|
|
2017-11-18 09:54:07 +00:00
|
|
|
if (!portChanged && window.location.pathname === path) {
|
2017-08-22 15:29:09 +00:00
|
|
|
window.location.reload();
|
|
|
|
} else {
|
2017-11-18 09:54:07 +00:00
|
|
|
if (portChanged) {
|
2017-11-12 09:03:56 +00:00
|
|
|
window.location = location.protocol + "//" + location.hostname + ":" + options.overrideURL + path;
|
|
|
|
} else {
|
|
|
|
window.location.pathname = path;
|
|
|
|
}
|
2017-08-22 15:29:09 +00:00
|
|
|
}
|
2017-06-26 19:34:16 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
LiveReload.addPlugin(HugoReload)
|
|
|
|
`, hugoNavigatePrefix)
|
|
|
|
)
|