Load livereload.js from "/"
authorKato Kazuyoshi <kato.kazuyoshi@gmail.com>
Wed, 14 Oct 2015 07:41:54 +0000 (00:41 -0700)
committerspf13 <steve.francia@gmail.com>
Thu, 15 Oct 2015 20:36:13 +0000 (16:36 -0400)
Fix #1406
Instead of loading the file from http://localhost:port/, it can be
loaded from /.

transform/livereloadinject.go
transform/livereloadinject_test.go [new file with mode: 0644]

index 38e4e8cc96df3f78b05b8a931a518352930a3f52..2fb130c17cae0f9407a4cb5327a0613d930fb7a2 100644 (file)
@@ -2,18 +2,13 @@ package transform
 
 import (
        "bytes"
-       "github.com/spf13/viper"
 )
 
 func LiveReloadInject(ct contentTransformer) {
        match := []byte("</body>")
-       port := viper.GetString("port")
-       replace := []byte(`<script data-no-instant>document.write('<script src="http://'
-        + (location.host || 'localhost').split(':')[0]
-               + ':` + port + `/livereload.js?mindelay=10"></'
-        + 'script>')</script></body>`)
-       newcontent := bytes.Replace(ct.Content(), match, replace, -1)
+       replace := []byte(`<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script></body>`)
 
+       newcontent := bytes.Replace(ct.Content(), match, replace, -1)
        if len(newcontent) == len(ct.Content()) {
                match := []byte("</BODY>")
                newcontent = bytes.Replace(ct.Content(), match, replace, -1)
diff --git a/transform/livereloadinject_test.go b/transform/livereloadinject_test.go
new file mode 100644 (file)
index 0000000..605f659
--- /dev/null
@@ -0,0 +1,20 @@
+package transform
+
+import (
+       "bytes"
+       "github.com/spf13/hugo/helpers"
+       "testing"
+)
+
+func TestLiveReloadInject(t *testing.T) {
+       out := new(bytes.Buffer)
+       in := helpers.StringToReader("</body>")
+
+       tr := NewChain(LiveReloadInject)
+       tr.Apply(out, in, []byte("path"))
+
+       expected := `<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script></body>`
+       if string(out.Bytes()) != expected {
+               t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
+       }
+}