Fix for issue 839 and 490 on Windows

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.
This commit is contained in:
Dan Hersam 2015-01-29 13:30:51 -05:00 committed by bep
parent d397bc4f43
commit 523f38a9a8
3 changed files with 14 additions and 4 deletions

View file

@ -358,7 +358,7 @@ func NewWatcher(port int) error {
continue 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 static_changed = static_changed || isstatic
dynamic_changed = dynamic_changed || !isstatic dynamic_changed = dynamic_changed || !isstatic

View file

@ -174,9 +174,17 @@ func AbsPathify(inPath string) string {
return filepath.Clean(filepath.Join(viper.GetString("WorkingDir"), inPath)) 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) { func MakeStaticPathRelative(inPath string) (string, error) {
staticDir := AbsPathify(viper.GetString("StaticDir")) staticDir := GetStaticDirPath()
themeStaticDir := AbsPathify("themes/"+viper.GetString("theme")) + "/static/" themeStaticDir := GetThemesDirPath()
return MakePathRelative(inPath, staticDir, themeStaticDir) return MakePathRelative(inPath, staticDir, themeStaticDir)
} }

View file

@ -15,6 +15,7 @@ package livereload
import ( import (
"net/http" "net/http"
"strings"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
@ -44,7 +45,8 @@ func ForceRefresh() {
func RefreshPath(s string) { func RefreshPath(s string) {
// Tell livereload a file has changed - will force a hard refresh if not CSS or an image // 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) { func ServeJS(w http.ResponseWriter, r *http.Request) {