refactor: only reload templates when such files are updated
Alan Pearce alan@alanpearce.eu
Sun, 05 May 2024 18:05:30 +0200
2 files changed, 9 insertions(+), 6 deletions(-)
M internal/server/dev.go → internal/server/dev.go
@@ -44,7 +44,7 @@ return errors.WithMessage(err, "error walking directory tree") } -func (watcher FileWatcher) Start(callback func()) { +func (watcher FileWatcher) Start(callback func(string)) { for { select { case event := <-watcher.Events: @@ -61,7 +61,7 @@ } } } if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) { - callback() + callback(event.Name) time.Sleep(500 * time.Millisecond) } case err := <-watcher.Errors:
M internal/server/server.go → internal/server/server.go
@@ -203,10 +203,13 @@ err = fw.AddRecursive(path.Join("frontend")) if err != nil { return nil, errors.WithMessage(err, "could not add directory to file watcher") } - go fw.Start(func() { - templates, err = loadTemplates() - if err != nil { - slog.Error(fmt.Sprintf("could not reload templates: %v", err)) + go fw.Start(func(filename string) { + slog.Debug(fmt.Sprintf("got filename %s", filename)) + if path.Ext(filename) == ".gotmpl" { + templates, err = loadTemplates() + if err != nil { + slog.Error(fmt.Sprintf("could not reload templates: %v", err)) + } } liveReload.Reload() })