@webjsdev/server 0.8.73 → 0.8.74

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webjsdev/server",
3
- "version": "0.8.73",
3
+ "version": "0.8.74",
4
4
  "type": "module",
5
5
  "description": "The server for WebJs, a full-stack JavaScript framework built on web components with server-side rendering and no build step. Provides the file-based router, SSR, server actions, route handlers, middleware, and live reload on Node 24+ or Bun.",
6
6
  "main": "index.js",
@@ -492,10 +492,16 @@ function __webjsApplyReload(verdict) {
492
492
  // overlay comes back describing the CURRENT error. Dismissing afterwards
493
493
  // would race that push and wipe a legitimately new overlay.
494
494
  dismissDevOverlay();
495
- refresh(verdict).then(function (ok) {
496
- if (!ok) { location.reload(); return; }
497
- refreshStyles();
498
- }, function () { location.reload(); });
495
+ // Load the rebuilt stylesheets FIRST and swap the markup in only once
496
+ // they are in (#1535): a \`webjs.dev.regenerate\` compile takes 100ms to
497
+ // seconds, and markup swapped in before it lands paints a new class with
498
+ // no rule. The old sheets stay applied until the swap, then go.
499
+ preloadStyles().then(function (styles) {
500
+ return refresh(verdict).then(function (ok) {
501
+ if (!ok) { location.reload(); return; }
502
+ styles.commit();
503
+ });
504
+ }).then(null, function () { location.reload(); });
499
505
  return;
500
506
  }
501
507
  location.reload();
package/src/dev-styles.js CHANGED
@@ -17,39 +17,42 @@
17
17
  */
18
18
 
19
19
  /**
20
- * Re-request every same-origin stylesheet under `root`, cache-busted.
20
+ * Load the rebuilt stylesheets BEFORE the in-place refresh swaps the markup in
21
+ * (#1535), so a new class never paints without its rule.
21
22
  *
22
- * The replacement is inserted BESIDE the old link, and which of the two
23
- * survives depends on how the request went. That asymmetry is the whole point
24
- * and must not be collapsed into one shared handler:
23
+ * The re-request used to run AFTER the swap, and that order was the flash: the new
24
+ * markup is on screen while its stylesheet is still being rebuilt and fetched
25
+ * (a `webjs.dev.regenerate` compile is 100ms to seconds), so an element that
26
+ * gained a utility class shows with no rule for it until the sheet lands.
27
+ * Measured on a fixture with an 800ms compile: 56 animation frames (about
28
+ * 0.9s) painted the edited element unstyled. A full reload never had the
29
+ * problem, because a `<head>` stylesheet is render-blocking and the browser
30
+ * holds the old page until it loads.
25
31
  *
26
- * - On `load` the OLD link is dropped, so the page is never briefly unstyled.
27
- * - On `error` the NEW link is dropped and the old one kept. A re-request can
28
- * fail (the server is mid-restart, the file was renamed or deleted), and
29
- * removing the last working sheet there would leave the page permanently
30
- * unstyled, which is exactly the failure #936 and #1400 exist to prevent.
31
- *
32
- * Duplicates are collapsed to one link per IDENTITY, where the identity is
33
- * every attribute except the href's query. The duplicate being collapsed is the
34
- * one the head merge re-appends (the incoming bare-href link beside the busted
35
- * one), so the head would otherwise gain a link on every refresh. Keying on the
36
- * path alone would be wrong in the other direction: it would delete an author's
37
- * second, legitimately distinct link to the same file, such as a `media="print"`
38
- * sheet.
32
+ * So the cache-busted replacements go in first, BESIDE the current links
33
+ * (both apply meanwhile, the same rules twice), and the returned promise
34
+ * resolves once each has loaded or failed, or after `timeoutMs` for one that
35
+ * hangs. The caller swaps the markup in and then calls `commit()`, which drops
36
+ * each old link whose replacement loaded, plus any same-identity copy the head
37
+ * merge appended, keeping the replacement. A replacement that failed is
38
+ * removed and the old link kept (#936, #1400: never take the last working
39
+ * sheet down). One still pending at commit finishes the same way when it
40
+ * settles.
39
41
  *
40
42
  * @param {ParentNode} [root] where to scan, defaulting to the whole document.
41
- * Injectable so a browser test can scope itself to its own container.
42
43
  * @param {() => number} [now] cache-buster source, injectable for tests.
43
- * @returns {Element[]} the replacement links, so a caller (a test) can await them.
44
+ * @param {number} [timeoutMs] the longest to hold the swap for a stylesheet.
45
+ * @returns {Promise<{ links: Element[], commit: () => void }>}
44
46
  */
45
- export function refreshStyles(root, now) {
47
+ export function preloadStyles(root, now, timeoutMs) {
46
48
  const scope = root || (typeof document !== 'undefined' ? document : null);
47
- if (!scope) return [];
49
+ const limit = typeof timeoutMs === 'number' ? timeoutMs : 10000;
50
+ if (!scope) return Promise.resolve({ links: [], commit: function () {} });
48
51
  const stamp = now || Date.now;
49
52
  /** @type {Record<string, { el: Element, url: URL }>} */
50
53
  const kept = Object.create(null);
51
- const links = [].slice.call(scope.querySelectorAll('link[rel~="stylesheet"][href]'));
52
- for (const el of links) {
54
+ const current = [].slice.call(scope.querySelectorAll('link[rel~="stylesheet"][href]'));
55
+ for (const el of current) {
53
56
  let url;
54
57
  try { url = new URL(el.getAttribute('href'), location.href); } catch (_) { continue; }
55
58
  if (url.origin !== location.origin) continue;
@@ -57,7 +60,21 @@ export function refreshStyles(root, now) {
57
60
  if (kept[key]) { if (el.parentNode) el.parentNode.removeChild(el); continue; }
58
61
  kept[key] = { el, url };
59
62
  }
60
- const added = [];
63
+ let committed = false;
64
+ /** @type {Array<{ key: string, old: Element, next: Element, state: string }>} */
65
+ const pairs = [];
66
+ // Drop every other copy of this sheet (the old link, a copy the head merge
67
+ // re-appended) so exactly the replacement is left.
68
+ const settleLoaded = function (pair) {
69
+ const links = [].slice.call(scope.querySelectorAll('link[rel~="stylesheet"][href]'));
70
+ for (const el of links) {
71
+ if (el === pair.next) continue;
72
+ let url;
73
+ try { url = new URL(el.getAttribute('href'), location.href); } catch (_) { continue; }
74
+ if (url.origin === location.origin && identityKey(el, url) === pair.key && el.parentNode) el.parentNode.removeChild(el);
75
+ }
76
+ };
77
+ const waits = [];
61
78
  for (const key of Object.keys(kept)) {
62
79
  const old = kept[key].el;
63
80
  const url = kept[key].url;
@@ -65,17 +82,34 @@ export function refreshStyles(root, now) {
65
82
  url.searchParams.set('__webjs_dev', String(stamp()));
66
83
  const next = /** @type {Element} */ (old.cloneNode(false));
67
84
  next.setAttribute('href', url.pathname + url.search);
68
- next.addEventListener('load', function () {
69
- if (old.parentNode) old.parentNode.removeChild(old);
70
- });
71
- next.addEventListener('error', function () {
72
- // Keep the sheet that still works rather than the one that just failed.
73
- if (next.parentNode) next.parentNode.removeChild(next);
74
- });
85
+ const pair = { key, old, next, state: 'pending' };
86
+ pairs.push(pair);
87
+ waits.push(new Promise(function (resolve) {
88
+ next.addEventListener('load', function () {
89
+ pair.state = 'load';
90
+ if (committed) settleLoaded(pair);
91
+ resolve(undefined);
92
+ });
93
+ next.addEventListener('error', function () {
94
+ pair.state = 'error';
95
+ if (next.parentNode) next.parentNode.removeChild(next);
96
+ resolve(undefined);
97
+ });
98
+ }));
75
99
  old.parentNode.insertBefore(next, old.nextSibling);
76
- added.push(next);
77
100
  }
78
- return added;
101
+ const commit = function () {
102
+ committed = true;
103
+ for (const pair of pairs) if (pair.state === 'load') settleLoaded(pair);
104
+ };
105
+ const links = pairs.map(function (p) { return p.next; });
106
+ if (!waits.length) return Promise.resolve({ links, commit });
107
+ return new Promise(function (resolve) {
108
+ let done = false;
109
+ const finish = function () { if (!done) { done = true; resolve({ links, commit }); } };
110
+ Promise.all(waits).then(finish);
111
+ setTimeout(finish, limit);
112
+ });
79
113
  }
80
114
 
81
115
  /**