astwerk

Scripts & routing

Compiling scripts

CompileFrom is what turned scripts/demo.go into the WebAssembly running the demos in this section — no bundler, no package.json. See Scripts & WebAssembly.

go
//go:build js && wasm

package main

import "github.com/LukasDerBaum42/astwerk/wasmwrap"

func main() {
	wasmwrap.Query("#toggle").On("click", func(e wasmwrap.Event) {
		e.PreventDefault()
		wasmwrap.Query("#nav").Class().Toggle("open")
	})
	select {} // keep the module alive for the handler
}

// "scripts": {CompileFrom: "scripts"},  →  build/scripts/demo.wasm + wasm_exec.js
What the build produced

scripts/demo.go became build/scripts/demo.wasm, with Go's wasm_exec.js copied in beside it — exactly what WasmScript loads on the demo pages above.

Client-side routing

reactive.Router swaps views against the real address bar, which is exactly why it isn't embedded live on this page — a demo router would hijack this site's own navigation. This site's docs sidebar and pager are plain links, not a router; this is the shape a single-page app takes instead. See reactive.

go
reactive.Router(root, []reactive.Route{
	{Path: "/", View: home},
	{Path: "/projects/:slug", View: func(p reactive.Params) wasmwrap.Element {
		return project(p.Get("slug"))
	}},
	{Path: "/*", View: notFound},
})
reactive.InterceptLinks(wasmwrap.Body())

// Navigate("/projects/dice-dungeon") pushes a history entry and swaps the
// view; each view gets its own scope, disposed when you navigate away.