about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-06-19 13:50:37 +0200
committerAlan Pearce2024-06-19 14:10:44 +0200
commit45a2e290aa2bc59deb5971255accba517500cc25 (patch)
tree1d6fe7c96aab7e13faa2fdb5eea4416370a1423b
parent1f9f24e2cef08f40fe2597b3644d08b28e31d370 (diff)
downloadwebsite-45a2e290aa2bc59deb5971255accba517500cc25.tar.lz
website-45a2e290aa2bc59deb5971255accba517500cc25.tar.zst
website-45a2e290aa2bc59deb5971255accba517500cc25.zip
fix dev server/live reload
-rw-r--r--internal/builder/builder.go1
-rw-r--r--internal/builder/page.templ8
-rw-r--r--internal/builder/template.go2
-rw-r--r--internal/server/dev.go55
-rw-r--r--internal/server/server.go7
-rwxr-xr-xjustfile2
-rw-r--r--modd.conf6
7 files changed, 64 insertions, 17 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go
index b4ade4a..44ab402 100644
--- a/internal/builder/builder.go
+++ b/internal/builder/builder.go
@@ -267,5 +267,6 @@ func BuildSite(ioConfig IOConfig) (*Result, error) {
 		}
 	}
 
+	loadCSS()
 	return build(ioConfig.Destination, *config)
 }
diff --git a/internal/builder/page.templ b/internal/builder/page.templ
index c99e315..7b3380f 100644
--- a/internal/builder/page.templ
+++ b/internal/builder/page.templ
@@ -52,6 +52,14 @@ templ page(site config.Config, page PageSettings) {
 				<a href="https://opensource.org/licenses/MIT">MIT</a>
 			</footer>
 			@counter(page.Path, page.Title)
+			if site.InjectLiveReload {
+				<script defer>
+					new EventSource("/_/reload").onmessage = event => {
+						console.log("got message", event)
+						window.location.reload()
+					};
+				</script>
+			}
 		</body>
 	</html>
 }
diff --git a/internal/builder/template.go b/internal/builder/template.go
index 9b1d9bd..53199fc 100644
--- a/internal/builder/template.go
+++ b/internal/builder/template.go
@@ -30,7 +30,7 @@ var (
 	}
 )
 
-func init() {
+func loadCSS() {
 	bytes, err := os.ReadFile("templates/style.css")
 	if err != nil {
 		panic(err)
diff --git a/internal/server/dev.go b/internal/server/dev.go
index f7ebb82..ce718eb 100644
--- a/internal/server/dev.go
+++ b/internal/server/dev.go
@@ -5,7 +5,9 @@ import (
 	"io/fs"
 	"log/slog"
 	"os"
+	"path"
 	"path/filepath"
+	"slices"
 	"time"
 	"website/internal/log"
 
@@ -17,6 +19,29 @@ type FileWatcher struct {
 	*fsnotify.Watcher
 }
 
+var (
+	ignores = []string{
+		"*.templ",
+		"*.go",
+	}
+	checkSettleInterval = 200 * time.Millisecond
+)
+
+func matches(name string) func(string) bool {
+	return func(pattern string) bool {
+		matched, err := path.Match(pattern, name)
+		if err != nil {
+			log.Warn("error checking watcher ignores", "error", err)
+		}
+
+		return matched
+	}
+}
+
+func ignored(pathname string) bool {
+	return slices.ContainsFunc(ignores, matches(path.Base(pathname)))
+}
+
 func NewFileWatcher() (*FileWatcher, error) {
 	watcher, err := fsnotify.NewWatcher()
 	if err != nil {
@@ -46,23 +71,31 @@ func (watcher FileWatcher) AddRecursive(from string) error {
 }
 
 func (watcher FileWatcher) Start(callback func(string)) {
+	var timer *time.Timer
 	for {
 		select {
 		case event := <-watcher.Events:
-			if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) {
-				f, err := os.Stat(event.Name)
-				if err != nil {
-					slog.Error(fmt.Sprintf("error handling %s event: %v", event.Op.String(), err))
-				} else if f.IsDir() {
-					err = watcher.Add(event.Name)
+			if !ignored(event.Name) {
+				log.Info("watcher event", "name", event.Name, "op", event.Op.String())
+				if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) {
+					f, err := os.Stat(event.Name)
 					if err != nil {
-						slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err))
+						slog.Error(fmt.Sprintf("error handling %s event: %v", event.Op.String(), err))
+					} else if f.IsDir() {
+						err = watcher.Add(event.Name)
+						if err != nil {
+							slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err))
+						}
 					}
 				}
-			}
-			if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) {
-				callback(event.Name)
-				time.Sleep(500 * time.Millisecond)
+				if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) {
+					if timer == nil {
+						timer = time.AfterFunc(checkSettleInterval, func() {
+							callback(event.Name)
+						})
+					}
+					timer.Reset(checkSettleInterval)
+				}
 			}
 		case err := <-watcher.Errors:
 			slog.Error(fmt.Sprintf("error in watcher: %v", err))
diff --git a/internal/server/server.go b/internal/server/server.go
index 1b0407d..a658235 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -100,7 +100,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 		if err != nil {
 			return nil, errors.WithMessage(err, "could not create file watcher")
 		}
-		for _, dir := range []string{"content", "static", "templates"} {
+		for _, dir := range []string{"content", "static", "templates", "internal/builder"} {
 			err := fw.AddRecursive(dir)
 			if err != nil {
 				return nil, errors.WithMessagef(
@@ -110,8 +110,11 @@ func New(runtimeConfig *Config) (*Server, error) {
 				)
 			}
 		}
+		err = fw.Add(".")
+		if err != nil {
+			return nil, errors.WithMessage(err, "could not add directory to file watcher")
+		}
 		go fw.Start(func(filename string) {
-			log.Debug("file updated", "filename", filename)
 			r, err := builder.BuildSite(builderConfig)
 			if err != nil {
 				log.Error("could not build site", "error", err)
diff --git a/justfile b/justfile
index 0937667..00f13a1 100755
--- a/justfile
+++ b/justfile
@@ -22,7 +22,7 @@ build:
 	go run ./cmd/build
 
 dev:
-	systemfd -s http::3000 -- modd
+	modd
 
 deploy-vercel-preview: clean build
 	vercel pull --environment=preview
diff --git a/modd.conf b/modd.conf
index 072956c..f956fea 100644
--- a/modd.conf
+++ b/modd.conf
@@ -1,3 +1,5 @@
-config.toml {
-    daemon: templ generate --watch --proxy "http://localhost:3000" --cmd "go run ./cmd/server --dev" --open-browser=false
+**/*.go !**/*_templ.go {
+  daemon +sigint: systemfd -s http::3000 -- \
+    templ generate --watch \
+      --cmd="go run ./cmd/server --dev"
 }