spark-ssr 0.3.1 → 0.3.3

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": "spark-ssr",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Zero-config SSR for spark-html on Bun. The HTML template infers everything: filesystem routing, layouts, <spark-ssr> declarative data (SQL, URLs, globs, modules), auto CRUD with validation, guards, no-JS forms, schema + seeds, live updates, SEO. No build step.",
5
5
  "homepage": "https://wilkinnovo.github.io/spark-html",
6
6
  "type": "module",
package/src/hydrate.js CHANGED
@@ -155,9 +155,20 @@ export function clientScript({ analysis, plan, table, cols, key, live }) {
155
155
  // live (§9): every write the server sees on this table pings the channel;
156
156
  // every open tab refetches through its own session. Realtime as one
157
157
  // attribute — no socket code, no pub/sub setup.
158
+ // Close on pagehide so the channel's HTTP/1.1 socket frees the instant we
159
+ // navigate away — a live EventSource that outlives its page eats one of the
160
+ // browser's ~6 per-host connections, and enough of them (rapid navigation
161
+ // across live pages) starve the next page's own request until the tab hangs.
162
+ // Reopen and refetch on a back/forward-cache restore.
158
163
  if (live && listVar) {
159
- L.push(`const __live = new EventSource('/__spark/live');`);
160
- L.push(`__live.onmessage = (e) => { if (e.data === '${table}') __refresh(); };`);
164
+ L.push(`let __live;`);
165
+ L.push(`function __openLive() {`);
166
+ L.push(` __live = new EventSource('/__spark/live');`);
167
+ L.push(` __live.onmessage = (e) => { if (e.data === '${table}') __refresh(); };`);
168
+ L.push(`}`);
169
+ L.push(`__openLive();`);
170
+ L.push(`addEventListener('pagehide', () => { if (__live) __live.close(); });`);
171
+ L.push(`addEventListener('pageshow', (e) => { if (e.persisted) { __openLive(); __refresh(); } });`);
161
172
  }
162
173
  return L.join('\n') + '\n';
163
174
  }
package/src/server.js CHANGED
@@ -297,8 +297,30 @@ export async function serve(options = {}) {
297
297
  }
298
298
  // Reconnect-then-reload: after a server restart the EventSource reconnects,
299
299
  // and a fresh open following an error means "the server came back" — reload.
300
- const RELOAD_CLIENT = '<script>(()=>{const e=new EventSource("/__spark/reload");let d=0;'
301
- + 'e.onmessage=()=>location.reload();e.onerror=()=>{d=1};e.onopen=()=>{if(d)location.reload()}})()</script>';
300
+ // Close on pagehide: a live EventSource holds one of the browser's ~6
301
+ // per-host HTTP/1.1 sockets, and one that outlives its page starves the
302
+ // next navigation — rapid link-clicking (or a service worker that keeps a
303
+ // controlled client's sockets around) piles them up until the tab hangs
304
+ // loading. Freeing the socket the instant we leave keeps the pool clear;
305
+ // reopen if the page is restored from the back/forward cache.
306
+ // A service worker must never control a spark-ssr dev page: the dev server
307
+ // ships none, so a controller is always a leftover from a PREVIOUS project on
308
+ // this same localhost port. A stale caching worker serves old HTML (so fixes
309
+ // never appear), holds the per-host sockets live reload needs, and throws
310
+ // Cache.put() errors on aborted navigations — the tab hangs and no amount of
311
+ // rescaffolding helps, because deleting files never unregisters a worker.
312
+ // Unregister it, drop its caches, reload once (a session flag stops any loop).
313
+ const RELOAD_CLIENT = '<script>(()=>{'
314
+ + 'if(navigator.serviceWorker&&navigator.serviceWorker.controller&&!sessionStorage.getItem("__spark_sw")){'
315
+ + 'sessionStorage.setItem("__spark_sw","1");'
316
+ + 'console.warn("[spark-ssr] a stale service worker was controlling this dev page — unregistering it and clearing its caches");'
317
+ + 'navigator.serviceWorker.getRegistrations().then(r=>Promise.all(r.map(x=>x.unregister())))'
318
+ + '.then(()=>window.caches?caches.keys().then(k=>Promise.all(k.map(x=>caches.delete(x)))):0)'
319
+ + '.then(()=>location.reload());return}'
320
+ + 'let e,d=0;const open=()=>{e=new EventSource("/__spark/reload");'
321
+ + 'e.onmessage=()=>location.reload();e.onerror=()=>{d=1};e.onopen=()=>{if(d)location.reload()}};open();'
322
+ + 'addEventListener("pagehide",()=>{if(e)e.close()});'
323
+ + 'addEventListener("pageshow",v=>{if(v.persisted)open()})})()</script>';
302
324
 
303
325
  // Heartbeats keep every SSE socket outside Bun's idleTimeout (the default
304
326
  // would kill them at 10 s — and a killed reload socket reconnects, which