Use --baseURL path for live-reload URL

Fixes #6595
This commit is contained in:
sth 2020-12-02 12:52:26 +01:00 committed by GitHub
parent aebfe156fb
commit 0ad378b09c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 14 deletions

View file

@ -357,7 +357,9 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
if !f.c.paused { if !f.c.paused {
port = f.c.Cfg.GetInt("liveReloadPort") 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 return
} }
@ -501,8 +503,13 @@ func (c *commandeer) serve(s *serverCmd) error {
mu, serverURL, endpoint, err := srv.createEndpoint(i) mu, serverURL, endpoint, err := srv.createEndpoint(i)
if doLiveReload { if doLiveReload {
mu.HandleFunc("/livereload.js", livereload.ServeJS) u, err := url.Parse(helpers.SanitizeURL(baseURLs[i]))
mu.HandleFunc("/livereload", livereload.Handler) 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) jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", serverURL, s.serverInterface)
go func() { go func() {

View file

@ -16,6 +16,7 @@ package commands
import ( import (
"bytes" "bytes"
"io" "io"
"net/url"
"github.com/gohugoio/hugo/transform" "github.com/gohugoio/hugo/transform"
"github.com/gohugoio/hugo/transform/livereloadinject" "github.com/gohugoio/hugo/transform/livereloadinject"
@ -82,9 +83,9 @@ var buildErrorTemplate = `<!doctype html>
</html> </html>
` `
func injectLiveReloadScript(src io.Reader, port int) string { func injectLiveReloadScript(src io.Reader, baseURL url.URL) string {
var b bytes.Buffer var b bytes.Buffer
chain := transform.Chain{livereloadinject.New(port)} chain := transform.Chain{livereloadinject.New(baseURL)}
chain.Apply(&b, src) chain.Apply(&b, src)
return b.String() return b.String()

View file

@ -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") { 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. // For performance reasons we only inject the Hugo generator tag on the home page.

View file

@ -16,6 +16,7 @@ package publisher
import ( import (
"errors" "errors"
"io" "io"
"net/url"
"sync/atomic" "sync/atomic"
"github.com/gohugoio/hugo/resources" "github.com/gohugoio/hugo/resources"
@ -51,8 +52,8 @@ type Descriptor struct {
StatCounter *uint64 StatCounter *uint64
// Configuration that trigger pre-processing. // Configuration that trigger pre-processing.
// LiveReload script will be injected if this is > 0 // LiveReload script will be injected if this is != nil
LiveReloadPort int LiveReloadBaseURL *url.URL
// Enable to inject the Hugo generated tag in the header. Is currently only // Enable to inject the Hugo generated tag in the header. Is currently only
// injected on the home page for HTML type of output formats. // 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 isHTML {
if f.LiveReloadPort > 0 { if f.LiveReloadBaseURL != nil {
transformers = append(transformers, livereloadinject.New(f.LiveReloadPort)) transformers = append(transformers, livereloadinject.New(*f.LiveReloadBaseURL))
} }
// This is only injected on the home page. // This is only injected on the home page.

View file

@ -16,6 +16,9 @@ package livereloadinject
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"html"
"net/url"
"strings"
"github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/transform" "github.com/gohugoio/hugo/transform"
@ -35,7 +38,8 @@ var tags = []tag{
// New creates a function that can be used // New creates a function that can be used
// to inject a script tag for the livereload JavaScript in a HTML document. // 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 { return func(ft transform.FromTo) error {
b := ft.From().Bytes() b := ft.From().Bytes()
var idx = -1 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)) c := make([]byte, len(b))
copy(c, b) copy(c, b)
@ -59,7 +69,7 @@ func New(port int) transform.Transformer {
return err return err
} }
script := []byte(fmt.Sprintf(`<script src="/livereload.js?port=%d&amp;mindelay=10&amp;v=2" data-no-instant defer></script>`, port)) script := []byte(fmt.Sprintf(`<script src="%s" data-no-instant defer></script>`, html.EscapeString(src)))
i := idx i := idx
if match.appendScript { if match.appendScript {

View file

@ -15,6 +15,7 @@ package livereloadinject
import ( import (
"bytes" "bytes"
"net/url"
"strings" "strings"
"testing" "testing"
@ -25,12 +26,17 @@ import (
func TestLiveReloadInject(t *testing.T) { func TestLiveReloadInject(t *testing.T) {
c := qt.New(t) c := qt.New(t)
expectBase := `<script src="/livereload.js?port=1313&amp;mindelay=10&amp;v=2" data-no-instant defer></script>` lrurl, err := url.Parse("http://localhost:1234/subpath")
if err != nil {
t.Errorf("Parsing test URL failed")
return
}
expectBase := `<script src="/subpath/livereload.js?mindelay=10&amp;v=2&amp;port=1234&amp;path=subpath/livereload" data-no-instant defer></script>`
apply := func(s string) string { apply := func(s string) string {
out := new(bytes.Buffer) out := new(bytes.Buffer)
in := strings.NewReader(s) in := strings.NewReader(s)
tr := transform.New(New(1313)) tr := transform.New(New(*lrurl))
tr.Apply(out, in) tr.Apply(out, in)
return out.String() return out.String()