rosetack 0.1.1 → 0.1.2

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.
@@ -68,7 +68,10 @@ function ifMark(closes, cond, fn, scope) {
68
68
  const i = closes.length;
69
69
  let snap = null;
70
70
  closes.push(() => {
71
- if (!cond()) return null;
71
+ if (!cond()) {
72
+ snap = null;
73
+ return null;
74
+ }
72
75
  if (!snap) {
73
76
  const c = [];
74
77
  snap = { html: String(fn(scope, c)), closes: c };
@@ -92,6 +95,11 @@ function headMark(closes, fn) {
92
95
  const i = closes.push(fn) - 1;
93
96
  return `<!--\u27E6h:${i}\u27E7-->` + fn() + `<!--\u27E6/h:${i}\u27E7-->`;
94
97
  }
98
+ function rawMark(closes, fn, arg) {
99
+ const i = closes.push((s) => ({ html: String(fn(s) ?? ""), closes: [] })) - 1;
100
+ const r = closes[i](arg);
101
+ return `<!--\u27E6i:${i}\u27E7-->` + r.html + `<!--\u27E6/i:${i}\u27E7-->`;
102
+ }
95
103
  var MARK_RE = /^\u27e6([milh]):(\d+)\u27e7$/;
96
104
  var END_RE = /^\u27e6\/([milh]):(\d+)\u27e7$/;
97
105
  var wired = /* @__PURE__ */ new WeakSet();
@@ -104,6 +112,7 @@ var HEAD_ATTR = "data-rosetack-head";
104
112
  var handlers = globalThis.__rosetack_handlers ??= {};
105
113
  function applyHead(html) {
106
114
  const seen = /* @__PURE__ */ new Set();
115
+ const positional = /* @__PURE__ */ new Map();
107
116
  const frag = tpl(html);
108
117
  const title = frag.querySelector("title");
109
118
  if (title) {
@@ -117,9 +126,16 @@ function applyHead(html) {
117
126
  for (const el of Array.from(document.head.querySelectorAll(`[${HEAD_ATTR}]`))) el.remove();
118
127
  for (const el of Array.from(frag.children)) {
119
128
  if (el.tagName === "TITLE") continue;
120
- const key = el.getAttribute("name") ?? el.getAttribute("property") ?? el.getAttribute("rel") ?? el.getAttribute("charset");
121
- if (!key) continue;
122
- const managed = `${el.tagName.toLowerCase()}:${key}`;
129
+ const tag = el.tagName.toLowerCase();
130
+ const identity = el.getAttribute("name") ?? el.getAttribute("property") ?? el.getAttribute("rel") ?? el.getAttribute("charset");
131
+ let managed;
132
+ if (identity) managed = `${tag}:${identity}`;
133
+ else if (el.id) managed = `${tag}:${el.id}`;
134
+ else {
135
+ const n = positional.get(tag) ?? 0;
136
+ positional.set(tag, n + 1);
137
+ managed = `${tag}:#${n}`;
138
+ }
123
139
  if (seen.has(managed)) continue;
124
140
  seen.add(managed);
125
141
  el.setAttribute(HEAD_ATTR, managed);
@@ -142,16 +158,27 @@ function wire(root, closes, scope) {
142
158
  const walker = document.createTreeWalker(holder, NodeFilter.SHOW_COMMENT);
143
159
  while (walker.nextNode()) comments.push(walker.currentNode);
144
160
  const nested = /* @__PURE__ */ new Set();
161
+ const open = [];
145
162
  for (let i = 0; i < comments.length; i++) {
146
163
  const m = (comments[i].nodeValue ?? "").match(MARK_RE);
147
- if (!m || m[1] !== "i" && m[1] !== "l") continue;
148
- for (let j = i + 1; j < comments.length; j++) {
149
- const em = (comments[j].nodeValue ?? "").match(END_RE);
150
- if (em && em[1] === m[1] && +em[2] === +m[2]) {
151
- for (let k = i + 1; k < j; k++) nested.add(comments[k]);
164
+ if (m && (m[1] === "i" || m[1] === "l")) {
165
+ open.push(i);
166
+ continue;
167
+ }
168
+ const em = (comments[i].nodeValue ?? "").match(END_RE);
169
+ if (!em) continue;
170
+ const key = em[1] + em[2];
171
+ let at = -1;
172
+ for (let k = open.length - 1; k >= 0; k--) {
173
+ const om = (comments[open[k]].nodeValue ?? "").match(MARK_RE);
174
+ if (om[1] + om[2] === key) {
175
+ at = k;
152
176
  break;
153
177
  }
154
178
  }
179
+ if (at < 0) continue;
180
+ for (let k = open[at] + 1; k < i; k++) nested.add(comments[k]);
181
+ open.length = at;
155
182
  }
156
183
  const ends = /* @__PURE__ */ new Map();
157
184
  for (const c of comments) {
@@ -209,7 +236,10 @@ function wire(root, closes, scope) {
209
236
  st.__st = void 0;
210
237
  }
211
238
  const r = closes[idx](scope);
212
- if (!r) return;
239
+ if (!r) {
240
+ st.__ever = true;
241
+ return;
242
+ }
213
243
  let frag;
214
244
  if (st.__ever) {
215
245
  frag = tpl(r.html);
@@ -305,6 +335,23 @@ function clearRequestState() {
305
335
  dataCache.clear();
306
336
  pendingDeltas.clear();
307
337
  }
338
+ var requestQuery = {};
339
+ var prefetchOverride = null;
340
+ function $query() {
341
+ if (prefetchOverride) return prefetchOverride;
342
+ if (typeof location === "undefined") return requestQuery;
343
+ const q = {};
344
+ new URLSearchParams(location.search).forEach((v, k) => {
345
+ q[k] = v;
346
+ });
347
+ return q;
348
+ }
349
+ function setRequestQuery(q) {
350
+ requestQuery = q;
351
+ }
352
+ function setPrefetchOverride(q) {
353
+ prefetchOverride = q;
354
+ }
308
355
  var requestContext = {};
309
356
  function getContext() {
310
357
  return requestContext;
@@ -559,6 +606,38 @@ function $sessionCookie(name, value, opts = {}) {
559
606
  secure || sameSite === "None" ? "Secure" : ""
560
607
  ].filter(Boolean).join("; ");
561
608
  }
609
+ function reportVitals() {
610
+ if (typeof window === "undefined" || typeof performance === "undefined") return;
611
+ const live = {};
612
+ window.__rosetack_vitals = live;
613
+ const send = (name, value) => {
614
+ live[name] = value;
615
+ window.dispatchEvent(new CustomEvent("rosetack:vitals", { detail: { name, value, route: location.pathname } }));
616
+ };
617
+ const nav = performance.getEntriesByType?.("navigation")?.[0];
618
+ if (nav) send("ttfb", Math.round(nav.responseStart));
619
+ const paints = performance.getEntriesByType?.("paint") ?? [];
620
+ const fcp = paints.find((p) => p.name === "first-contentful-paint");
621
+ if (fcp) send("fcp", Math.round(fcp.startTime));
622
+ const PO = window.PerformanceObserver;
623
+ if (!PO) return;
624
+ let cls = 0;
625
+ let inp = 0;
626
+ const flush = () => {
627
+ if (cls) send("cls", Math.round(cls * 1e3) / 1e3);
628
+ if (inp) send("inp", Math.round(inp));
629
+ };
630
+ document.addEventListener("visibilitychange", () => {
631
+ if (document.visibilityState === "hidden") flush();
632
+ });
633
+ new PO((list) => {
634
+ for (const e of list.getEntries()) {
635
+ if (e.entryType === "layout-shift" && !e.hadRecentInput) cls += e.value ?? 0;
636
+ if (e.entryType === "event" && e.interactionId) inp = Math.max(inp, e.duration ?? 0);
637
+ if (e.entryType === "largest-contentful-paint") send("lcp", Math.round(e.startTime));
638
+ }
639
+ }).observe({ entryTypes: ["layout-shift", "event", "largest-contentful-paint"] });
640
+ }
562
641
  export {
563
642
  $actionError,
564
643
  $actionErrorOrThrow,
@@ -568,6 +647,7 @@ export {
568
647
  $data,
569
648
  $merge,
570
649
  $prepend,
650
+ $query,
571
651
  $sessionCookie,
572
652
  $store,
573
653
  $t,
@@ -600,7 +680,9 @@ export {
600
680
  localeList,
601
681
  onCleanup,
602
682
  onMount,
683
+ rawMark,
603
684
  refresh,
685
+ reportVitals,
604
686
  resetEffects,
605
687
  resetRequestContext,
606
688
  restoreState,
@@ -608,7 +690,9 @@ export {
608
690
  serializeState,
609
691
  setLocalePacks,
610
692
  setLocales,
693
+ setPrefetchOverride,
611
694
  setRefreshHook,
695
+ setRequestQuery,
612
696
  setState,
613
697
  setStateDelta,
614
698
  setStoreTransport,