reload templates (not just rerender) on src change

This commit is contained in:
facundoolano 2024-02-15 14:41:02 -03:00
parent b788826423
commit f43b0cdb60

View file

@ -14,17 +14,13 @@ import (
// Generate and serve the site, rebuilding when the source files change.
func Serve() error {
site, err := site.Load(SRC_DIR, LAYOUTS_DIR)
if err != nil {
return err
}
if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil {
if err := rebuild(); err != nil {
return err
}
// watch for changes in src and layouts, and trigger a rebuild
watcher, err := setupWatcher(site)
watcher, err := setupWatcher()
if err != nil {
return err
}
@ -39,6 +35,19 @@ func Serve() error {
return nil
}
func rebuild() error {
site, err := site.Load(SRC_DIR, LAYOUTS_DIR)
if err != nil {
return err
}
if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil {
return err
}
return nil
}
// Tweaks the http file system to construct a server that hides the .html suffix from requests.
// Based on https://stackoverflow.com/a/57281956/993769
type HTMLDir struct {
@ -57,7 +66,7 @@ func (d HTMLDir) Open(name string) (http.File, error) {
return f, err
}
func setupWatcher(site *site.Site) (*fsnotify.Watcher, error) {
func setupWatcher() (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
@ -88,7 +97,7 @@ func setupWatcher(site *site.Site) (*fsnotify.Watcher, error) {
return
}
if err := site.Build(SRC_DIR, TARGET_DIR, false, true); err != nil {
if err := rebuild(); err != nil {
fmt.Println("error:", err)
return
}