Fix for issue 839 and 490 on Windows
authorDan Hersam <dan@hersam.com>
Thu, 29 Jan 2015 18:30:51 +0000 (13:30 -0500)
committerbep <bjorn.erik.pedersen@gmail.com>
Fri, 30 Jan 2015 13:18:17 +0000 (14:18 +0100)
The paths were seen as changed but not static because of the backslashes in
ev.Name. Once the backslashes were added, I discovered that the JSON
sent to livereload was invalid and failed to work because it had backslashes.

Hence the code to replace the backslashes from the path to make them work
in JSON and for the URL.

With this fix, changes to a stylesheet are shown on the page, and if it's a
single file that changed, it's reflected in the browser without reloading the whole
page.

commands/hugo.go
helpers/path.go
livereload/livereload.go

index cce37a5f9cab56ed0f72f017fd5f88816c18d1f6..e53804f1ce9814b6ddce91d03e5a8f9e107a325c 100644 (file)
@@ -358,7 +358,7 @@ func NewWatcher(port int) error {
                                                continue
                                        }
 
-                                       isstatic := strings.HasPrefix(ev.Name, helpers.AbsPathify(viper.GetString("StaticDir"))) || strings.HasPrefix(ev.Name, helpers.AbsPathify("themes/"+viper.GetString("theme"))+"/static/")
+                                       isstatic := strings.HasPrefix(ev.Name, helpers.GetStaticDirPath()) || strings.HasPrefix(ev.Name, helpers.GetThemesDirPath())
                                        static_changed = static_changed || isstatic
                                        dynamic_changed = dynamic_changed || !isstatic
 
index 3351e02651e881dacfa1a4406710ee2e35b1cd79..7965a6adaec8e6910e06ae4da4d5593dec569758 100644 (file)
@@ -174,9 +174,17 @@ func AbsPathify(inPath string) string {
        return filepath.Clean(filepath.Join(viper.GetString("WorkingDir"), inPath))
 }
 
+func GetStaticDirPath() string {
+       return AbsPathify(viper.GetString("StaticDir"))
+}
+
+func GetThemesDirPath() string {
+       return AbsPathify(filepath.Join("themes", viper.GetString("theme"), "static"))
+}
+
 func MakeStaticPathRelative(inPath string) (string, error) {
-       staticDir := AbsPathify(viper.GetString("StaticDir"))
-       themeStaticDir := AbsPathify("themes/"+viper.GetString("theme")) + "/static/"
+       staticDir := GetStaticDirPath()
+       themeStaticDir := GetThemesDirPath()
 
        return MakePathRelative(inPath, staticDir, themeStaticDir)
 }
index 1b1546158afb8a43d11416a5b9d9a9fbd3fc4cf9..a47a7b687227fd4500ab439f9c31ee3725715eb5 100644 (file)
@@ -15,6 +15,7 @@ package livereload
 
 import (
        "net/http"
+       "strings"
 
        "github.com/gorilla/websocket"
 )
@@ -44,7 +45,8 @@ func ForceRefresh() {
 
 func RefreshPath(s string) {
        // Tell livereload a file has changed - will force a hard refresh if not CSS or an image
-       wsHub.broadcast <- []byte(`{"command":"reload","path":"` + s + "\"" + `,"originalPath":"","liveCSS":true,"liveImg":true}`)
+       url_path := strings.Replace(s, "\\", "/", -1) // If path has backslashes on Windows, make path work for URL
+       wsHub.broadcast <- []byte(`{"command":"reload","path":"` + url_path + "\"" + `,"originalPath":"","liveCSS":true,"liveImg":true}`)
 }
 
 func ServeJS(w http.ResponseWriter, r *http.Request) {