astwerk

9. The dev loop

go run . is fine once, but you want the build to watch and rebuild. The devserver package is that loop. Move the tree into a helper so both branches build the same site, and add --serve:

go
package main

import (
	"context"
	"log"
	"os"

	"github.com/LukasDerBaum42/astwerk/content"
	"github.com/LukasDerBaum42/astwerk/devserver"
	"github.com/LukasDerBaum42/astwerk/ssg"
	"github.com/a-h/templ"
	"example.com/mysite/pages"
)

type FrontMatter struct {
	Title       string
	Description string
}

func main() {
	if len(os.Args) > 1 && os.Args[1] == "--serve" {
		devserver.Run(context.Background(), devserver.Config{
			Build: devserver.Steps(
				devserver.Command("templ", "generate"),
				devserver.Command("go", "run", ".", "--dev"),
			),
		})
		return
	}
	dev := len(os.Args) > 1 && os.Args[1] == "--dev"
	if err := ssg.Build(tree(), ssg.BuildOptions{Dev: dev}); err != nil {
		log.Fatal(err)
	}
}

func tree() ssg.Node {
	root := ssg.Node{
		Title: "My Site",
		Page:  ssg.Templ(pages.Home),
		Children: map[string]ssg.Node{
			"about":    {Title: "About", Page: ssg.Templ(pages.About)},
			"projects": projects(),
			"style":    {CopyFrom: "style"},
		},
	}
	return ssg.BuildLocales(root, []ssg.Locale{{Code: "de"}})
}

// projects is unchanged from step 5.

The build runs out of process, because a running Go program cannot load new code — editing a .templ recompiles, rebuilds, and reloads the browser. A failed build shows its compiler error in the browser instead of silently serving the last good output. See Dev server.

Try it. Edit a .templ file while the dev server runs — then introduce a compile error on purpose.

What you should see

Each save rebuilds and reloads the browser; a broken build shows its compiler error as an overlay instead of serving stale output.

Why must the build run out of process?

What happens when a build fails?