@dokebi/template-inertia 0.4.15 → 0.4.17

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.
package/AI.md CHANGED
@@ -265,6 +265,7 @@ A module keeps its Vue pages in its own views folder (@dokebi/module AI.md §4a)
265
265
  - The same host check as the dev routes runs first (loopback names, IPs, @dokebi/app domains, `devHosts`; others get `403`). Behind a tunnel set dokebi-web `trustProxy` so the tunnel's host is seen (@dokebi/web AI.md §2.6). A host that cannot reach Vite is logged at error `INERTIA_VITE_DEV_FAILED` once per window.
266
266
  - Vite's token check (protection against cross-site WebSocket hijacking) stays. Upgrades that are not Vite's subprotocols are cut. Closing the host closes its port and connections, and a host whose Vite fails to start closes its port.
267
267
  - **Late file events are no edits (D-0045 addendum 4, 0.4.3)**: on macOS, FSEvents also delivers events for writes made before the watch started (`dokebi init`, `dokebi add`, `npm install`, a checkout), minutes late when fseventsd is busy. The host records each module file's state (device, inode, size, modification time) when Vite first loads it in an environment (before Vite reads it) and the `vite.config.*` files before Vite starts, and drops a watcher `change`, `add` or `unlink` whose file state equals every recorded one, so such an event invalidates nothing, sends no reload or update (a reload was buffered for the first page that connected) and restarts nothing. Any other event goes on at once. Files Vite never loaded through its plugin pipeline are not filtered. A save that changes nothing but the modification time is still an edit.
268
+ - **Edits the watcher missed are read anyway (D-0283, 0.4.17)**: FSEvents can report an edit a minute late, or not before the next page load (a `git pull` that restarted the app while the host kept serving the old `.vue`, and the page broke on its new locales). The same records serve a comparison: when an app process connects (`trigger` `client`), before a server render (`ssr`) and when a page loads `<base>@vite/client` (`page`), the host compares every file Vite read with its state on disk. A file that changed is invalidated in every module graph at once and handed to Vite as the watcher's `change` (`unlink` when it is gone), so plugins, HMR updates and reloads run as for the watcher's own event, and the request that follows gets the file as it is; the watcher's late event then changes nothing and is dropped. Each find is info `INERTIA_VITE_WATCH_MISSED` (`trigger`, `files`, `sample` of at most 5, `compared`, `ms`). Open pages still get their immediate HMR from the watcher; the comparison guarantees that a reloaded page or a restarted app gets the files on disk.
268
269
  - **Pre-bundled dependencies that load (D-0128, 0.4.4)**: Vite keeps what its dependency optimizer wrote (each dependency's file under `node_modules/.vite/deps` and the `?v=` hash pages get) in memory, and at start trusts `_metadata.json` whenever the lockfile and config hashes match; it never looks at the files again, and it answers a file that is gone with 504 "Outdated Optimize Dep". A folder changed under it (two Vite optimizers on one folder — two `dokebi dev` sessions, a host still closing while the next starts — or a reinstall) therefore left every page blank through app restarts and dev restarts, until `rm -rf node_modules/.vite`. The host now compares its optimizer's files and metadata hashes with the folder when it starts, whenever an app process connects (every app or worker start) and after it answered a 504; when they differ it removes the deps folder and restarts its Vite (warn `INERTIA_VITE_DEPS_STALE` with `trigger` `start`, `client` or `outdated`, `drift` `missing` or `rewritten`, and a sample of the missing files), and the app process is answered after the repair. Open pages reconnect HMR and reload; a new page load gets the new hashes. At most one repair per `devDepsRepairIntervalMs`; faster changes are reported with warn `INERTIA_VITE_DEPS_CHANGING`. The host records its pid in `<cacheDir>/.dokebi/vite-host.json` and warns `INERTIA_VITE_HOST_SHARED` when another live host recorded itself there (stop one of the two `dokebi dev`). App processes pass Vite's status text on (`504 Outdated Optimize Dep`).
269
270
  - SSR: the host runs `ssrLoadModule(<source entry>)` and `render(page)` per request. The app process asks on connections of its own (a render that waits never holds the connections module requests are forwarded on) and cuts the request at `ssr.timeoutMs`, so renders that never finish do not pile up on the host. The source entry is `ssr.entry` (for example `src/apps/admin/views/ssr.js`), otherwise `ssr.js` in the folder of `vite.entry`. When neither exists the start fails (`SSR_ENTRY_MISSING`).
270
271
  - Without Vite in the project the start fails (`VITE_DEV_MISSING`; `npm install --save-dev vite`).
@@ -275,7 +276,7 @@ A module keeps its Vue pages in its own views folder (@dokebi/module AI.md §4a)
275
276
  - **Content-Security-Policy** (dokebi-web security headers, [D-0149](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0149-security-headers.md)): Vite's HMR socket connects to the page's own origin and `<base>` (`ws://localhost:<app port>/build/`, or `wss://<public host>/build/` through a tunnel; §5.1), which `connect-src 'self'` allows (CSP3), so nothing is added for it (0.4.13, [D-0249](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0249-dev-hmr-csp-self.md); before, `ws://*:* wss://*:*`). Only a project whose Vite config sends the socket elsewhere — `server.ws` (or the older `server.hmr`) with `host`, `protocol`, `port` or `clientPort` — gets that origin added in development, exactly: the scheme (both `ws:` and `wss:` when the page decides), the host (`*` when it is the page's) and the port (`clientPort`, else `port`; `*` when it is the page's), e.g. `clientPort: 5174` → `ws://*:5174 wss://*:5174`. The dev host reports it when an app process connects (debug `Vite HMR socket origin added to connect-src`). In development this plugin also adds `worker-src 'self' blob:` for the `blob:` SharedWorker Vite's client pings a restarted dev host from (without it the page never reconnects); production adds nothing. Pages load their scripts from `'self'` (the page data is JSON, not a script); keep `'unsafe-inline'` in a policy of your own when a Vite framework plugin adds inline scripts in development.
276
277
  - **Nonce policy** (0.4.12, [D-0247](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0247-csp-nonce.md); web 0.4.19 `contentSecurityPolicy: { nonce: true, styleNonce?: true }`): the first HTML — plain, sealed or server rendered, built or in development — writes the request's nonce (`web.cspNonce(request)`) on every module script and on the boot fallback's `<style>` and `<script>`, and adds `<meta property="csp-nonce" nonce="…">`, which Vite's client (development `<style>` of imported CSS) and its preload helper read. The page data block (`type="application/json"`) is never run and needs none. What the entry imports or creates is trusted by `'strict-dynamic'`. Inertia's own progress bar and error dialog styles take the nonce from `createInertiaApp({ nonce: document.querySelector('meta[property="csp-nonce"]')?.nonce || undefined })` (the generated `views/app.js` does; add it to an older entry before `styleNonce`). Checked in Chromium: a built Vue app with SSR and navigation (example 24), a seal host with server-rendered public pages and a framed page (example 80), and the Vite dev page with imported CSS, a dynamic import and the HMR client (`tests/dev-files.test.ts`).
277
278
  - The host answers a Vite middleware error with 500 (error log `INERTIA_VITE_HOST_FAILED`); 404 only when Vite has nothing for the URL. A host that does not answer is a 502 with error `INERTIA_VITE_DEV_FAILED` (once per 10 seconds, `repeated`); a host that cannot be reached at start is `VITE_DEV_UNAVAILABLE`.
278
- - Debug logs (D-0047): `Inertia settings resolved`, `Vite dev server ready` (ms, `shared`), `Vite dev route registered`, `Vite manifest read`, `Inertia SSR bundle loaded`, `Inertia SSR render deadline` (`timeoutMs`), `Inertia SSR render finished after its deadline` (a cut render that ended later), `Inertia SSR renders the source through Vite` (the source used), `Inertia scope created`, `Vite dev uses the host of dokebi dev`, `Vite dev starts a host in this process`, `Vite dev host connected` / `disconnected`; in the host `Vite dev host listening` (`port`, `hosts`), `Vite dev host started` (ms), `Vite dev host closed` (`connections`), `Vite dev host listens on IPv4 loopback only`, `Vite dev host port is taken on ::1, trying another`, `Vite dev host filters watcher events of unchanged files`, `Vite dev host ignored an event for a file that did not change` (the first and every 100th, `dropped`); `Vite dev host settings read` (`depsRepairIntervalMs`), `Vite dev host recorded itself in the cache folder` (`shared`), `Vite dev host re-optimized its dependencies` (`trigger`, `ms`, `repairs`), and per check only when debug is on `Vite dev host dependency cache matches` (`trigger`, `files`); per real file event, only when debug is on, `Vite dev host passes a file change`. A file whose state cannot be read is warn `INERTIA_VITE_WATCH_STAT_FAILED` (the event goes on). Per request, only when debug is on: `Vite dev route refused a host`, `Vite dev host closed an upgrade that is not Vite`, `Vite dev host refused a request without its key`, `Vite dev request ended by the browser`. Host listener and request failures are error `INERTIA_VITE_HOST_FAILED`.
279
+ - Debug logs (D-0047): `Inertia settings resolved`, `Vite dev server ready` (ms, `shared`), `Vite dev route registered`, `Vite manifest read`, `Inertia SSR bundle loaded`, `Inertia SSR render deadline` (`timeoutMs`), `Inertia SSR render finished after its deadline` (a cut render that ended later), `Inertia SSR renders the source through Vite` (the source used), `Inertia scope created`, `Vite dev uses the host of dokebi dev`, `Vite dev starts a host in this process`, `Vite dev host connected` / `disconnected`; in the host `Vite dev host listening` (`port`, `hosts`), `Vite dev host started` (ms), `Vite dev host closed` (`connections`), `Vite dev host listens on IPv4 loopback only`, `Vite dev host port is taken on ::1, trying another`, `Vite dev host filters watcher events of unchanged files`, `Vite dev host ignored an event for a file that did not change` (the first and every 100th, `dropped`); `Vite dev host settings read` (`depsRepairIntervalMs`), `Vite dev host recorded itself in the cache folder` (`shared`), `Vite dev host re-optimized its dependencies` (`trigger`, `ms`, `repairs`), and per check only when debug is on `Vite dev host dependency cache matches` (`trigger`, `files`); per real file event, only when debug is on, `Vite dev host passes a file change`; per comparison that found nothing, only when debug is on, `Vite dev host compared the files Vite read with the disk` (`trigger`, `compared`, `ms`). A file whose state cannot be read is warn `INERTIA_VITE_WATCH_STAT_FAILED` (the event goes on). Per request, only when debug is on: `Vite dev route refused a host`, `Vite dev host closed an upgrade that is not Vite`, `Vite dev host refused a request without its key`, `Vite dev request ended by the browser`. Host listener and request failures are error `INERTIA_VITE_HOST_FAILED`.
279
280
 
280
281
  ## 5.2 What the dev server serves (D-0129)
281
282
 
package/dist/index.js CHANGED
@@ -1065,7 +1065,7 @@ class InertiaRuntime {
1065
1065
  }
1066
1066
  export const createPlugin = definePlugin({
1067
1067
  name: 'dokebi-template-inertia',
1068
- version: '0.4.15',
1068
+ version: '0.4.17',
1069
1069
  core: '^0.4.2',
1070
1070
  options,
1071
1071
  entry: parseEntry,
package/dist/vite-dev.js CHANGED
@@ -283,6 +283,9 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
283
283
  const browser = request.headers[DEV_CLIENT_HEADER] !== undefined;
284
284
  const cache = deps;
285
285
  if (path === HOST_PATH && !browser) {
286
+ // D-0283: files changed under Vite that the watcher has not reported (a `git pull` that
287
+ // restarted the app) are read again before the app's pages are served.
288
+ served.sweep('client');
286
289
  // An app process connects (it started or restarted): it is answered once the pre-bundled
287
290
  // dependencies match what Vite serves, so its pages get hashes that load (D-0128).
288
291
  void (cache ? cache.ensure('client').catch(report('deps')) : Promise.resolve()).then(() => sendJson(response, 200, {
@@ -292,9 +295,15 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
292
295
  hmr: hmrSources(server.config.server.ws),
293
296
  }));
294
297
  }
295
- else if (path === SSR_PATH && request.method === 'POST' && !browser)
298
+ else if (path === SSR_PATH && request.method === 'POST' && !browser) {
299
+ // D-0283: the server render reads the files as they are on disk.
300
+ served.sweep('ssr');
296
301
  void ssr(request, response, server);
302
+ }
297
303
  else {
304
+ // D-0283: a page loads Vite's client first; its modules then come as they are on disk.
305
+ if (browser && path === `${server.config.base}@vite/client`)
306
+ served.sweep('page');
298
307
  // Vite answers 504 for a pre-bundled file it cannot serve: if the folder changed under it,
299
308
  // re-optimize so the next page load gets files that exist (D-0128).
300
309
  if (cache)
@@ -305,7 +314,8 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
305
314
  server.middlewares(request, response, (error) => viteFallthrough(response, error, report('vite')));
306
315
  }
307
316
  });
308
- // Events FSEvents still holds from before the start are no edits (D-0045 addendum 4).
317
+ // Events FSEvents still holds from before the start are no edits (D-0045 addendum 4); the same
318
+ // records find the edits the watcher has not reported (D-0283).
309
319
  const served = new ServedFiles(logger);
310
320
  served.recordConfig(root);
311
321
  try {
@@ -4,6 +4,20 @@ interface WatchedServer {
4
4
  watcher: {
5
5
  emit(event: string | symbol, ...args: unknown[]): boolean;
6
6
  };
7
+ /** Vite's environments (client, ssr): their module graphs invalidate a file at once. */
8
+ environments?: Record<string, {
9
+ moduleGraph?: {
10
+ onFileChange?(file: string): void;
11
+ };
12
+ }>;
13
+ }
14
+ /** What a sweep found and handed to Vite. */
15
+ export interface SweepResult {
16
+ /** Files that changed or went away since Vite read them. */
17
+ readonly files: readonly string[];
18
+ /** Recorded files compared. */
19
+ readonly compared: number;
20
+ readonly ms: number;
7
21
  }
8
22
  /** The plugin as Vite takes it (this package does not depend on Vite's types). */
9
23
  export interface StaleEventPlugin {
@@ -34,6 +48,15 @@ export declare class ServedFiles {
34
48
  recordConfig(root: string): void;
35
49
  /** Whether an event for `file` is no change (dropped); a change updates the records. */
36
50
  stale(file: string, event: string): boolean;
51
+ /** The Vite whose module graphs and watcher a sweep uses (Vite's `configureServer`). */
52
+ attach(server: WatchedServer): void;
53
+ /**
54
+ * Hands Vite the recorded files whose state changed since it read them and that the watcher has
55
+ * not reported yet: each is invalidated in every module graph at once (the next request reads
56
+ * it) and emitted on the watcher as `change` (`unlink` when it is gone), so plugins, HMR and
57
+ * reloads run as for the watcher's own event. `trigger` names what asked (logged).
58
+ */
59
+ sweep(trigger: string): SweepResult;
37
60
  }
38
61
  /**
39
62
  * The Vite plugin of the host: records module files on their first load (`enforce: 'pre'`, so the
@@ -12,6 +12,15 @@
12
12
  // as it is. Any other event goes on and the records take the new state, so a real edit is handled
13
13
  // at once, as without the filter. Files Vite never loaded through the plugin pipeline (no record)
14
14
  // are never filtered.
15
+ //
16
+ // The records also catch what the watcher never reported (D-0283). FSEvents can deliver an edit a
17
+ // minute late, or not before the next page load (shop dev, 2026-11-26: after a `git pull` the
18
+ // host served the old EliteCounters.vue for more than two minutes and the page broke on its new
19
+ // locales). `sweep()` compares every recorded file with its state now; a file that changed is
20
+ // invalidated in Vite's module graphs at once and handed to Vite as the watcher's event (plugins,
21
+ // HMR, reloads), so the request that follows gets the file as it is. The host sweeps when an app
22
+ // process connects, before a server render and when a page loads Vite's client. The late event of
23
+ // the watcher then finds nothing changed and is dropped.
15
24
  import { statSync } from 'node:fs';
16
25
  import { isAbsolute, join, resolve } from 'node:path';
17
26
  import { diagnostics } from '@dokebi/core/libraries';
@@ -29,6 +38,8 @@ const CONFIG_FILES = [
29
38
  const STARTUP = 'startup';
30
39
  /** Watcher events that make Vite reload, update or restart. */
31
40
  const FILE_EVENTS = new Set(['change', 'add', 'unlink']);
41
+ /** Files named in a log line at most. */
42
+ const SWEEP_SAMPLE = 5;
32
43
  /** One path form for Vite's ids (`/`) and chokidar's paths (native). */
33
44
  const normal = (path) => resolve(path).replaceAll('\\', '/');
34
45
  /** The file of a module id, or undefined for virtual and dependency modules. */
@@ -50,6 +61,10 @@ export class ServedFiles {
50
61
  #logger;
51
62
  #dropped = 0;
52
63
  #statFailures = 0;
64
+ /** The Vite the records belong to (the last one configured: a restart configures a new one). */
65
+ #server;
66
+ /** A file a sweep hands to Vite: its event passes the filter although the records match. */
67
+ #forwarding;
53
68
  #warnedAt = Number.NEGATIVE_INFINITY;
54
69
  constructor(logger) {
55
70
  this.#logger = logger;
@@ -81,6 +96,8 @@ export class ServedFiles {
81
96
  /** Whether an event for `file` is no change (dropped); a change updates the records. */
82
97
  stale(file, event) {
83
98
  const path = normal(file);
99
+ if (path === this.#forwarding)
100
+ return false;
84
101
  const records = this.#records.get(path);
85
102
  if (!records)
86
103
  return false;
@@ -108,6 +125,75 @@ export class ServedFiles {
108
125
  });
109
126
  return false;
110
127
  }
128
+ /** The Vite whose module graphs and watcher a sweep uses (Vite's `configureServer`). */
129
+ attach(server) {
130
+ this.#server = server;
131
+ }
132
+ /**
133
+ * Hands Vite the recorded files whose state changed since it read them and that the watcher has
134
+ * not reported yet: each is invalidated in every module graph at once (the next request reads
135
+ * it) and emitted on the watcher as `change` (`unlink` when it is gone), so plugins, HMR and
136
+ * reloads run as for the watcher's own event. `trigger` names what asked (logged).
137
+ */
138
+ sweep(trigger) {
139
+ const begin = performance.now();
140
+ const server = this.#server;
141
+ if (!server)
142
+ return { files: [], compared: 0, ms: 0 };
143
+ const missed = [];
144
+ for (const [path, records] of this.#records) {
145
+ const state = this.#read(path);
146
+ // Unreadable: logged by #read; the watcher's own event decides.
147
+ if (state === null)
148
+ continue;
149
+ if (state !== undefined && [...records.values()].every((r) => r === state))
150
+ continue;
151
+ missed.push({ path, event: state === undefined ? 'unlink' : 'change' });
152
+ }
153
+ const compared = this.#records.size;
154
+ for (const { path, event } of missed) {
155
+ for (const environment of Object.values(server.environments ?? {}))
156
+ environment.moduleGraph?.onFileChange?.(path);
157
+ this.#forwarding = path;
158
+ try {
159
+ // Through the wrapped emit (other plugins may wrap it too); stale() lets it pass and
160
+ // moves the records to the state now.
161
+ server.watcher.emit(event, path);
162
+ server.watcher.emit('all', event, path);
163
+ }
164
+ finally {
165
+ this.#forwarding = undefined;
166
+ }
167
+ // The pass above moved the records; a gone file has none.
168
+ if (event === 'unlink')
169
+ this.#records.delete(path);
170
+ else {
171
+ const state = this.#read(path);
172
+ const records = this.#records.get(path);
173
+ if (records && state)
174
+ for (const environment of records.keys())
175
+ records.set(environment, state);
176
+ }
177
+ }
178
+ const ms = Math.round((performance.now() - begin) * 10) / 10;
179
+ const files = missed.map((item) => item.path);
180
+ if (files.length > 0)
181
+ this.#logger.log('info', 'Vite dev host found files changed on disk that its watcher had not reported; Vite reads them again', {
182
+ code: 'INERTIA_VITE_WATCH_MISSED',
183
+ trigger,
184
+ files: files.length,
185
+ sample: files.slice(0, SWEEP_SAMPLE),
186
+ compared,
187
+ ms,
188
+ });
189
+ else if (this.#logger.isEnabled('debug'))
190
+ this.#logger.log('debug', 'Vite dev host compared the files Vite read with the disk', {
191
+ trigger,
192
+ compared,
193
+ ms,
194
+ });
195
+ return { files, compared, ms };
196
+ }
111
197
  /** The state, undefined when absent, null when it cannot be read (logged; the event goes on). */
112
198
  #read(path) {
113
199
  try {
@@ -119,7 +205,7 @@ export class ServedFiles {
119
205
  return undefined;
120
206
  // Once per 10 seconds, with how many failed since the last warning (D-0047).
121
207
  this.#statFailures++;
122
- const now = Date.now();
208
+ const now = performance.now(); // D-0254: the warn interval is monotonic.
123
209
  if (now - this.#warnedAt >= 10_000) {
124
210
  this.#logger.log('warn', 'Vite dev host could not read the state of a file', {
125
211
  code: 'INERTIA_VITE_WATCH_STAT_FAILED',
@@ -144,6 +230,7 @@ export function staleEventFilter(files, logger) {
144
230
  name: 'dokebi:stale-watch-events',
145
231
  enforce: 'pre',
146
232
  configureServer(server) {
233
+ files.attach(server);
147
234
  const watcher = server.watcher;
148
235
  const emit = watcher.emit;
149
236
  // chokidar emits the event, then `all` with the same arguments: one decision for both.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.15",
3
+ "version": "0.4.17",
4
4
  "description": "Inertia v3 server adapter for Dokebi: page JSON, Vite manifest assets and optional server-side rendering.",
5
5
  "keywords": [
6
6
  "dokebi",
@@ -143,10 +143,10 @@
143
143
  ]
144
144
  },
145
145
  "devDependencies": {
146
- "@dokebi/i18n": "0.2.8",
147
- "@dokebi/session": "0.4.9",
148
- "@dokebi/web": "0.4.21",
149
- "@dokebi/websocket": "0.4.9",
146
+ "@dokebi/i18n": "0.2.9",
147
+ "@dokebi/session": "0.4.10",
148
+ "@dokebi/web": "0.4.25",
149
+ "@dokebi/websocket": "0.4.10",
150
150
  "@dokebi/websocket-hub": "0.4.5",
151
151
  "@inertiajs/vue3": "3.9.1",
152
152
  "vue": "3.5.44"