Enable soft livereload of CSS and images

Prior to this commit a dummy JavaScript filename was sent to LiveReload when changing a static file (CSS, image etc.), forcing a full browser reload of the page.

This commit fixes this by sending the relative file path of the changed static resource, enabling partial live reloading for CSS- and image-changes. If more than one static file happens to end up in the same changeevent-batch, it will fall back to do a full refresh. To enable this logic, the change events with names ending with ".goutputstream*" is now filtered out as temporary.

Changes in dynamic content behaves like before.

Issue #490
This commit is contained in:
bep 2014-09-10 12:21:22 +02:00 committed by spf13
parent 0099b5a3cb
commit df489b4712
4 changed files with 65 additions and 3 deletions

View file

@ -312,10 +312,11 @@ func NewWatcher(port int) error {
static_changed := false
dynamic_changed := false
static_files_changed := make(map[string]bool)
for _, ev := range evs {
ext := filepath.Ext(ev.Name)
istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp")
istemp := strings.HasSuffix(ext, "~") || (ext == ".swp") || (ext == ".tmp") || (strings.HasPrefix(ext, ".goutputstream"))
if istemp {
continue
}
@ -328,6 +329,12 @@ func NewWatcher(port int) error {
static_changed = static_changed || isstatic
dynamic_changed = dynamic_changed || !isstatic
if isstatic {
if staticPath, err := helpers.MakeStaticPathRelative(ev.Name); err == nil {
static_files_changed[staticPath] = true
}
}
// add new directory to watch list
if s, err := os.Stat(ev.Name); err == nil && s.Mode().IsDir() {
if ev.IsCreate() {
@ -342,7 +349,16 @@ func NewWatcher(port int) error {
if !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
livereload.ForceRefresh()
// force refresh when more than one file
if len(static_files_changed) == 1 {
for path := range static_files_changed {
livereload.RefreshPath(path)
}
} else {
livereload.ForceRefresh()
}
}
}