jskelet 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/AGENTS.md +132 -132
  2. package/CHANGELOG.md +403 -383
  3. package/LICENSE +21 -21
  4. package/bin/jskelet.mjs +103 -103
  5. package/docs/01-baslangic.md +285 -285
  6. package/docs/02-mimari.md +293 -287
  7. package/docs/03-routing.md +486 -480
  8. package/docs/04-render-ve-sablonlar.md +490 -490
  9. package/docs/05-islands.md +482 -482
  10. package/docs/06-cache.md +1231 -1209
  11. package/docs/07-yapilandirma.md +44 -21
  12. package/docs/08-build.md +366 -366
  13. package/docs/09-dev-araclari.md +335 -335
  14. package/docs/10-dagitim.md +329 -329
  15. package/docs/11-tasima.md +1 -0
  16. package/docs/12-panel-ve-oturum.md +384 -384
  17. package/docs/README.md +105 -105
  18. package/docs/en/01-getting-started.md +292 -292
  19. package/docs/en/02-architecture.md +311 -305
  20. package/docs/en/03-routing.md +503 -497
  21. package/docs/en/04-rendering.md +504 -504
  22. package/docs/en/05-islands.md +492 -492
  23. package/docs/en/06-caching.md +1197 -1198
  24. package/docs/en/07-configuration.md +1009 -986
  25. package/docs/en/08-build.md +383 -383
  26. package/docs/en/09-dev-tools.md +342 -342
  27. package/docs/en/10-deployment.md +332 -332
  28. package/docs/en/11-migration.md +360 -359
  29. package/docs/en/12-dashboards-and-sessions.md +392 -392
  30. package/docs/en/README.md +112 -112
  31. package/package.json +102 -102
  32. package/src/build/ensure-build.mjs +15 -15
  33. package/src/build/paths.mjs +143 -143
  34. package/src/build/resolve-peer.mjs +36 -36
  35. package/src/build/tasks/client.mjs +268 -268
  36. package/src/build/tasks/css.mjs +124 -124
  37. package/src/build/tasks/fonts.mjs +146 -146
  38. package/src/build/tasks/icons.mjs +224 -224
  39. package/src/build/tasks/images.mjs +244 -244
  40. package/src/build/tasks/precompress.mjs +78 -78
  41. package/src/client/{cache-panel → admin}/i18n.js +756 -670
  42. package/src/client/{cache-panel → admin}/login.html +74 -74
  43. package/src/client/{cache-panel → admin}/panel.css +804 -756
  44. package/src/client/admin/panel.html +486 -0
  45. package/src/client/{cache-panel → admin}/panel.js +1242 -915
  46. package/src/client/devtools/report.html +185 -185
  47. package/src/client/devtools/report.js +725 -725
  48. package/src/client/dom.js +95 -95
  49. package/src/client/form.js +192 -192
  50. package/src/client/index.js +35 -35
  51. package/src/client/registry.js +297 -297
  52. package/src/client/safe-image.js +91 -91
  53. package/src/client/store.js +36 -36
  54. package/src/client/swap.js +188 -188
  55. package/src/config/defaults.js +16 -7
  56. package/src/config/index.js +38 -22
  57. package/src/config/pattern.js +107 -107
  58. package/src/http/control-flow.js +71 -71
  59. package/src/http/cookies.js +257 -257
  60. package/src/http/request-cache.js +46 -46
  61. package/src/http/request-context.js +162 -162
  62. package/src/index.js +83 -83
  63. package/src/init.mjs +221 -221
  64. package/src/log.mjs +58 -0
  65. package/src/runtime/alias-hooks.mjs +119 -119
  66. package/src/runtime/register.mjs +4 -4
  67. package/src/server/admin/actions.js +229 -0
  68. package/src/server/admin/auth.js +125 -0
  69. package/src/server/admin/event-log.js +151 -0
  70. package/src/server/admin/gate.js +209 -0
  71. package/src/server/admin/inventory.js +188 -0
  72. package/src/server/admin/mount.js +56 -0
  73. package/src/server/admin/router.js +216 -0
  74. package/src/server/admin/snapshot.js +126 -0
  75. package/src/server/assets.js +147 -147
  76. package/src/server/cache-deps.js +42 -42
  77. package/src/server/cloudflare.js +607 -607
  78. package/src/server/create-app.js +295 -291
  79. package/src/server/data-cache.js +462 -462
  80. package/src/server/dev/report.js +369 -369
  81. package/src/server/dev/socket.js +170 -170
  82. package/src/server/dev/version-check.mjs +139 -139
  83. package/src/server/html-cache.js +817 -817
  84. package/src/server/metadata.js +102 -102
  85. package/src/server/middleware/compression.js +205 -205
  86. package/src/server/middleware/csrf.js +134 -134
  87. package/src/server/middleware/dev-gate.js +62 -62
  88. package/src/server/middleware/headers.js +37 -37
  89. package/src/server/middleware/redirects.js +32 -32
  90. package/src/server/middleware/static-precompressed.js +100 -100
  91. package/src/server/middleware/trailing-slash.js +53 -0
  92. package/src/server/middleware/upstream-proxy.js +141 -141
  93. package/src/server/prewarm.js +601 -601
  94. package/src/server/redis.js +569 -569
  95. package/src/server/router.js +128 -128
  96. package/src/server/status-page.js +164 -164
  97. package/src/server/upstream-limiter.js +376 -376
  98. package/src/server/upstream-tracking.js +166 -166
  99. package/src/start.mjs +7 -7
  100. package/src/templates/layout.ejs +44 -44
  101. package/src/version.mjs +31 -31
  102. package/src/views/components/loader.js +85 -85
  103. package/src/views/helpers/html.js +102 -102
  104. package/src/views/helpers/tags.js +245 -245
  105. package/src/client/cache-panel/panel.html +0 -308
  106. package/src/server/cache-panel.js +0 -759
@@ -1,342 +1,342 @@
1
- # 09 — Development tools
2
-
3
- This document explains what `jskelet dev` does and why it does it that way: how
4
- the two child processes are managed, the shape of the terminal output, the
5
- watched directories and the reason a custom watcher was written instead of using
6
- `node --watch`, the distinction between CSS hot-swap and a full reload, the
7
- devtools overlay opened with `Alt+D`, the detailed report page, and the dev gate
8
- built on `DEV_TOKEN`. The build steps themselves are in
9
- [08-build.md](./08-build.md).
10
-
11
- ## The `jskelet dev` flow
12
-
13
- The command manages two long-lived child processes in a single terminal:
14
-
15
- ```
16
- jskelet dev
17
- ├─ build watch node src/build/build.mjs --watch
18
- └─ server node [--env-file=.env] --import <register.mjs> src/start.mjs
19
- ```
20
-
21
- `NODE_ENV=development` is assigned here in a platform-independent way — no
22
- `cross-env` needed. The child processes also receive `JSKELET_CHILD=1`
23
- (suppresses the build banner) and, if a TTY is present, `JSKELET_COLOR=1`
24
- (forces color on piped output).
25
-
26
- Startup order: banner → build steps → server ready → `Ready` summary. The
27
- summary is printed once both the build and the server are ready; otherwise it
28
- got buried among the build lines arriving afterwards.
29
-
30
- The server signals readiness with a single line inside `startServer`:
31
-
32
- ```
33
- jskelet → http://localhost:3000 (development)
34
- ```
35
-
36
- The shape of this line is a contract; the dev script parses it and prints the
37
- summary line accordingly.
38
-
39
- ### The shape of the terminal output
40
-
41
- Child process output does not stream through as-is. There are two regions and
42
- they never mix:
43
-
44
- 1. **Startup:** banner, aligned build lines, `Ready` summary.
45
- 2. **Runtime:** timestamped, single-line events (HTTP requests, CSS rebuild,
46
- server restart).
47
-
48
- Error stacks are turned into a framed box: because stack lines arrive in
49
- fragments, they are collected after a short silence (60 ms), the error name and
50
- message are parsed, the first three frames are shown, and the project root is
51
- shortened to `.`. When you are developing your own framework, not losing the
52
- error in the stream is the detail that genuinely makes a difference.
53
-
54
- Color carries meaning: `✓` green, `✖` red, `⚠` yellow, `↻` cyan; durations and
55
- paths gray. No decorative color is used. If `NO_COLOR` is set, no color is used
56
- at all.
57
-
58
- `Ctrl+C` (SIGINT/SIGTERM) shuts down all child processes. If a child exits with
59
- a non-zero code (other than an expected restart), an error is printed and the
60
- dev process exits too.
61
-
62
- ## Watch directories
63
-
64
- Server restarts are managed by the framework's own watcher.
65
-
66
- ```js
67
- WATCH_DIRS = [
68
- config.dirs.routes,
69
- config.dirs.views,
70
- <root>/lib,
71
- ...config.watch, // jskelet.config.mjs → watch
72
- ]
73
- ```
74
-
75
- The `jskelet.config.mjs` file itself is watched as well: when the config
76
- changes, both the server and the build must come up with the new settings.
77
-
78
- Watched extensions: `.js`, `.mjs`, `.json`, `.ejs`.
79
-
80
- `views` is watched too, because most components live in
81
- `views/components/**.js` and, since those modules are imported into the server
82
- once, changes made without a restart never reached the browser (that "I edited
83
- the template and nothing changed" feeling comes from here).
84
-
85
- `client/` and `styles/` are **not** in this list: esbuild and the CSS watchers
86
- handle them on their own ([08-build.md](./08-build.md)).
87
-
88
- If a directory cannot be watched, a warning is printed and no automatic restart
89
- happens for that directory; everything else keeps working.
90
-
91
- ### Why `node --watch` was not used
92
-
93
- Even when given `--watch-path`, Node watched the project root in this setup.
94
- Whenever build output (`public/assets`, `manifest.json`) or the dev tooling log
95
- was written, the server restarted for nothing — in fact a self-feeding loop was
96
- set up: restart → startup warning → write → restart.
97
-
98
- The custom watcher does three things:
99
-
100
- 1. **Watches only server sources.**
101
- 2. **Coalesces changes** (250 ms) and reports which files changed.
102
- 3. **Filters out phantom events.** On Windows, `fs.watch` can emit events for a
103
- file's neighbors when it is written; without comparing `mtime`, a single save
104
- turned into two restarts. Current timestamps are read up front at startup, so
105
- the first phantom event is filtered out as well.
106
-
107
- The restart line shows the changed file, or how many changed:
108
-
109
- ```
110
- 21:04:12 ↻ server restarting… routes/10-pages.mjs
111
- 21:04:12 server restarted 412ms
112
- ```
113
-
114
- If `JSKELET_VERBOSE=1` is set, all files are listed when more than one changed.
115
-
116
- ## CSS hot-swap and full reload
117
-
118
- The dev server watches `.jskelet/manifest.json` and broadcasts events to the
119
- browser over the live channel (`<devBasePath>/ws`). Since the manifest is
120
- rewritten on every build round, change detection is done through the manifest.
121
-
122
- | What changed | Behavior |
123
- | --- | --- |
124
- | Only `app.css` | **CSS hot-swap:** the stylesheet is swapped, the page is not reloaded. State and scroll position are preserved. |
125
- | `main.js`, the sprite, another asset, or more than one key | **Full reload** |
126
-
127
- In both cases the HTML cache is cleared first: the stored HTML carries the old
128
- hashed asset URLs and, if not cleared, the page keeps asking for a deleted file.
129
-
130
- Manifest events are coalesced over 120 ms. If watching is not supported, live
131
- reload is disabled and everything else keeps working.
132
-
133
- When the server restarts, the overlay figures it out from the **boot id**: every
134
- process broadcasts a unique `boot` value, the overlay sees the change, shows the
135
- "restarted" note and does not reset its own state.
136
-
137
- ## The live channel
138
-
139
- Everything the overlay shows — statistics, live reload and CSS hot-swap events —
140
- arrives over a single WebSocket (`<devBasePath>/ws`). The panel used to poll for
141
- statistics every two seconds, so every open tab kept hitting the server even
142
- while the panel was closed. Now the server pushes as things change: when a
143
- request or an error is recorded (coalesced over 120 ms), and every two seconds so
144
- the time-based fields (uptime, memory, the prewarm counter) stay fresh. The
145
- heartbeat is deliberately independent of prewarming: if the channel's tempo
146
- followed a background job, the panel would be tied to that job's rhythm. Nothing
147
- is computed when no panel is connected.
148
-
149
- The handshake happens on the HTTP `upgrade` event, and that event never reaches
150
- the middleware chain, so the channel is attached straight to the server after
151
- `listen` (`attachDevSocket`). The server side pulls in no dependency such as
152
- `ws`: all it needs is to write server-to-client text frames and to answer the
153
- client's ping/close frames.
154
-
155
- If the socket opens and later drops — that is, the server is restarting — it
156
- reconnects every half second and the indicator reads "server restarting…" in the
157
- meantime. If it never opens, it is retried four times with a widening gap: the
158
- page may have been loaded during the server's restart window, and a single
159
- failure does not mean the channel is unavailable. If those attempts fail too (a
160
- proxy in between may not pass WebSocket through), the overlay falls back to the
161
- old path: the `/events` SSE stream plus polling `/stats`.
162
-
163
- ## Devtools overlay
164
-
165
- A floating bubble in the bottom right; opened with `Alt+D`, closed with `Esc` or
166
- by clicking the backdrop. It is only emitted by the layout when
167
- `NODE_ENV=development`:
168
-
169
- ```ejs
170
- <% if (devtools) { %>
171
- <script type="module" src="<%= devBasePath %>/overlay.js"></script>
172
- <% } %>
173
- ```
174
-
175
- The overlay file is **not part of the build.** The server serves it raw from the
176
- framework package; that is why there is no bundler involved, it works as a
177
- single file, and it adds nothing to the production output. The entire UI lives
178
- inside a shadow DOM and does not mix with the page's CSS.
179
-
180
- What it shows:
181
-
182
- - **Errors:** browser-side JS errors, resource loading errors
183
- (`img`/`script`/`link`), and the server's `console.error` / `console.warn`
184
- output. On the server side `console` is wrapped so warnings do not get lost in
185
- the terminal.
186
- - **Requests:** the method, path, status, duration and `X-JSkelet-Cache` value
187
- of every HTML request. The same lines are printed to the terminal too.
188
- - **Web Vitals:** TTFB, FCP, LCP, CLS, INP, DCL, load, long task count and
189
- blocking time.
190
- - **Prewarm:** the progress of the warming round; it can be triggered manually
191
- from the panel, and individual paths can be retried.
192
- - **Process:** pid, Node version, uptime, RSS and heap usage.
193
- - **Version:** the installed JSkelet version compared against the `latest` tag
194
- on npm. When a newer release exists, the **Server** tab grows an `update` chip
195
- and a line that copies the upgrade command. The lookup runs once, 1.5 seconds
196
- after boot, is cached for six hours in `os.tmpdir()` and is skipped silently
197
- when the registry is unreachable. Set `JSKELET_VERSION_CHECK=0` to disable it.
198
-
199
- Warming requests (`user-agent: jskelet-prewarm`) are filtered out of both the
200
- terminal and the request list: hundreds of requests should not flood the view.
201
- Progress appears in the badge next to the bubble.
202
-
203
- ### Why state is written to `os.tmpdir()`
204
-
205
- If request and error records lived in process memory, history would be erased on
206
- every restart and the overlay would come up empty. So the records are carried
207
- across restarts in a file.
208
-
209
- The file is **not written into the project tree**: every write triggered the
210
- watcher and restarted the server, which set up a self-feeding loop (restart →
211
- startup warning → write → restart). Instead, the file is written to
212
- `os.tmpdir()/jskelet-devtools-<hash of the project root>.json`; thanks to the
213
- hash, multiple JSkelet projects on the same machine do not overwrite each
214
- other's records.
215
-
216
- The write happens after 300 ms of silence rather than on every request, and its
217
- failure does not stop the dev flow. At most 50 requests and 50 errors are kept.
218
-
219
- The panel's open/closed state, its active tab and the browser error log are kept
220
- in tab memory (`sessionStorage`), so after a reload the panel comes back with
221
- the same tab.
222
-
223
- ## Report page
224
-
225
- The bubble shows the current state; the report page produces a view of the whole
226
- site. The address:
227
-
228
- ```
229
- http://localhost:3000/__jskelet/dev/report
230
- ```
231
-
232
- (Or wherever `brand.devBasePath` points, if you changed it.)
233
-
234
- Its contents:
235
-
236
- - **Pages:** the Web Vitals measurements of every visited page, resource count
237
- and total bytes (broken down by type), island status (how many are ready, and
238
- their names), API calls made in the browser, and the size/duration/cache
239
- status of the SSR output. Pages that were never visited but were warmed are
240
- listed too: the SSR side is known, the client measurements stay empty.
241
- - **Server API calls:** outbound `fetch` calls made during SSR — URL, host,
242
- method, status, duration, bytes. `globalThis.fetch` is only wrapped in
243
- development; the production path is left untouched. Requests to our own server
244
- (warming, health check) do not count as API calls.
245
- - **Build output:** the raw/gzip/brotli size of every asset in the manifest, and
246
- chunk analysis from esbuild's metafile — the size of each output, which
247
- sources it is made of, which chunks it imports. Sources are reduced to
248
- readable groups (package name or parent folder), so the question "which
249
- library accounts for 40 kB of this chunk" can be answered.
250
- - **HTML cache:** entry count and a dump (key, bytes, status, whether it is
251
- stale, how many seconds until it expires, which encodings are stored).
252
- - **Prewarm:** the full result of the last round.
253
- - **Request and error logs.**
254
-
255
- Measurements live on the server, not in the browser tab; resetting is done from
256
- the server as well. Size calculations are not repeated unless the file changed.
257
-
258
- The report layer is only loaded in development and never enters the production
259
- output.
260
-
261
- ## Dev endpoints
262
-
263
- Under `brand.devBasePath` (default `/__jskelet/dev`):
264
-
265
- | Path | Method | Job |
266
- | --- | --- | --- |
267
- | `/overlay.js` | GET | The overlay script |
268
- | `/logo.png` | GET | The overlay logo |
269
- | `/ws` | GET (upgrade) | Live channel: statistics, live reload and CSS hot-swap events |
270
- | `/events` | GET | SSE: the fallback event stream, used only when WebSocket cannot be established |
271
- | `/stats` | GET | Current statistics; the data endpoint of that same fallback |
272
- | `/report` | GET | The report page (HTML) |
273
- | `/report.js` | GET | The report page's script |
274
- | `/report/data` | GET | The report's single data source (JSON) |
275
- | `/vitals` | POST | The measurement bundle sent by the overlay |
276
- | `/report/clear` | POST | Resets page measurements and server API records |
277
- | `/prewarm` | POST | Triggers warming manually. If the body has `paths`, only those paths; 409 if warming is already running. |
278
- | `/clear` | POST | Resets the request and error logs |
279
-
280
- All of these endpoints are mounted by `mountDevtools()` only when
281
- `NODE_ENV=development`; thanks to the dynamic import, nothing is loaded into the
282
- production process.
283
-
284
- ## Dev gate — `DEV_TOKEN`
285
-
286
- To hide an environment that is not public yet: while `DEV_TOKEN` is set,
287
- **every** request without the token gets a 404.
288
-
289
- ```bash
290
- DEV_TOKEN=a-long-random-string npm start
291
- ```
292
-
293
- Access:
294
-
295
- ```
296
- https://staging.example.com/?dev_token=a-long-random-string
297
- ```
298
-
299
- Behavior:
300
-
301
- - **404, not 403.** A 403 confirms the environment exists; a 404 acts as if it
302
- never did.
303
- - Once the token arrives as a query parameter, it is written to a cookie
304
- (`Path=/`, `SameSite=Lax`, 14 days), so sharing the link is enough. The cookie
305
- and parameter name is `brand.devTokenCookie` (default `dev_token`).
306
- - The **exact** paths in the `devGateBypass` list are open under all conditions.
307
- Default: `/api/healthcheck`, `/robots.txt`, `/sitemap.xml`,
308
- `/site.webmanifest`, `/favicon.ico`. If your health check lives at a different
309
- path, remember to add it to this list, otherwise your orchestrator will see a
310
- 404.
311
- - Without `DEV_TOKEN` the middleware is entirely disabled and costs nothing in
312
- production.
313
- - Since warming makes requests to its own server, it carries the token as a
314
- cookie; without it every page gets a 404 and the cache never fills up
315
- ([06-caching.md](./06-caching.md)).
316
-
317
- In the middleware chain the gate sits after `headers` and **before**
318
- `redirects`: an environment that is not public yet should not leak even its
319
- redirect rules.
320
-
321
- ## Differences between development and production
322
-
323
- | Topic | Development | Production |
324
- | --- | --- | --- |
325
- | EJS template cache | Off | On |
326
- | Manifest reading | On every request | Once |
327
- | Image manifest | On every call | Once |
328
- | Broken route module | Warn + skip | Throw |
329
- | Devtools and report | Mounted | Never loaded |
330
- | `globalThis.fetch` | Wrapped (measurement) | Untouched |
331
- | Prewarm concurrency | 1 | 4 |
332
- | Prewarm rate limit | 4 requests/second | Unlimited |
333
- | Prewarm delay | 3000 ms | 500 ms |
334
- | Missing icon warning | Emitted | Not emitted |
335
- | Precompress | Does not run in watch | Runs |
336
- | Image optimization | Does not run in watch | Runs |
337
-
338
- ## What's next
339
-
340
- - Details of the build steps: [08-build.md](./08-build.md)
341
- - Going to production: [10-deployment.md](./10-deployment.md)
342
- - Reading and clearing the cache: [06-caching.md](./06-caching.md)
1
+ # 09 — Development tools
2
+
3
+ This document explains what `jskelet dev` does and why it does it that way: how
4
+ the two child processes are managed, the shape of the terminal output, the
5
+ watched directories and the reason a custom watcher was written instead of using
6
+ `node --watch`, the distinction between CSS hot-swap and a full reload, the
7
+ devtools overlay opened with `Alt+D`, the detailed report page, and the dev gate
8
+ built on `DEV_TOKEN`. The build steps themselves are in
9
+ [08-build.md](./08-build.md).
10
+
11
+ ## The `jskelet dev` flow
12
+
13
+ The command manages two long-lived child processes in a single terminal:
14
+
15
+ ```
16
+ jskelet dev
17
+ ├─ build watch node src/build/build.mjs --watch
18
+ └─ server node [--env-file=.env] --import <register.mjs> src/start.mjs
19
+ ```
20
+
21
+ `NODE_ENV=development` is assigned here in a platform-independent way — no
22
+ `cross-env` needed. The child processes also receive `JSKELET_CHILD=1`
23
+ (suppresses the build banner) and, if a TTY is present, `JSKELET_COLOR=1`
24
+ (forces color on piped output).
25
+
26
+ Startup order: banner → build steps → server ready → `Ready` summary. The
27
+ summary is printed once both the build and the server are ready; otherwise it
28
+ got buried among the build lines arriving afterwards.
29
+
30
+ The server signals readiness with a single line inside `startServer`:
31
+
32
+ ```
33
+ jskelet → http://localhost:3000 (development)
34
+ ```
35
+
36
+ The shape of this line is a contract; the dev script parses it and prints the
37
+ summary line accordingly.
38
+
39
+ ### The shape of the terminal output
40
+
41
+ Child process output does not stream through as-is. There are two regions and
42
+ they never mix:
43
+
44
+ 1. **Startup:** banner, aligned build lines, `Ready` summary.
45
+ 2. **Runtime:** timestamped, single-line events (HTTP requests, CSS rebuild,
46
+ server restart).
47
+
48
+ Error stacks are turned into a framed box: because stack lines arrive in
49
+ fragments, they are collected after a short silence (60 ms), the error name and
50
+ message are parsed, the first three frames are shown, and the project root is
51
+ shortened to `.`. When you are developing your own framework, not losing the
52
+ error in the stream is the detail that genuinely makes a difference.
53
+
54
+ Color carries meaning: `✓` green, `✖` red, `⚠` yellow, `↻` cyan; durations and
55
+ paths gray. No decorative color is used. If `NO_COLOR` is set, no color is used
56
+ at all.
57
+
58
+ `Ctrl+C` (SIGINT/SIGTERM) shuts down all child processes. If a child exits with
59
+ a non-zero code (other than an expected restart), an error is printed and the
60
+ dev process exits too.
61
+
62
+ ## Watch directories
63
+
64
+ Server restarts are managed by the framework's own watcher.
65
+
66
+ ```js
67
+ WATCH_DIRS = [
68
+ config.dirs.routes,
69
+ config.dirs.views,
70
+ <root>/lib,
71
+ ...config.watch, // jskelet.config.mjs → watch
72
+ ]
73
+ ```
74
+
75
+ The `jskelet.config.mjs` file itself is watched as well: when the config
76
+ changes, both the server and the build must come up with the new settings.
77
+
78
+ Watched extensions: `.js`, `.mjs`, `.json`, `.ejs`.
79
+
80
+ `views` is watched too, because most components live in
81
+ `views/components/**.js` and, since those modules are imported into the server
82
+ once, changes made without a restart never reached the browser (that "I edited
83
+ the template and nothing changed" feeling comes from here).
84
+
85
+ `client/` and `styles/` are **not** in this list: esbuild and the CSS watchers
86
+ handle them on their own ([08-build.md](./08-build.md)).
87
+
88
+ If a directory cannot be watched, a warning is printed and no automatic restart
89
+ happens for that directory; everything else keeps working.
90
+
91
+ ### Why `node --watch` was not used
92
+
93
+ Even when given `--watch-path`, Node watched the project root in this setup.
94
+ Whenever build output (`public/assets`, `manifest.json`) or the dev tooling log
95
+ was written, the server restarted for nothing — in fact a self-feeding loop was
96
+ set up: restart → startup warning → write → restart.
97
+
98
+ The custom watcher does three things:
99
+
100
+ 1. **Watches only server sources.**
101
+ 2. **Coalesces changes** (250 ms) and reports which files changed.
102
+ 3. **Filters out phantom events.** On Windows, `fs.watch` can emit events for a
103
+ file's neighbors when it is written; without comparing `mtime`, a single save
104
+ turned into two restarts. Current timestamps are read up front at startup, so
105
+ the first phantom event is filtered out as well.
106
+
107
+ The restart line shows the changed file, or how many changed:
108
+
109
+ ```
110
+ 21:04:12 ↻ server restarting… routes/10-pages.mjs
111
+ 21:04:12 server restarted 412ms
112
+ ```
113
+
114
+ If `JSKELET_VERBOSE=1` is set, all files are listed when more than one changed.
115
+
116
+ ## CSS hot-swap and full reload
117
+
118
+ The dev server watches `.jskelet/manifest.json` and broadcasts events to the
119
+ browser over the live channel (`<devBasePath>/ws`). Since the manifest is
120
+ rewritten on every build round, change detection is done through the manifest.
121
+
122
+ | What changed | Behavior |
123
+ | --- | --- |
124
+ | Only `app.css` | **CSS hot-swap:** the stylesheet is swapped, the page is not reloaded. State and scroll position are preserved. |
125
+ | `main.js`, the sprite, another asset, or more than one key | **Full reload** |
126
+
127
+ In both cases the HTML cache is cleared first: the stored HTML carries the old
128
+ hashed asset URLs and, if not cleared, the page keeps asking for a deleted file.
129
+
130
+ Manifest events are coalesced over 120 ms. If watching is not supported, live
131
+ reload is disabled and everything else keeps working.
132
+
133
+ When the server restarts, the overlay figures it out from the **boot id**: every
134
+ process broadcasts a unique `boot` value, the overlay sees the change, shows the
135
+ "restarted" note and does not reset its own state.
136
+
137
+ ## The live channel
138
+
139
+ Everything the overlay shows — statistics, live reload and CSS hot-swap events —
140
+ arrives over a single WebSocket (`<devBasePath>/ws`). The panel used to poll for
141
+ statistics every two seconds, so every open tab kept hitting the server even
142
+ while the panel was closed. Now the server pushes as things change: when a
143
+ request or an error is recorded (coalesced over 120 ms), and every two seconds so
144
+ the time-based fields (uptime, memory, the prewarm counter) stay fresh. The
145
+ heartbeat is deliberately independent of prewarming: if the channel's tempo
146
+ followed a background job, the panel would be tied to that job's rhythm. Nothing
147
+ is computed when no panel is connected.
148
+
149
+ The handshake happens on the HTTP `upgrade` event, and that event never reaches
150
+ the middleware chain, so the channel is attached straight to the server after
151
+ `listen` (`attachDevSocket`). The server side pulls in no dependency such as
152
+ `ws`: all it needs is to write server-to-client text frames and to answer the
153
+ client's ping/close frames.
154
+
155
+ If the socket opens and later drops — that is, the server is restarting — it
156
+ reconnects every half second and the indicator reads "server restarting…" in the
157
+ meantime. If it never opens, it is retried four times with a widening gap: the
158
+ page may have been loaded during the server's restart window, and a single
159
+ failure does not mean the channel is unavailable. If those attempts fail too (a
160
+ proxy in between may not pass WebSocket through), the overlay falls back to the
161
+ old path: the `/events` SSE stream plus polling `/stats`.
162
+
163
+ ## Devtools overlay
164
+
165
+ A floating bubble in the bottom right; opened with `Alt+D`, closed with `Esc` or
166
+ by clicking the backdrop. It is only emitted by the layout when
167
+ `NODE_ENV=development`:
168
+
169
+ ```ejs
170
+ <% if (devtools) { %>
171
+ <script type="module" src="<%= devBasePath %>/overlay.js"></script>
172
+ <% } %>
173
+ ```
174
+
175
+ The overlay file is **not part of the build.** The server serves it raw from the
176
+ framework package; that is why there is no bundler involved, it works as a
177
+ single file, and it adds nothing to the production output. The entire UI lives
178
+ inside a shadow DOM and does not mix with the page's CSS.
179
+
180
+ What it shows:
181
+
182
+ - **Errors:** browser-side JS errors, resource loading errors
183
+ (`img`/`script`/`link`), and the server's `console.error` / `console.warn`
184
+ output. On the server side `console` is wrapped so warnings do not get lost in
185
+ the terminal.
186
+ - **Requests:** the method, path, status, duration and `X-JSkelet-Cache` value
187
+ of every HTML request. The same lines are printed to the terminal too.
188
+ - **Web Vitals:** TTFB, FCP, LCP, CLS, INP, DCL, load, long task count and
189
+ blocking time.
190
+ - **Prewarm:** the progress of the warming round; it can be triggered manually
191
+ from the panel, and individual paths can be retried.
192
+ - **Process:** pid, Node version, uptime, RSS and heap usage.
193
+ - **Version:** the installed JSkelet version compared against the `latest` tag
194
+ on npm. When a newer release exists, the **Server** tab grows an `update` chip
195
+ and a line that copies the upgrade command. The lookup runs once, 1.5 seconds
196
+ after boot, is cached for six hours in `os.tmpdir()` and is skipped silently
197
+ when the registry is unreachable. Set `JSKELET_VERSION_CHECK=0` to disable it.
198
+
199
+ Warming requests (`user-agent: jskelet-prewarm`) are filtered out of both the
200
+ terminal and the request list: hundreds of requests should not flood the view.
201
+ Progress appears in the badge next to the bubble.
202
+
203
+ ### Why state is written to `os.tmpdir()`
204
+
205
+ If request and error records lived in process memory, history would be erased on
206
+ every restart and the overlay would come up empty. So the records are carried
207
+ across restarts in a file.
208
+
209
+ The file is **not written into the project tree**: every write triggered the
210
+ watcher and restarted the server, which set up a self-feeding loop (restart →
211
+ startup warning → write → restart). Instead, the file is written to
212
+ `os.tmpdir()/jskelet-devtools-<hash of the project root>.json`; thanks to the
213
+ hash, multiple JSkelet projects on the same machine do not overwrite each
214
+ other's records.
215
+
216
+ The write happens after 300 ms of silence rather than on every request, and its
217
+ failure does not stop the dev flow. At most 50 requests and 50 errors are kept.
218
+
219
+ The panel's open/closed state, its active tab and the browser error log are kept
220
+ in tab memory (`sessionStorage`), so after a reload the panel comes back with
221
+ the same tab.
222
+
223
+ ## Report page
224
+
225
+ The bubble shows the current state; the report page produces a view of the whole
226
+ site. The address:
227
+
228
+ ```
229
+ http://localhost:3000/__jskelet/dev/report
230
+ ```
231
+
232
+ (Or wherever `brand.devBasePath` points, if you changed it.)
233
+
234
+ Its contents:
235
+
236
+ - **Pages:** the Web Vitals measurements of every visited page, resource count
237
+ and total bytes (broken down by type), island status (how many are ready, and
238
+ their names), API calls made in the browser, and the size/duration/cache
239
+ status of the SSR output. Pages that were never visited but were warmed are
240
+ listed too: the SSR side is known, the client measurements stay empty.
241
+ - **Server API calls:** outbound `fetch` calls made during SSR — URL, host,
242
+ method, status, duration, bytes. `globalThis.fetch` is only wrapped in
243
+ development; the production path is left untouched. Requests to our own server
244
+ (warming, health check) do not count as API calls.
245
+ - **Build output:** the raw/gzip/brotli size of every asset in the manifest, and
246
+ chunk analysis from esbuild's metafile — the size of each output, which
247
+ sources it is made of, which chunks it imports. Sources are reduced to
248
+ readable groups (package name or parent folder), so the question "which
249
+ library accounts for 40 kB of this chunk" can be answered.
250
+ - **HTML cache:** entry count and a dump (key, bytes, status, whether it is
251
+ stale, how many seconds until it expires, which encodings are stored).
252
+ - **Prewarm:** the full result of the last round.
253
+ - **Request and error logs.**
254
+
255
+ Measurements live on the server, not in the browser tab; resetting is done from
256
+ the server as well. Size calculations are not repeated unless the file changed.
257
+
258
+ The report layer is only loaded in development and never enters the production
259
+ output.
260
+
261
+ ## Dev endpoints
262
+
263
+ Under `brand.devBasePath` (default `/__jskelet/dev`):
264
+
265
+ | Path | Method | Job |
266
+ | --- | --- | --- |
267
+ | `/overlay.js` | GET | The overlay script |
268
+ | `/logo.png` | GET | The overlay logo |
269
+ | `/ws` | GET (upgrade) | Live channel: statistics, live reload and CSS hot-swap events |
270
+ | `/events` | GET | SSE: the fallback event stream, used only when WebSocket cannot be established |
271
+ | `/stats` | GET | Current statistics; the data endpoint of that same fallback |
272
+ | `/report` | GET | The report page (HTML) |
273
+ | `/report.js` | GET | The report page's script |
274
+ | `/report/data` | GET | The report's single data source (JSON) |
275
+ | `/vitals` | POST | The measurement bundle sent by the overlay |
276
+ | `/report/clear` | POST | Resets page measurements and server API records |
277
+ | `/prewarm` | POST | Triggers warming manually. If the body has `paths`, only those paths; 409 if warming is already running. |
278
+ | `/clear` | POST | Resets the request and error logs |
279
+
280
+ All of these endpoints are mounted by `mountDevtools()` only when
281
+ `NODE_ENV=development`; thanks to the dynamic import, nothing is loaded into the
282
+ production process.
283
+
284
+ ## Dev gate — `DEV_TOKEN`
285
+
286
+ To hide an environment that is not public yet: while `DEV_TOKEN` is set,
287
+ **every** request without the token gets a 404.
288
+
289
+ ```bash
290
+ DEV_TOKEN=a-long-random-string npm start
291
+ ```
292
+
293
+ Access:
294
+
295
+ ```
296
+ https://staging.example.com/?dev_token=a-long-random-string
297
+ ```
298
+
299
+ Behavior:
300
+
301
+ - **404, not 403.** A 403 confirms the environment exists; a 404 acts as if it
302
+ never did.
303
+ - Once the token arrives as a query parameter, it is written to a cookie
304
+ (`Path=/`, `SameSite=Lax`, 14 days), so sharing the link is enough. The cookie
305
+ and parameter name is `brand.devTokenCookie` (default `dev_token`).
306
+ - The **exact** paths in the `devGateBypass` list are open under all conditions.
307
+ Default: `/api/healthcheck`, `/robots.txt`, `/sitemap.xml`,
308
+ `/site.webmanifest`, `/favicon.ico`. If your health check lives at a different
309
+ path, remember to add it to this list, otherwise your orchestrator will see a
310
+ 404.
311
+ - Without `DEV_TOKEN` the middleware is entirely disabled and costs nothing in
312
+ production.
313
+ - Since warming makes requests to its own server, it carries the token as a
314
+ cookie; without it every page gets a 404 and the cache never fills up
315
+ ([06-caching.md](./06-caching.md)).
316
+
317
+ In the middleware chain the gate sits after `headers` and **before**
318
+ `redirects`: an environment that is not public yet should not leak even its
319
+ redirect rules.
320
+
321
+ ## Differences between development and production
322
+
323
+ | Topic | Development | Production |
324
+ | --- | --- | --- |
325
+ | EJS template cache | Off | On |
326
+ | Manifest reading | On every request | Once |
327
+ | Image manifest | On every call | Once |
328
+ | Broken route module | Warn + skip | Throw |
329
+ | Devtools and report | Mounted | Never loaded |
330
+ | `globalThis.fetch` | Wrapped (measurement) | Untouched |
331
+ | Prewarm concurrency | 1 | 4 |
332
+ | Prewarm rate limit | 4 requests/second | Unlimited |
333
+ | Prewarm delay | 3000 ms | 500 ms |
334
+ | Missing icon warning | Emitted | Not emitted |
335
+ | Precompress | Does not run in watch | Runs |
336
+ | Image optimization | Does not run in watch | Runs |
337
+
338
+ ## What's next
339
+
340
+ - Details of the build steps: [08-build.md](./08-build.md)
341
+ - Going to production: [10-deployment.md](./10-deployment.md)
342
+ - Reading and clearing the cache: [06-caching.md](./06-caching.md)