@dokebi/template-inertia 0.4.16 → 0.4.18
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 +3 -1
- package/dist/index.js +43 -1
- package/dist/vite-dev.d.ts +7 -0
- package/dist/vite-dev.js +23 -2
- package/dist/vite-watch.d.ts +23 -0
- package/dist/vite-watch.js +87 -0
- package/package.json +5 -5
package/AI.md
CHANGED
|
@@ -265,6 +265,8 @@ 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
|
+
- **The drain ends the open HMR sockets (0.4.18, [D-0288](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0288-dev-kills-after-the-apps-stop-deadlines.md))**: the Vite host outlives a `dokebi dev` restart, so nothing else closes a socket the app joined to it; next to @dokebi/websocket each one is a hijacked request web's drain waits for, and an open page held every restart to the drain deadline (center dev: `Web drain started pending: 2` for one tab, then the process was killed). On drain they end (debug `INERTIA_VITE_HMR_ENDED` with the count) and an HMR upgrade after that is answered 503; the page's Vite client reconnects to the next app and reloads, as it did when the process ended.
|
|
269
|
+
- **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
270
|
- **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
271
|
- 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
272
|
- Without Vite in the project the start fails (`VITE_DEV_MISSING`; `npm install --save-dev vite`).
|
|
@@ -275,7 +277,7 @@ A module keeps its Vue pages in its own views folder (@dokebi/module AI.md §4a)
|
|
|
275
277
|
- **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
278
|
- **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
279
|
- 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
|
|
280
|
+
- 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
281
|
|
|
280
282
|
## 5.2 What the dev server serves (D-0129)
|
|
281
283
|
|
package/dist/index.js
CHANGED
|
@@ -364,10 +364,45 @@ function parseEntry(input) {
|
|
|
364
364
|
class InertiaRuntime {
|
|
365
365
|
settings;
|
|
366
366
|
entry;
|
|
367
|
+
/** D-0288: the Vite dev connection once opened (development only), for the drain. */
|
|
368
|
+
#devServer;
|
|
369
|
+
/** D-0288: set when the app drains; an HMR upgrade after it is not joined (503). */
|
|
370
|
+
#dev = { draining: false };
|
|
367
371
|
constructor(settings, entry) {
|
|
368
372
|
this.settings = settings;
|
|
369
373
|
this.entry = entry;
|
|
370
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* D-0288: the HMR sockets joined to the Vite dev host end when the app drains. The host
|
|
377
|
+
* outlives a `dokebi dev` restart, so nothing else closes them: through a WebSocket plugin each
|
|
378
|
+
* is a hijacked request web's drain counted until its deadline (center dev: "Web drain started
|
|
379
|
+
* pending: 2" for one open page, then the process was killed). The page's Vite client
|
|
380
|
+
* reconnects to the next app and reloads, as it did when the process ended.
|
|
381
|
+
*/
|
|
382
|
+
async drain(context) {
|
|
383
|
+
this.#dev.draining = true;
|
|
384
|
+
const opened = this.#devServer?.();
|
|
385
|
+
if (!opened)
|
|
386
|
+
return;
|
|
387
|
+
let vite;
|
|
388
|
+
try {
|
|
389
|
+
vite = await opened;
|
|
390
|
+
}
|
|
391
|
+
catch (error) {
|
|
392
|
+
// It never opened (its failure was said where it was awaited): nothing was joined.
|
|
393
|
+
if (context.logger.isEnabled('debug'))
|
|
394
|
+
context.logger.log('debug', 'Vite HMR sockets: the dev server never opened', {
|
|
395
|
+
error: describeError(error),
|
|
396
|
+
});
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const sockets = vite.endUpgrades();
|
|
400
|
+
if (sockets > 0)
|
|
401
|
+
context.logger.log('debug', 'Vite HMR sockets ended for the drain', {
|
|
402
|
+
code: 'INERTIA_VITE_HMR_ENDED',
|
|
403
|
+
sockets,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
371
406
|
async setup(context) {
|
|
372
407
|
const server = context.getDependency(web);
|
|
373
408
|
const logger = context.logger;
|
|
@@ -561,6 +596,8 @@ class InertiaRuntime {
|
|
|
561
596
|
const routeIds = this.settings.routeIds;
|
|
562
597
|
const repeats = new Repeats({ intervalMs: limits.logRepeatWindowMs });
|
|
563
598
|
let devServer;
|
|
599
|
+
this.#devServer = () => devServer;
|
|
600
|
+
const dev = this.#dev;
|
|
564
601
|
const devBases = new Set();
|
|
565
602
|
// Vite refuses hosts it does not know (DNS rebinding). Its check is off (one Vite serves
|
|
566
603
|
// every app); instead the dev routes check the host themselves: loopback names, IPs, the
|
|
@@ -604,6 +641,11 @@ class InertiaRuntime {
|
|
|
604
641
|
};
|
|
605
642
|
/** Joins one Vite upgrade to the host after the dev routes' host check. */
|
|
606
643
|
const joinUpgrade = (request, socket, head, base) => {
|
|
644
|
+
// D-0288: the app drains; the page's client tries again and reaches the next app.
|
|
645
|
+
if (dev.draining) {
|
|
646
|
+
socket.end('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n');
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
607
649
|
const host = hostName(request);
|
|
608
650
|
if (!devHosts.allows(host)) {
|
|
609
651
|
if (logger.isEnabled('debug'))
|
|
@@ -1065,7 +1107,7 @@ class InertiaRuntime {
|
|
|
1065
1107
|
}
|
|
1066
1108
|
export const createPlugin = definePlugin({
|
|
1067
1109
|
name: 'dokebi-template-inertia',
|
|
1068
|
-
version: '0.4.
|
|
1110
|
+
version: '0.4.18',
|
|
1069
1111
|
core: '^0.4.2',
|
|
1070
1112
|
options,
|
|
1071
1113
|
entry: parseEntry,
|
package/dist/vite-dev.d.ts
CHANGED
|
@@ -60,6 +60,13 @@ export interface ViteDev {
|
|
|
60
60
|
* the page's own origin (a tunnel, a proxy), never the host's loopback port.
|
|
61
61
|
*/
|
|
62
62
|
upgrade(request: IncomingMessage, socket: Duplex, head: Buffer, report: (error: unknown) => void): void;
|
|
63
|
+
/**
|
|
64
|
+
* D-0288: ends the HMR sockets joined so far (each with its connection to the host) and
|
|
65
|
+
* answers how many. Called when the app drains: the host outlives a `dokebi dev` restart, so
|
|
66
|
+
* nothing else ends them, and a socket a WebSocket plugin routed through Fastify is a hijacked
|
|
67
|
+
* request web's drain waits for. The page's Vite client reconnects to the next app.
|
|
68
|
+
*/
|
|
69
|
+
endUpgrades(): number;
|
|
63
70
|
/** Releases this process's connections (and its own host, when it started one). */
|
|
64
71
|
close(): Promise<void>;
|
|
65
72
|
}
|
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 {
|
|
@@ -513,6 +523,8 @@ export async function connectViteDev(logger, url, key) {
|
|
|
513
523
|
logger.log('debug', 'Vite dev host connected', { url: target.origin, hmr: sources });
|
|
514
524
|
/** HMR sockets joined to the host, ended by close(). */
|
|
515
525
|
const joined = new Set();
|
|
526
|
+
/** The end of each joined upgrade (D-0288: the drain ends them). */
|
|
527
|
+
const ends = new Set();
|
|
516
528
|
return {
|
|
517
529
|
hmrSources: Object.freeze(sources),
|
|
518
530
|
upgrade(request, socket, head, report) {
|
|
@@ -524,11 +536,13 @@ export async function connectViteDev(logger, url, key) {
|
|
|
524
536
|
if (ended)
|
|
525
537
|
return;
|
|
526
538
|
ended = true;
|
|
539
|
+
ends.delete(end);
|
|
527
540
|
joined.delete(socket);
|
|
528
541
|
joined.delete(upstream);
|
|
529
542
|
socket.destroy();
|
|
530
543
|
upstream.destroy();
|
|
531
544
|
};
|
|
545
|
+
ends.add(end);
|
|
532
546
|
upstream.once('connect', () => {
|
|
533
547
|
// The handshake as the browser sent it (rawHeaders keeps names, order and repeats; Node
|
|
534
548
|
// refused any line break in them): Vite checks the token and the subprotocol itself.
|
|
@@ -617,6 +631,12 @@ export async function connectViteDev(logger, url, key) {
|
|
|
617
631
|
return answer.result;
|
|
618
632
|
throw remoteError(answer.error ?? { message: `Vite dev host answered ${status}` });
|
|
619
633
|
},
|
|
634
|
+
endUpgrades: () => {
|
|
635
|
+
const count = ends.size;
|
|
636
|
+
for (const end of [...ends])
|
|
637
|
+
end();
|
|
638
|
+
return count;
|
|
639
|
+
},
|
|
620
640
|
close: async () => {
|
|
621
641
|
agent.destroy();
|
|
622
642
|
renders.destroy();
|
|
@@ -657,6 +677,7 @@ export async function openViteDev(logger, environment = process.env, root = proc
|
|
|
657
677
|
forward: client.forward,
|
|
658
678
|
render: client.render,
|
|
659
679
|
upgrade: client.upgrade,
|
|
680
|
+
endUpgrades: client.endUpgrades,
|
|
660
681
|
close: async () => {
|
|
661
682
|
try {
|
|
662
683
|
await client.close();
|
package/dist/vite-watch.d.ts
CHANGED
|
@@ -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
|
package/dist/vite-watch.js
CHANGED
|
@@ -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 {
|
|
@@ -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.
|
|
3
|
+
"version": "0.4.18",
|
|
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.
|
|
147
|
-
"@dokebi/session": "0.4.
|
|
148
|
-
"@dokebi/web": "0.4.
|
|
149
|
-
"@dokebi/websocket": "0.4.
|
|
146
|
+
"@dokebi/i18n": "0.2.9",
|
|
147
|
+
"@dokebi/session": "0.4.11",
|
|
148
|
+
"@dokebi/web": "0.4.26",
|
|
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"
|