Skip to content

Commit

Permalink
Fix live reload when both CSS and HTML changes
Browse files Browse the repository at this point in the history
This seems to be a browser bug (tested in both Chrome and Safari on MacOS), but it seems that doing a `window.location.reload()` (or `window.location.reload(true)`) doesn't refresh the CSS changes, even if HTTP caching is disabled.

This commit works around this by doing additional refreshes of the CSSes.

Closes #12600
  • Loading branch information
bep committed Jun 15, 2024
1 parent 57165d4 commit ad6d91c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions commands/hugobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,29 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
livereload.RefreshPath(pathToRefresh)
} else {
livereload.ForceRefresh()
// See https://github.com/gohugoio/hugo/issues/12600.
// If this change set also contains one or more CSS files, we need to
// refresh these as well.
var cssChanges []string
var otherChanges []string

for _, ev := range changed {
if strings.HasSuffix(ev, ".css") {
cssChanges = append(cssChanges, ev)
} else {
otherChanges = append(otherChanges, ev)
}
}

if len(otherChanges) > 0 {
livereload.ForceRefresh()
// Allow some time for the live reload script to get reconnected.
time.Sleep(100 * time.Millisecond)
}

for _, ev := range cssChanges {
livereload.RefreshPath(h.PathSpec.RelURL(paths.ToSlashTrimLeading(ev), false))
}
}
}

Expand Down

0 comments on commit ad6d91c

Please sign in to comment.