spark-ssr 0.3.2 → 0.3.4

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.js +23 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spark-ssr",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
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/server.js CHANGED
@@ -172,11 +172,18 @@ function pageData(page, cache, pagesDir) {
172
172
  const pageP = parsed[parsed.length - 1];
173
173
 
174
174
  // Compose bodies innermost-out: the page replaces each layout's <slot>.
175
+ // Comments are masked first so a literal <slot> written inside a layout
176
+ // comment (the template's own explainer text does this) isn't mistaken for
177
+ // the real slot — which would inject the whole page inside the comment.
175
178
  let body = pageP.body;
176
179
  for (let i = parsed.length - 2; i >= 0; i--) {
177
180
  const lay = parsed[i].body;
178
181
  const SLOT = /<slot\b[^>]*>(?:\s*<\/slot>)?/i;
179
- body = SLOT.test(lay) ? lay.replace(SLOT, () => body) : lay + body;
182
+ const { masked, restore } = maskComments(lay);
183
+ const m = masked.match(SLOT);
184
+ body = m
185
+ ? restore(masked.slice(0, m.index)) + body + restore(masked.slice(m.index + m[0].length))
186
+ : lay + body;
180
187
  }
181
188
 
182
189
  const blocks = parsed.flatMap((p) => p.blocks);
@@ -303,7 +310,21 @@ export async function serve(options = {}) {
303
310
  // controlled client's sockets around) piles them up until the tab hangs
304
311
  // loading. Freeing the socket the instant we leave keeps the pool clear;
305
312
  // reopen if the page is restored from the back/forward cache.
306
- const RELOAD_CLIENT = '<script>(()=>{let e,d=0;const open=()=>{e=new EventSource("/__spark/reload");'
313
+ // A service worker must never control a spark-ssr dev page: the dev server
314
+ // ships none, so a controller is always a leftover from a PREVIOUS project on
315
+ // this same localhost port. A stale caching worker serves old HTML (so fixes
316
+ // never appear), holds the per-host sockets live reload needs, and throws
317
+ // Cache.put() errors on aborted navigations — the tab hangs and no amount of
318
+ // rescaffolding helps, because deleting files never unregisters a worker.
319
+ // Unregister it, drop its caches, reload once (a session flag stops any loop).
320
+ const RELOAD_CLIENT = '<script>(()=>{'
321
+ + 'if(navigator.serviceWorker&&navigator.serviceWorker.controller&&!sessionStorage.getItem("__spark_sw")){'
322
+ + 'sessionStorage.setItem("__spark_sw","1");'
323
+ + 'console.warn("[spark-ssr] a stale service worker was controlling this dev page — unregistering it and clearing its caches");'
324
+ + 'navigator.serviceWorker.getRegistrations().then(r=>Promise.all(r.map(x=>x.unregister())))'
325
+ + '.then(()=>window.caches?caches.keys().then(k=>Promise.all(k.map(x=>caches.delete(x)))):0)'
326
+ + '.then(()=>location.reload());return}'
327
+ + 'let e,d=0;const open=()=>{e=new EventSource("/__spark/reload");'
307
328
  + 'e.onmessage=()=>location.reload();e.onerror=()=>{d=1};e.onopen=()=>{if(d)location.reload()}};open();'
308
329
  + 'addEventListener("pagehide",()=>{if(e)e.close()});'
309
330
  + 'addEventListener("pageshow",v=>{if(v.persisted)open()})})()</script>';