From 0ad378b09cea90a2a70d7ff06af668abe22475a1 Mon Sep 17 00:00:00 2001 From: sth Date: Wed, 2 Dec 2020 12:52:26 +0100 Subject: [PATCH] Use --baseURL path for live-reload URL Fixes #6595 --- commands/server.go | 13 ++++++++++--- commands/server_errors.go | 5 +++-- hugolib/site.go | 5 ++++- publisher/publisher.go | 9 +++++---- transform/livereloadinject/livereloadinject.go | 14 ++++++++++++-- .../livereloadinject/livereloadinject_test.go | 10 ++++++++-- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/commands/server.go b/commands/server.go index b6a8d282c..ef504f20f 100644 --- a/commands/server.go +++ b/commands/server.go @@ -357,7 +357,9 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro if !f.c.paused { port = f.c.Cfg.GetInt("liveReloadPort") } - fmt.Fprint(w, injectLiveReloadScript(r, port)) + lr := *u + lr.Host = fmt.Sprintf("%s:%d", lr.Hostname(), port) + fmt.Fprint(w, injectLiveReloadScript(r, lr)) return } @@ -501,8 +503,13 @@ func (c *commandeer) serve(s *serverCmd) error { mu, serverURL, endpoint, err := srv.createEndpoint(i) if doLiveReload { - mu.HandleFunc("/livereload.js", livereload.ServeJS) - mu.HandleFunc("/livereload", livereload.Handler) + u, err := url.Parse(helpers.SanitizeURL(baseURLs[i])) + if err != nil { + return err + } + + mu.HandleFunc(u.Path+"/livereload.js", livereload.ServeJS) + mu.HandleFunc(u.Path+"/livereload", livereload.Handler) } jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", serverURL, s.serverInterface) go func() { diff --git a/commands/server_errors.go b/commands/server_errors.go index 7f467ee1c..0e3bc50de 100644 --- a/commands/server_errors.go +++ b/commands/server_errors.go @@ -16,6 +16,7 @@ package commands import ( "bytes" "io" + "net/url" "github.com/gohugoio/hugo/transform" "github.com/gohugoio/hugo/transform/livereloadinject" @@ -82,9 +83,9 @@ var buildErrorTemplate = ` ` -func injectLiveReloadScript(src io.Reader, port int) string { +func injectLiveReloadScript(src io.Reader, baseURL url.URL) string { var b bytes.Buffer - chain := transform.Chain{livereloadinject.New(port)} + chain := transform.Chain{livereloadinject.New(baseURL)} chain.Apply(&b, src) return b.String() diff --git a/hugolib/site.go b/hugolib/site.go index c89995ab0..05dd2307a 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1716,7 +1716,10 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath s } if s.running() && s.Cfg.GetBool("watch") && !s.Cfg.GetBool("disableLiveReload") { - pd.LiveReloadPort = s.Cfg.GetInt("liveReloadPort") + pd.LiveReloadBaseURL = s.PathSpec.BaseURL.URL() + if s.Cfg.GetInt("liveReloadPort") != -1 { + pd.LiveReloadBaseURL.Host = fmt.Sprintf("%s:%d", pd.LiveReloadBaseURL.Hostname(), s.Cfg.GetInt("liveReloadPort")) + } } // For performance reasons we only inject the Hugo generator tag on the home page. diff --git a/publisher/publisher.go b/publisher/publisher.go index 8b8d2fa63..ac2151529 100644 --- a/publisher/publisher.go +++ b/publisher/publisher.go @@ -16,6 +16,7 @@ package publisher import ( "errors" "io" + "net/url" "sync/atomic" "github.com/gohugoio/hugo/resources" @@ -51,8 +52,8 @@ type Descriptor struct { StatCounter *uint64 // Configuration that trigger pre-processing. - // LiveReload script will be injected if this is > 0 - LiveReloadPort int + // LiveReload script will be injected if this is != nil + LiveReloadBaseURL *url.URL // Enable to inject the Hugo generated tag in the header. Is currently only // injected on the home page for HTML type of output formats. @@ -166,8 +167,8 @@ func (p DestinationPublisher) createTransformerChain(f Descriptor) transform.Cha } if isHTML { - if f.LiveReloadPort > 0 { - transformers = append(transformers, livereloadinject.New(f.LiveReloadPort)) + if f.LiveReloadBaseURL != nil { + transformers = append(transformers, livereloadinject.New(*f.LiveReloadBaseURL)) } // This is only injected on the home page. diff --git a/transform/livereloadinject/livereloadinject.go b/transform/livereloadinject/livereloadinject.go index a6e56ac41..cc80d046e 100644 --- a/transform/livereloadinject/livereloadinject.go +++ b/transform/livereloadinject/livereloadinject.go @@ -16,6 +16,9 @@ package livereloadinject import ( "bytes" "fmt" + "html" + "net/url" + "strings" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/transform" @@ -35,7 +38,8 @@ var tags = []tag{ // New creates a function that can be used // to inject a script tag for the livereload JavaScript in a HTML document. -func New(port int) transform.Transformer { +func New(baseURL url.URL) transform.Transformer { + return func(ft transform.FromTo) error { b := ft.From().Bytes() var idx = -1 @@ -51,6 +55,12 @@ func New(port int) transform.Transformer { } } + path := strings.TrimSuffix(baseURL.Path, "/") + + src := path + "/livereload.js?mindelay=10&v=2" + src += "&port=" + baseURL.Port() + src += "&path=" + strings.TrimPrefix(path+"/livereload", "/") + c := make([]byte, len(b)) copy(c, b) @@ -59,7 +69,7 @@ func New(port int) transform.Transformer { return err } - script := []byte(fmt.Sprintf(``, port)) + script := []byte(fmt.Sprintf(``, html.EscapeString(src))) i := idx if match.appendScript { diff --git a/transform/livereloadinject/livereloadinject_test.go b/transform/livereloadinject/livereloadinject_test.go index 59124f20b..50fbb8b92 100644 --- a/transform/livereloadinject/livereloadinject_test.go +++ b/transform/livereloadinject/livereloadinject_test.go @@ -15,6 +15,7 @@ package livereloadinject import ( "bytes" + "net/url" "strings" "testing" @@ -25,12 +26,17 @@ import ( func TestLiveReloadInject(t *testing.T) { c := qt.New(t) - expectBase := `` + lrurl, err := url.Parse("http://localhost:1234/subpath") + if err != nil { + t.Errorf("Parsing test URL failed") + return + } + expectBase := `` apply := func(s string) string { out := new(bytes.Buffer) in := strings.NewReader(s) - tr := transform.New(New(1313)) + tr := transform.New(New(*lrurl)) tr.Apply(out, in) return out.String()