You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add before_build and after_build hooks to dev server
Add optional hooks that run around each rebuild cycle in the dev server.
Hooks return Result(Nil, String) so they can abort the rebuild with a
descriptive error message (e.g. a failing Tailwind compilation).
Execution order: before_build → build command → after_build → SSE reload.
A failing before_build aborts the build entirely; a failing after_build
prevents the browser reload. Both error paths log the reason and keep
the server running.
Internal changes:
- Introduce RebuildStateConfig public type to bundle build_command and
hooks into rebuild_actor.new()
- Refactor rebuild() to use Result with `use _ <- result.try` for clean
short-circuit chaining via run_hook() and exec_build() helpers
- Update existing tests for the new RebuildStateConfig API
- Add 7 new tests covering hook invocation, ordering, and error handling
- Document hooks in docs/dev-server.md (API, reference table, rebuild flow)
Copy file name to clipboardExpand all lines: docs/dev-server.md
+50-8Lines changed: 50 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,14 +99,14 @@ The server starts, performs an initial build, then watches for changes:
99
99
```text
100
100
🔨 blogatto dev server starting...
101
101
👀 Watching for file changes...
102
-
👀 Watching: ./static
103
-
👀 Watching: ./src/
104
-
👀 Watching: ./blog
102
+
👀 Watching: ./static
103
+
👀 Watching: ./src/
104
+
👀 Watching: ./blog
105
105
106
106
⟳ Rebuilding...
107
107
✓ Rebuild complete
108
-
→ http://127.0.0.1:3000
109
-
Output: ./dist
108
+
→ http://127.0.0.1:3000
109
+
Output: ./dist
110
110
```
111
111
112
112
Open `http://127.0.0.1:3000` in your browser. When you save a file, the site rebuilds and the browser reloads automatically.
@@ -122,6 +122,14 @@ blog.config()
122
122
|> dev.port(8080)
123
123
|> dev.host("0.0.0.0")
124
124
|> dev.live_reload(False)
125
+
|> dev.before_build(fn() {
126
+
io.println("Starting build...")
127
+
Ok(Nil)
128
+
})
129
+
|> dev.after_build(fn() {
130
+
io.println("Build finished!")
131
+
Ok(Nil)
132
+
})
125
133
|> dev.start()
126
134
```
127
135
@@ -182,6 +190,37 @@ dev.new(config)
182
190
|> dev.live_reload(False)
183
191
```
184
192
193
+
### `dev.before_build(server, hook)`
194
+
195
+
Set a function to run **before** each rebuild. The hook runs before the build command is executed, on every rebuild (including the initial build on startup). This is useful for setup or cleanup tasks.
196
+
197
+
The hook must return `Result(Nil, String)`. If it returns `Error(reason)`, the build is aborted and the error reason is logged. The hook runs regardless of whether the subsequent build would succeed or fail.
198
+
199
+
```gleam
200
+
dev.new(config)
201
+
|> dev.before_build(fn() {
202
+
// Clean generated assets, fetch data, etc.
203
+
io.println("Preparing build...")
204
+
Ok(Nil)
205
+
})
206
+
```
207
+
208
+
### `dev.after_build(server, hook)`
209
+
210
+
Set a function to run **after** each successful rebuild. The hook runs only when the build command exits with code 0. This is useful for post-processing tasks like running Tailwind CSS, copying additional assets, or sending notifications.
211
+
212
+
The hook must return `Result(Nil, String)`. If it returns `Error(reason)`, the error is logged and browsers are **not** reloaded. The hook is **not** called when the build fails.
213
+
214
+
```gleam
215
+
dev.new(config)
216
+
|> dev.after_build(fn() {
217
+
case shellout.command("npx", ["tailwindcss", "-o", "./dist/style.css"], ".", []) {
Copy file name to clipboardExpand all lines: gleam.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
name = "blogatto"
2
-
version = "4.0.2"
2
+
version = "5.0.0"
3
3
description = "A Gleam framework for building static blogs with Lustre and Markdown. Generates HTML pages, RSS feeds, sitemaps, and robots.txt from markdown files with frontmatter, with multilingual support."
4
4
repository = { type = "github", user = "veeso", repo = "blogatto" }
0 commit comments