all repos — searchix @ 158904f480e558ca00f680e7c577bb6329605eff

Search engine for NixOS, nix-darwin, home-manager and NUR users

refactor: only reload templates when such files are updated
Alan Pearce alan@alanpearce.eu
Sun, 05 May 2024 18:05:30 +0200
commit

158904f480e558ca00f680e7c577bb6329605eff

parent

c689cf5d0c8f6c62fb21c300cec187305d8262d9

2 files changed, 9 insertions(+), 6 deletions(-)

jump to
M internal/server/dev.gointernal/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.gointernal/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()
 		})