2024-05-02 04:35:43 -04:00
|
|
|
// Copyright 2024 The Hugo Authors. All rights reserved.
|
2014-05-16 17:49:27 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-16 17:49:27 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-16 17:49:27 -04: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 18:21:03 -05: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 17:49:27 -04:00
|
|
|
|
|
|
|
package livereload
|
|
|
|
|
|
|
|
import (
|
2017-06-26 15:34:16 -04:00
|
|
|
"fmt"
|
2017-11-12 04:03:56 -05:00
|
|
|
"net"
|
2014-05-16 17:49:27 -04:00
|
|
|
"net/http"
|
2017-11-12 04:03:56 -05:00
|
|
|
"net/url"
|
2016-12-27 22:14:43 -05:00
|
|
|
"path/filepath"
|
2014-05-16 17:49:27 -04:00
|
|
|
|
2022-08-01 05:42:26 -04:00
|
|
|
_ "embed"
|
|
|
|
|
2022-10-26 06:54:50 -04:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2014-05-16 17:49:27 -04:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
2017-06-26 15:34:16 -04:00
|
|
|
// Prefix to signal to LiveReload that we need to navigate to another path.
|
2024-05-02 04:35:43 -04:00
|
|
|
// Do not change this.
|
2017-06-26 15:34:16 -04:00
|
|
|
const hugoNavigatePrefix = "__hugo_navigate"
|
|
|
|
|
2017-11-12 04:03:56 -05: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 02:26:16 -04: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 10:41:52 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-12 04:03:56 -05: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 07:23:25 -05:00
|
|
|
ReadBufferSize: 1024, WriteBufferSize: 1024,
|
|
|
|
}
|
2014-05-16 17:49:27 -04:00
|
|
|
|
2016-03-24 09:05:08 -04:00
|
|
|
// Handler is a HandlerFunc handling the livereload
|
|
|
|
// Websocket interaction.
|
2014-05-16 17:49:27 -04: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 09:05:08 -04:00
|
|
|
// Initialize starts the Websocket Hub handling live reloads.
|
2014-05-16 17:49:27 -04:00
|
|
|
func Initialize() {
|
|
|
|
go wsHub.run()
|
|
|
|
}
|
|
|
|
|
2016-03-24 09:05:08 -04:00
|
|
|
// ForceRefresh tells livereload to force a hard refresh.
|
2014-05-16 17:49:27 -04:00
|
|
|
func ForceRefresh() {
|
2014-09-10 06:21:22 -04:00
|
|
|
RefreshPath("/x.js")
|
|
|
|
}
|
|
|
|
|
2017-11-12 04:03:56 -05: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 09:05:08 -04: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 06:21:22 -04:00
|
|
|
func RefreshPath(s string) {
|
2017-11-12 04:03:56 -05:00
|
|
|
refreshPathForPort(s, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshPathForPort(s string, port int) {
|
2014-09-10 06:21:22 -04:00
|
|
|
// Tell livereload a file has changed - will force a hard refresh if not CSS or an image
|
2016-12-27 22:14:43 -05:00
|
|
|
urlPath := filepath.ToSlash(s)
|
2017-11-12 04:03:56 -05: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 17:49:27 -04:00
|
|
|
}
|
|
|
|
|
2023-05-22 13:11:12 -04:00
|
|
|
// ServeJS serves the livereload.js who's reference is injected into the page.
|
2014-05-16 17:49:27 -04:00
|
|
|
func ServeJS(w http.ResponseWriter, r *http.Request) {
|
2023-01-04 12:24:36 -05:00
|
|
|
w.Header().Set("Content-Type", media.Builtin.JavascriptType.Type)
|
2017-06-26 15:34:16 -04:00
|
|
|
w.Write(liveReloadJS())
|
2014-05-16 17:49:27 -04:00
|
|
|
}
|
|
|
|
|
2017-06-26 15:34:16 -04:00
|
|
|
func liveReloadJS() []byte {
|
2024-05-02 04:35:43 -04:00
|
|
|
return []byte(livereloadJS)
|
2017-06-26 15:34:16 -04:00
|
|
|
}
|
|
|
|
|
2024-05-02 04:35:43 -04:00
|
|
|
//go:embed livereload.min.js
|
|
|
|
var livereloadJS string
|