@fictjs/runtime 0.15.0 → 0.17.0

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 (55) hide show
  1. package/dist/advanced.cjs +9 -9
  2. package/dist/advanced.js +4 -4
  3. package/dist/{chunk-CBRGOLTR.cjs → chunk-5FVWBK4M.cjs} +40 -40
  4. package/dist/{chunk-CBRGOLTR.cjs.map → chunk-5FVWBK4M.cjs.map} +1 -1
  5. package/dist/{chunk-BADX4WTQ.cjs → chunk-6DNYVH5U.cjs} +17 -17
  6. package/dist/{chunk-BADX4WTQ.cjs.map → chunk-6DNYVH5U.cjs.map} +1 -1
  7. package/dist/{chunk-MAHWGB55.js → chunk-CFAWL76V.js} +5 -5
  8. package/dist/{chunk-MAHWGB55.js.map → chunk-CFAWL76V.js.map} +1 -1
  9. package/dist/{chunk-WJMZ7X46.cjs → chunk-ECKYFH5Q.cjs} +5 -5
  10. package/dist/{chunk-WJMZ7X46.cjs.map → chunk-ECKYFH5Q.cjs.map} +1 -1
  11. package/dist/{chunk-ZJZ6LMDN.js → chunk-F5SDRX4J.js} +2 -2
  12. package/dist/{chunk-ZJZ6LMDN.js.map → chunk-F5SDRX4J.js.map} +1 -1
  13. package/dist/{chunk-4P4DYWLQ.js → chunk-IIWHTV23.js} +3 -3
  14. package/dist/{chunk-AR2T7JEX.cjs → chunk-INYTG4NG.cjs} +174 -174
  15. package/dist/{chunk-AR2T7JEX.cjs.map → chunk-INYTG4NG.cjs.map} +1 -1
  16. package/dist/{chunk-RK2WSQYL.js → chunk-M42N54LG.js} +3 -3
  17. package/dist/{chunk-ZWQLXWSV.js → chunk-UQTWIV3S.js} +3 -3
  18. package/dist/{chunk-ECNK25S4.cjs → chunk-WY4LI5PB.cjs} +8 -8
  19. package/dist/{chunk-ECNK25S4.cjs.map → chunk-WY4LI5PB.cjs.map} +1 -1
  20. package/dist/index.cjs +42 -42
  21. package/dist/index.d.cts +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.dev.js.map +1 -1
  24. package/dist/index.js +3 -3
  25. package/dist/internal-list.cjs +4 -4
  26. package/dist/internal-list.js +3 -3
  27. package/dist/internal.cjs +5 -5
  28. package/dist/internal.d.cts +2 -2
  29. package/dist/internal.d.ts +2 -2
  30. package/dist/internal.js +4 -4
  31. package/dist/jsx-dev-runtime.cjs.map +1 -1
  32. package/dist/jsx-dev-runtime.d.cts +46 -0
  33. package/dist/jsx-dev-runtime.d.ts +46 -0
  34. package/dist/jsx-dev-runtime.js.map +1 -1
  35. package/dist/jsx-runtime.cjs.map +1 -1
  36. package/dist/jsx-runtime.d.cts +46 -0
  37. package/dist/jsx-runtime.d.ts +46 -0
  38. package/dist/jsx-runtime.js.map +1 -1
  39. package/dist/loader.cjs +143 -26
  40. package/dist/loader.cjs.map +1 -1
  41. package/dist/loader.d.cts +1 -1
  42. package/dist/loader.d.ts +1 -1
  43. package/dist/loader.js +122 -5
  44. package/dist/loader.js.map +1 -1
  45. package/dist/{props-DabFQwLR.d.ts → props-C04ScJgm.d.ts} +46 -0
  46. package/dist/{props-tImUZAty.d.cts → props-CdmuXCiu.d.cts} +46 -0
  47. package/dist/{resume-C5IKAIdh.d.ts → resume-C166aAVg.d.ts} +2 -2
  48. package/dist/{resume-DPZxmA95.d.cts → resume-C20cRVj9.d.cts} +2 -2
  49. package/package.json +1 -1
  50. package/src/jsx.ts +46 -0
  51. package/src/loader.ts +153 -4
  52. package/src/resume.ts +5 -5
  53. /package/dist/{chunk-4P4DYWLQ.js.map → chunk-IIWHTV23.js.map} +0 -0
  54. /package/dist/{chunk-RK2WSQYL.js.map → chunk-M42N54LG.js.map} +0 -0
  55. /package/dist/{chunk-ZWQLXWSV.js.map → chunk-UQTWIV3S.js.map} +0 -0
package/dist/loader.cjs CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- var _chunkWJMZ7X46cjs = require('./chunk-WJMZ7X46.cjs');
11
+ var _chunkECKYFH5Qcjs = require('./chunk-ECKYFH5Q.cjs');
12
12
 
13
13
  // src/loader.ts
14
14
  function resolveModuleUrl(url) {
@@ -21,6 +21,89 @@ function resolveModuleUrl(url) {
21
21
  }
22
22
  return url;
23
23
  }
24
+ function resolveAbsoluteModuleUrl(url, ownerDocument) {
25
+ const baseUrl = _nullishCoalesce(_optionalChain([ownerDocument, 'optionalAccess', _ => _.baseURI]), () => ( (typeof document !== "undefined" ? document.baseURI : void 0)));
26
+ if (!baseUrl) return url;
27
+ try {
28
+ return new URL(url, baseUrl).href;
29
+ } catch (e) {
30
+ return url;
31
+ }
32
+ }
33
+ function normalizeImportUrl(url) {
34
+ if (!url.startsWith("data:")) {
35
+ return url;
36
+ }
37
+ const dataSeparatorIndex = url.indexOf(",");
38
+ if (dataSeparatorIndex === -1) {
39
+ return url;
40
+ }
41
+ const metadata = url.slice(0, dataSeparatorIndex);
42
+ const payload = url.slice(dataSeparatorIndex + 1);
43
+ if (metadata.includes(";base64")) {
44
+ return url;
45
+ }
46
+ try {
47
+ return `${metadata},${encodeURIComponent(decodeURIComponent(payload))}`;
48
+ } catch (e2) {
49
+ return `${metadata},${encodeURIComponent(payload)}`;
50
+ }
51
+ }
52
+ function captureControlState(node, event) {
53
+ if (event.type !== "input" && event.type !== "change") return null;
54
+ if (node instanceof HTMLInputElement) {
55
+ if (node.type === "file") return null;
56
+ if (node.type === "checkbox" || node.type === "radio") {
57
+ return { checked: node.checked };
58
+ }
59
+ return { value: node.value };
60
+ }
61
+ if (node instanceof HTMLTextAreaElement) {
62
+ return { value: node.value };
63
+ }
64
+ if (node instanceof HTMLSelectElement) {
65
+ return node.multiple ? {
66
+ selectedValues: Array.from(node.selectedOptions).map((option) => option.value)
67
+ } : {
68
+ value: node.value,
69
+ selectedIndex: node.selectedIndex
70
+ };
71
+ }
72
+ return null;
73
+ }
74
+ function restoreControlState(node, state) {
75
+ if (!state) return;
76
+ if (node instanceof HTMLInputElement) {
77
+ if (typeof state.checked === "boolean") {
78
+ node.checked = state.checked;
79
+ }
80
+ if (typeof state.value === "string" && node.type !== "file") {
81
+ node.value = state.value;
82
+ }
83
+ return;
84
+ }
85
+ if (node instanceof HTMLTextAreaElement) {
86
+ if (typeof state.value === "string") {
87
+ node.value = state.value;
88
+ }
89
+ return;
90
+ }
91
+ if (node instanceof HTMLSelectElement) {
92
+ if (Array.isArray(state.selectedValues) && node.multiple) {
93
+ const selected = new Set(state.selectedValues);
94
+ for (const option of Array.from(node.options)) {
95
+ option.selected = selected.has(option.value);
96
+ }
97
+ return;
98
+ }
99
+ if (typeof state.selectedIndex === "number") {
100
+ node.selectedIndex = state.selectedIndex;
101
+ }
102
+ if (typeof state.value === "string") {
103
+ node.value = state.value;
104
+ }
105
+ }
106
+ }
24
107
  var hydratedScopes = /* @__PURE__ */ new Set();
25
108
  var prefetchedUrls = /* @__PURE__ */ new Set();
26
109
  var prefetchCleanup = null;
@@ -54,7 +137,7 @@ function installResumableLoader(options = {}) {
54
137
  prefetchedUrls.clear();
55
138
  processedSnapshots.clear();
56
139
  emittedIssueKeys.clear();
57
- _chunkWJMZ7X46cjs.__fictSetSSRState.call(void 0, null);
140
+ _chunkECKYFH5Qcjs.__fictSetSSRState.call(void 0, null);
58
141
  if (eventListenerCleanup) {
59
142
  eventListenerCleanup();
60
143
  eventListenerCleanup = null;
@@ -68,10 +151,10 @@ function installResumableLoader(options = {}) {
68
151
  snapshotObserver = null;
69
152
  }
70
153
  const snapshotEl = doc.getElementById(scriptId);
71
- if (_optionalChain([snapshotEl, 'optionalAccess', _ => _.textContent])) {
154
+ if (_optionalChain([snapshotEl, 'optionalAccess', _2 => _2.textContent])) {
72
155
  const state = parseSnapshotText(snapshotEl.textContent, `#${scriptId}`);
73
156
  if (state) {
74
- _chunkWJMZ7X46cjs.__fictSetSSRState.call(void 0, state);
157
+ _chunkECKYFH5Qcjs.__fictSetSSRState.call(void 0, state);
75
158
  }
76
159
  }
77
160
  const snapshotScripts = doc.querySelectorAll(
@@ -91,7 +174,7 @@ function installResumableLoader(options = {}) {
91
174
  parseSnapshotScript(script);
92
175
  }
93
176
  }
94
- const nested = _optionalChain([node, 'access', _2 => _2.querySelectorAll, 'optionalCall', _3 => _3(
177
+ const nested = _optionalChain([node, 'access', _3 => _3.querySelectorAll, 'optionalCall', _4 => _4(
95
178
  'script[type="application/json"][data-fict-snapshot]'
96
179
  )]);
97
180
  if (nested && nested.length) {
@@ -104,8 +187,8 @@ function installResumableLoader(options = {}) {
104
187
  });
105
188
  snapshotObserver.observe(_nullishCoalesce(doc.documentElement, () => ( doc)), { childList: true, subtree: true });
106
189
  }
107
- _chunkWJMZ7X46cjs.__fictEnableResumable.call(void 0, );
108
- const events = _nullishCoalesce(options.events, () => ( Array.from(_chunkWJMZ7X46cjs.DelegatedEvents)));
190
+ _chunkECKYFH5Qcjs.__fictEnableResumable.call(void 0, );
191
+ const events = _nullishCoalesce(options.events, () => ( Array.from(_chunkECKYFH5Qcjs.DelegatedEvents)));
109
192
  for (const eventName of events) {
110
193
  doc.addEventListener(eventName, handleResumableEvent, true);
111
194
  }
@@ -129,19 +212,19 @@ function parseSnapshotScript(script) {
129
212
  const source = script.id ? `#${script.id}` : "<script[data-fict-snapshot]>";
130
213
  const state = parseSnapshotText(text, source);
131
214
  if (state) {
132
- _chunkWJMZ7X46cjs.__fictMergeSSRState.call(void 0, state);
215
+ _chunkECKYFH5Qcjs.__fictMergeSSRState.call(void 0, state);
133
216
  }
134
217
  }
135
218
  function parseSnapshotText(text, source) {
136
219
  let parsed;
137
220
  try {
138
221
  parsed = JSON.parse(text);
139
- } catch (e) {
222
+ } catch (e3) {
140
223
  emitSnapshotIssue({
141
224
  code: "snapshot_parse_error",
142
225
  message: "[fict/loader] Failed to parse SSR snapshot JSON.",
143
226
  source,
144
- expectedVersion: _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
227
+ expectedVersion: _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
145
228
  });
146
229
  return null;
147
230
  }
@@ -153,18 +236,18 @@ function normalizeSnapshotState(value, source) {
153
236
  code: "snapshot_invalid_shape",
154
237
  message: "[fict/loader] Snapshot payload must be an object.",
155
238
  source,
156
- expectedVersion: _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
239
+ expectedVersion: _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
157
240
  });
158
241
  return null;
159
242
  }
160
243
  const rawVersion = value.v;
161
- const version = rawVersion === void 0 ? _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION : rawVersion;
162
- if (!Number.isInteger(version) || version !== _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION) {
244
+ const version = rawVersion === void 0 ? _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION : rawVersion;
245
+ if (!Number.isInteger(version) || version !== _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION) {
163
246
  const versionIssue = {
164
247
  code: "snapshot_unsupported_version",
165
248
  message: `[fict/loader] Snapshot schema version ${String(version)} is not supported by this runtime.`,
166
249
  source,
167
- expectedVersion: _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
250
+ expectedVersion: _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
168
251
  };
169
252
  if (typeof version === "number") {
170
253
  versionIssue.actualVersion = version;
@@ -180,11 +263,11 @@ function normalizeSnapshotState(value, source) {
180
263
  code: "snapshot_invalid_shape",
181
264
  message: "[fict/loader] Snapshot payload is missing a valid `scopes` object.",
182
265
  source,
183
- expectedVersion: _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
266
+ expectedVersion: _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION
184
267
  });
185
268
  return null;
186
269
  }
187
- return { v: _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION, scopes };
270
+ return { v: _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION, scopes };
188
271
  }
189
272
  function isRecord(value) {
190
273
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -193,7 +276,7 @@ function emitSnapshotIssue(issue) {
193
276
  const key = `${issue.code}|${issue.source}|${_nullishCoalesce(issue.scopeId, () => ( ""))}|${_nullishCoalesce(issue.actualVersion, () => ( ""))}|${issue.expectedVersion}`;
194
277
  if (emittedIssueKeys.has(key)) return;
195
278
  emittedIssueKeys.add(key);
196
- _optionalChain([snapshotIssueHandler, 'optionalCall', _4 => _4(issue)]);
279
+ _optionalChain([snapshotIssueHandler, 'optionalCall', _5 => _5(issue)]);
197
280
  if (typeof console !== "undefined" && typeof console.warn === "function") {
198
281
  console.warn(issue.message);
199
282
  }
@@ -314,7 +397,7 @@ function prefetchQrl(qrl, ownerDocument) {
314
397
  link.rel = "modulepreload";
315
398
  link.href = resolvedUrl;
316
399
  link.crossOrigin = "anonymous";
317
- _optionalChain([doc, 'access', _5 => _5.head, 'optionalAccess', _6 => _6.appendChild, 'call', _7 => _7(link)]);
400
+ _optionalChain([doc, 'access', _6 => _6.head, 'optionalAccess', _7 => _7.appendChild, 'call', _8 => _8(link)]);
318
401
  }
319
402
  }
320
403
  function handleResumableEvent(event) {
@@ -338,18 +421,18 @@ async function handleResumableEventAsync(event) {
338
421
  if (!host) continue;
339
422
  const scopeId = host.getAttribute("data-fict-s");
340
423
  if (!scopeId) continue;
341
- const snapshot = _chunkWJMZ7X46cjs.__fictGetSSRScope.call(void 0, scopeId);
424
+ const snapshot = _chunkECKYFH5Qcjs.__fictGetSSRScope.call(void 0, scopeId);
342
425
  if (!snapshot) {
343
426
  emitSnapshotIssue({
344
427
  code: "scope_snapshot_missing",
345
428
  message: `[fict/loader] Missing scope snapshot for ${scopeId}; skipping resumable handler execution.`,
346
429
  source: "event",
347
- expectedVersion: _chunkWJMZ7X46cjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION,
430
+ expectedVersion: _chunkECKYFH5Qcjs.FICT_SSR_SNAPSHOT_SCHEMA_VERSION,
348
431
  scopeId
349
432
  });
350
433
  continue;
351
434
  }
352
- _chunkWJMZ7X46cjs.__fictEnsureScope.call(void 0, scopeId, host, snapshot);
435
+ _chunkECKYFH5Qcjs.__fictEnsureScope.call(void 0, scopeId, host, snapshot);
353
436
  const { url, exportName } = parseQrl(qrl);
354
437
  if (event.cancelable && (event.type === "click" || event.type === "submit")) {
355
438
  const tag = node.tagName.toLowerCase();
@@ -357,30 +440,64 @@ async function handleResumableEventAsync(event) {
357
440
  event.preventDefault();
358
441
  }
359
442
  }
443
+ const preservedControlState = captureControlState(node, event);
444
+ let resumedDuringEvent = false;
360
445
  if (!hydratedScopes.has(scopeId)) {
361
446
  const resumeQrl = host.getAttribute("data-fict-h");
362
447
  if (resumeQrl) {
363
448
  const { url: resumeUrl, exportName: resumeExport } = parseQrl(resumeQrl);
364
449
  const resolvedResumeUrl = resolveModuleUrl(resumeUrl);
450
+ const resolvedAbsoluteResumeUrl = resolveAbsoluteModuleUrl(
451
+ resolvedResumeUrl,
452
+ _nullishCoalesce(host.ownerDocument, () => ( void 0))
453
+ );
454
+ const resolvedResumeQrl = `${resolvedResumeUrl}#${resumeExport}`;
455
+ const resolvedAbsoluteResumeQrl = `${resolvedAbsoluteResumeUrl}#${resumeExport}`;
456
+ const normalizedResumeImportUrl = normalizeImportUrl(resolvedResumeUrl);
365
457
  await Promise.resolve().then(() => _interopRequireWildcard(require(
366
458
  /* @vite-ignore */
367
- resolvedResumeUrl
459
+ normalizedResumeImportUrl
368
460
  )));
369
- const resumeFn = _chunkWJMZ7X46cjs.__fictGetResume.call(void 0, resumeExport);
461
+ const resumeFn = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_chunkECKYFH5Qcjs.__fictGetResume.call(void 0, resumeQrl), () => ( _chunkECKYFH5Qcjs.__fictGetResume.call(void 0, resolvedResumeQrl))), () => ( _chunkECKYFH5Qcjs.__fictGetResume.call(void 0, resolvedAbsoluteResumeQrl))), () => ( _chunkECKYFH5Qcjs.__fictGetResume.call(void 0, resumeExport)));
370
462
  if (typeof resumeFn === "function") {
371
463
  await resumeFn(scopeId, host);
372
464
  hydratedScopes.add(scopeId);
465
+ resumedDuringEvent = true;
373
466
  }
374
467
  }
375
468
  }
469
+ if (resumedDuringEvent) {
470
+ restoreControlState(node, preservedControlState);
471
+ }
376
472
  const resolvedUrl = resolveModuleUrl(url);
473
+ const normalizedImportUrl = normalizeImportUrl(resolvedUrl);
377
474
  const mod = await Promise.resolve().then(() => _interopRequireWildcard(require(
378
475
  /* @vite-ignore */
379
- resolvedUrl
476
+ normalizedImportUrl
380
477
  )));
381
478
  const handler = mod[exportName];
382
479
  if (typeof handler === "function") {
383
- await handler(scopeId, event, node);
480
+ const originalCurrentTarget = event.currentTarget;
481
+ Object.defineProperty(event, "currentTarget", {
482
+ configurable: true,
483
+ get() {
484
+ return node;
485
+ }
486
+ });
487
+ try {
488
+ await handler(
489
+ scopeId,
490
+ event,
491
+ node
492
+ );
493
+ } finally {
494
+ Object.defineProperty(event, "currentTarget", {
495
+ configurable: true,
496
+ get() {
497
+ return originalCurrentTarget;
498
+ }
499
+ });
500
+ }
384
501
  }
385
502
  return;
386
503
  }
@@ -413,5 +530,5 @@ function buildEventPath(event) {
413
530
 
414
531
 
415
532
 
416
- exports.__fictUseLexicalScope = _chunkWJMZ7X46cjs.__fictUseLexicalScope; exports.cleanupEventListeners = cleanupEventListeners; exports.installResumableLoader = installResumableLoader; exports.resetHydratedScopes = resetHydratedScopes; exports.resetPrefetchedUrls = resetPrefetchedUrls; exports.waitForPendingHandlers = waitForPendingHandlers;
533
+ exports.__fictUseLexicalScope = _chunkECKYFH5Qcjs.__fictUseLexicalScope; exports.cleanupEventListeners = cleanupEventListeners; exports.installResumableLoader = installResumableLoader; exports.resetHydratedScopes = resetHydratedScopes; exports.resetPrefetchedUrls = resetPrefetchedUrls; exports.waitForPendingHandlers = waitForPendingHandlers;
417
534
  //# sourceMappingURL=loader.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/fict/fict/packages/runtime/dist/loader.cjs","../src/loader.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;ACUA,SAAS,gBAAA,CAAiB,GAAA,EAAqB;AAC7C,EAAA,MAAM,SAAA,EAAY,UAAA,CAAuC,iBAAA;AAIzD,EAAA,GAAA,CAAI,QAAA,EAAU;AAEZ,IAAA,MAAM,SAAA,EAAW,QAAA,CAAS,GAAG,CAAA;AAC7B,IAAA,GAAA,CAAI,QAAA,EAAU;AACZ,MAAA,OAAO,QAAA;AAAA,IACT;AAAA,EACF;AAEA,EAAA,OAAO,GAAA;AACT;AAmEA,IAAM,eAAA,kBAAiB,IAAI,GAAA,CAAY,CAAA;AACvC,IAAM,eAAA,kBAAiB,IAAI,GAAA,CAAY,CAAA;AACvC,IAAI,gBAAA,EAAuC,IAAA;AAC3C,IAAI,qBAAA,EAA4C,IAAA;AAChD,IAAI,iBAAA,EAA4C,IAAA;AAChD,IAAM,mBAAA,kBAAqB,IAAI,GAAA,CAAuB,CAAA;AACtD,IAAI,qBAAA,EAAgE,IAAA;AACpE,IAAM,iBAAA,kBAAmB,IAAI,GAAA,CAAY,CAAA;AAKlC,SAAS,mBAAA,CAAA,EAA4B;AAC1C,EAAA,cAAA,CAAe,KAAA,CAAM,CAAA;AACvB;AAKO,SAAS,mBAAA,CAAA,EAA4B;AAC1C,EAAA,cAAA,CAAe,KAAA,CAAM,CAAA;AACvB;AAKA,IAAM,gBAAA,kBAAkB,IAAI,GAAA,CAAmB,CAAA;AAK/C,MAAA,SAAsB,sBAAA,CAAA,EAAwC;AAC5D,EAAA,GAAA,CAAI,eAAA,CAAgB,KAAA,IAAS,CAAA,EAAG,MAAA;AAChC,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EAAA;AACf,EAAA;AACqB,IAAA;AAC3C,EAAA;AAC6D,EAAA;AAC/D;AAEqD;AACtB,EAAA;AACQ,EAAA;AACxB,EAAA;AACG,IAAA;AACQ,IAAA;AACxB,EAAA;AACgB,EAAA;AACT,EAAA;AACT;ADzMyE;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/fict/fict/packages/runtime/dist/loader.cjs","sourcesContent":[null,"import { DelegatedEvents } from './constants'\nimport {\n FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n type SSRState,\n __fictEnableResumable,\n __fictEnsureScope,\n __fictGetResume,\n __fictGetSSRScope,\n __fictMergeSSRState,\n __fictSetSSRState,\n __fictUseLexicalScope,\n} from './resume'\n\n// ============================================================================\n// Module Resolution\n// ============================================================================\n\n/**\n * Resolve a module URL through the manifest if available.\n * In production, virtual module URLs (virtual:fict-handler:...) are mapped\n * to their built chunk URLs through the manifest.\n */\nfunction resolveModuleUrl(url: string): string {\n const manifest = (globalThis as Record<string, unknown>).__FICT_MANIFEST__ as\n | Record<string, string>\n | undefined\n\n if (manifest) {\n // Check if the URL (without #fragment) is in the manifest\n const resolved = manifest[url]\n if (resolved) {\n return resolved\n }\n }\n\n return url\n}\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PrefetchStrategy {\n /**\n * Enable visibility-based prefetch using IntersectionObserver.\n * Prefetches modules when interactive elements come into view.\n * @default true\n */\n visibility?: boolean\n /**\n * Root margin for IntersectionObserver (e.g., '200px' to prefetch earlier).\n * @default '200px'\n */\n visibilityMargin?: string\n /**\n * Enable hover-based prefetch using pointerover events.\n * Prefetches modules when user hovers over interactive elements.\n * @default true\n */\n hover?: boolean\n /**\n * Delay in ms before prefetching on hover (debounce rapid movements).\n * @default 50\n */\n hoverDelay?: number\n}\n\nexport interface ResumableLoaderOptions {\n document?: Document\n snapshotScriptId?: string\n events?: string[]\n /**\n * Receives structured snapshot/resume issues detected by the loader.\n * Useful for telemetry and fail-safe fallback orchestration.\n */\n onSnapshotIssue?: (issue: SnapshotIssue) => void\n /**\n * Prefetch strategy configuration.\n * Set to false to disable all prefetching.\n * @default { visibility: true, hover: true }\n */\n prefetch?: PrefetchStrategy | false\n}\n\nexport type SnapshotIssueCode =\n | 'snapshot_parse_error'\n | 'snapshot_invalid_shape'\n | 'snapshot_unsupported_version'\n | 'scope_snapshot_missing'\n\nexport interface SnapshotIssue {\n code: SnapshotIssueCode\n message: string\n source: string\n expectedVersion: number\n actualVersion?: number\n scopeId?: string\n}\n\n// ============================================================================\n// State\n// ============================================================================\n\nconst hydratedScopes = new Set<string>()\nconst prefetchedUrls = new Set<string>()\nlet prefetchCleanup: (() => void) | null = null\nlet eventListenerCleanup: (() => void) | null = null\nlet snapshotObserver: MutationObserver | null = null\nconst processedSnapshots = new Set<HTMLScriptElement>()\nlet snapshotIssueHandler: ((issue: SnapshotIssue) => void) | null = null\nconst emittedIssueKeys = new Set<string>()\n\n/**\n * Reset the hydrated scopes set. Useful for testing.\n */\nexport function resetHydratedScopes(): void {\n hydratedScopes.clear()\n}\n\n/**\n * Reset the prefetched URLs set. Useful for testing.\n */\nexport function resetPrefetchedUrls(): void {\n prefetchedUrls.clear()\n}\n\n/**\n * Set of pending handler promises. Used for testing to wait for all handlers to complete.\n */\nconst pendingHandlers = new Set<Promise<void>>()\n\n/**\n * Wait for all pending event handlers to complete. Useful for testing.\n */\nexport async function waitForPendingHandlers(): Promise<void> {\n if (pendingHandlers.size === 0) return\n await Promise.allSettled([...pendingHandlers])\n}\n\n/**\n * Clean up all registered event listeners. Useful for testing.\n */\nexport function cleanupEventListeners(): void {\n if (eventListenerCleanup) {\n eventListenerCleanup()\n eventListenerCleanup = null\n }\n}\n\n// ============================================================================\n// Main Entry Point\n// ============================================================================\n\nexport function installResumableLoader(options: ResumableLoaderOptions = {}): void {\n const doc = options.document ?? window.document\n const scriptId = options.snapshotScriptId ?? '__FICT_SNAPSHOT__'\n snapshotIssueHandler = options.onSnapshotIssue ?? null\n\n // Reset hydrated scopes for fresh loader installation\n hydratedScopes.clear()\n prefetchedUrls.clear()\n processedSnapshots.clear()\n emittedIssueKeys.clear()\n __fictSetSSRState(null)\n\n // Clean up previous event listeners\n if (eventListenerCleanup) {\n eventListenerCleanup()\n eventListenerCleanup = null\n }\n\n // Clean up previous prefetch handlers\n if (prefetchCleanup) {\n prefetchCleanup()\n prefetchCleanup = null\n }\n\n if (snapshotObserver) {\n snapshotObserver.disconnect()\n snapshotObserver = null\n }\n\n const snapshotEl = doc.getElementById(scriptId)\n if (snapshotEl?.textContent) {\n const state = parseSnapshotText(snapshotEl.textContent, `#${scriptId}`)\n if (state) {\n __fictSetSSRState(state)\n }\n }\n\n const snapshotScripts = doc.querySelectorAll(\n 'script[type=\"application/json\"][data-fict-snapshot]',\n )\n for (const script of Array.from(snapshotScripts)) {\n parseSnapshotScript(script as HTMLScriptElement)\n }\n\n if (typeof MutationObserver !== 'undefined') {\n snapshotObserver = new MutationObserver(mutations => {\n for (const mutation of mutations) {\n for (const node of Array.from(mutation.addedNodes)) {\n if (!(node instanceof Element)) continue\n if (node.tagName === 'SCRIPT') {\n const script = node as HTMLScriptElement\n if (isSnapshotScript(script)) {\n parseSnapshotScript(script)\n }\n }\n const nested = node.querySelectorAll?.(\n 'script[type=\"application/json\"][data-fict-snapshot]',\n )\n if (nested && nested.length) {\n for (const script of Array.from(nested)) {\n parseSnapshotScript(script as HTMLScriptElement)\n }\n }\n }\n }\n })\n snapshotObserver.observe(doc.documentElement ?? doc, { childList: true, subtree: true })\n }\n\n __fictEnableResumable()\n\n const events = options.events ?? Array.from(DelegatedEvents)\n for (const eventName of events) {\n doc.addEventListener(eventName, handleResumableEvent, true)\n }\n\n // Store cleanup function for event listeners\n eventListenerCleanup = () => {\n for (const eventName of events) {\n doc.removeEventListener(eventName, handleResumableEvent, true)\n }\n }\n\n // Setup prefetch if enabled\n if (options.prefetch !== false) {\n prefetchCleanup = setupPrefetch(doc, options.prefetch ?? {})\n }\n}\n\nfunction isSnapshotScript(script: HTMLScriptElement): boolean {\n return script.type === 'application/json' && script.hasAttribute('data-fict-snapshot')\n}\n\nfunction parseSnapshotScript(script: HTMLScriptElement): void {\n if (processedSnapshots.has(script)) return\n processedSnapshots.add(script)\n const text = script.textContent\n if (!text) return\n const source = script.id ? `#${script.id}` : '<script[data-fict-snapshot]>'\n const state = parseSnapshotText(text, source)\n if (state) {\n __fictMergeSSRState(state)\n }\n}\n\nfunction parseSnapshotText(text: string, source: string): SSRState | null {\n let parsed: unknown\n try {\n parsed = JSON.parse(text)\n } catch {\n emitSnapshotIssue({\n code: 'snapshot_parse_error',\n message: '[fict/loader] Failed to parse SSR snapshot JSON.',\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n })\n return null\n }\n\n return normalizeSnapshotState(parsed, source)\n}\n\nfunction normalizeSnapshotState(value: unknown, source: string): SSRState | null {\n if (!isRecord(value)) {\n emitSnapshotIssue({\n code: 'snapshot_invalid_shape',\n message: '[fict/loader] Snapshot payload must be an object.',\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n })\n return null\n }\n\n const rawVersion = value.v\n const version = rawVersion === undefined ? FICT_SSR_SNAPSHOT_SCHEMA_VERSION : rawVersion\n if (!Number.isInteger(version) || version !== FICT_SSR_SNAPSHOT_SCHEMA_VERSION) {\n const versionIssue: SnapshotIssue = {\n code: 'snapshot_unsupported_version',\n message: `[fict/loader] Snapshot schema version ${String(version)} is not supported by this runtime.`,\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n }\n if (typeof version === 'number') {\n versionIssue.actualVersion = version\n }\n emitSnapshotIssue({\n ...versionIssue,\n })\n return null\n }\n\n const scopes = value.scopes\n if (!isRecord(scopes)) {\n emitSnapshotIssue({\n code: 'snapshot_invalid_shape',\n message: '[fict/loader] Snapshot payload is missing a valid `scopes` object.',\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n })\n return null\n }\n\n return { v: FICT_SSR_SNAPSHOT_SCHEMA_VERSION, scopes: scopes as SSRState['scopes'] }\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\nfunction emitSnapshotIssue(issue: SnapshotIssue): void {\n const key =\n `${issue.code}|${issue.source}|${issue.scopeId ?? ''}|` +\n `${issue.actualVersion ?? ''}|${issue.expectedVersion}`\n if (emittedIssueKeys.has(key)) return\n emittedIssueKeys.add(key)\n\n snapshotIssueHandler?.(issue)\n\n if (typeof console !== 'undefined' && typeof console.warn === 'function') {\n console.warn(issue.message)\n }\n}\n\n// ============================================================================\n// Prefetch Implementation\n// ============================================================================\n\nfunction setupPrefetch(doc: Document, strategy: PrefetchStrategy): () => void {\n const cleanupFns: (() => void)[] = []\n\n // Visibility-based prefetch\n if (strategy.visibility !== false) {\n const cleanup = setupVisibilityPrefetch(doc, strategy.visibilityMargin ?? '200px')\n cleanupFns.push(cleanup)\n }\n\n // Hover-based prefetch\n if (strategy.hover !== false) {\n const cleanup = setupHoverPrefetch(doc, strategy.hoverDelay ?? 50)\n cleanupFns.push(cleanup)\n }\n\n return () => {\n for (const cleanup of cleanupFns) {\n cleanup()\n }\n }\n}\n\nfunction setupVisibilityPrefetch(doc: Document, rootMargin: string): () => void {\n // Check if IntersectionObserver is available\n if (typeof IntersectionObserver === 'undefined') {\n return () => {}\n }\n\n const observer = new IntersectionObserver(\n entries => {\n for (const entry of entries) {\n if (entry.isIntersecting) {\n const el = entry.target as Element\n prefetchElementQrls(el)\n // Stop observing after prefetch\n observer.unobserve(el)\n }\n }\n },\n { rootMargin },\n )\n\n // Observe all elements with on:* attributes\n const interactiveElements = doc.querySelectorAll(\n '[on\\\\:click], [on\\\\:input], [on\\\\:change], [on\\\\:submit], [on\\\\:keydown], [on\\\\:keyup]',\n )\n interactiveElements.forEach(el => observer.observe(el))\n\n // Also observe elements with data-fict-h (resumable components)\n const resumableHosts = doc.querySelectorAll('[data-fict-h]')\n resumableHosts.forEach(el => observer.observe(el))\n\n return () => {\n observer.disconnect()\n }\n}\n\nfunction setupHoverPrefetch(doc: Document, delay: number): () => void {\n let hoverTimeout: ReturnType<typeof setTimeout> | null = null\n let lastHoveredElement: Element | null = null\n\n const handlePointerOver = (event: Event) => {\n const target = event.target\n if (!(target instanceof Element)) return\n\n // Find the closest element with interactive attributes\n const interactiveEl =\n target.closest('[on\\\\:click]') ||\n target.closest('[on\\\\:input]') ||\n target.closest('[on\\\\:change]') ||\n target.closest('[on\\\\:submit]') ||\n target.closest('[data-fict-h]')\n\n if (!interactiveEl || interactiveEl === lastHoveredElement) return\n\n lastHoveredElement = interactiveEl\n\n // Clear previous timeout\n if (hoverTimeout) {\n clearTimeout(hoverTimeout)\n }\n\n // Debounce prefetch\n hoverTimeout = setTimeout(() => {\n prefetchElementQrls(interactiveEl)\n }, delay)\n }\n\n const handlePointerOut = () => {\n if (hoverTimeout) {\n clearTimeout(hoverTimeout)\n hoverTimeout = null\n }\n lastHoveredElement = null\n }\n\n doc.addEventListener('pointerover', handlePointerOver, { passive: true })\n doc.addEventListener('pointerout', handlePointerOut, { passive: true })\n\n return () => {\n doc.removeEventListener('pointerover', handlePointerOver)\n doc.removeEventListener('pointerout', handlePointerOut)\n if (hoverTimeout) {\n clearTimeout(hoverTimeout)\n }\n }\n}\n\nfunction prefetchElementQrls(el: Element): void {\n const ownerDocument = el.ownerDocument ?? (typeof document !== 'undefined' ? document : undefined)\n // Prefetch event handler QRLs\n const eventAttrs = ['on:click', 'on:input', 'on:change', 'on:submit', 'on:keydown', 'on:keyup']\n for (const attr of eventAttrs) {\n const qrl = el.getAttribute(attr)\n if (qrl) {\n prefetchQrl(qrl, ownerDocument)\n }\n }\n\n // Prefetch resume handler QRL\n const resumeQrl = el.getAttribute('data-fict-h')\n if (resumeQrl) {\n prefetchQrl(resumeQrl, ownerDocument)\n }\n\n // Also check children for nested QRLs\n const children = el.querySelectorAll(\n '[on\\\\:click], [on\\\\:input], [on\\\\:change], [on\\\\:submit], [data-fict-h]',\n )\n children.forEach(child => {\n for (const attr of eventAttrs) {\n const qrl = child.getAttribute(attr)\n if (qrl) {\n prefetchQrl(qrl, ownerDocument)\n }\n }\n const childResumeQrl = child.getAttribute('data-fict-h')\n if (childResumeQrl) {\n prefetchQrl(childResumeQrl, ownerDocument)\n }\n })\n}\n\nfunction prefetchQrl(qrl: string, ownerDocument?: Document): void {\n const { url } = parseQrl(qrl)\n if (!url || prefetchedUrls.has(url)) return\n\n prefetchedUrls.add(url)\n\n // Resolve through manifest for production builds\n const resolvedUrl = resolveModuleUrl(url)\n\n // Use modulepreload link for best browser support\n const doc = ownerDocument ?? (typeof document !== 'undefined' ? document : undefined)\n if (doc) {\n const link = doc.createElement('link')\n link.rel = 'modulepreload'\n link.href = resolvedUrl\n link.crossOrigin = 'anonymous'\n doc.head?.appendChild(link)\n }\n}\n\n// ============================================================================\n\n/**\n * Wrapper that tracks the async handler promise for testing.\n */\nfunction handleResumableEvent(event: Event): void {\n const promise = handleResumableEventAsync(event)\n pendingHandlers.add(promise)\n void promise\n .catch(error => {\n if (typeof console !== 'undefined' && typeof console.error === 'function') {\n console.error('[fict/loader] Failed to handle resumable event.', error)\n }\n })\n .finally(() => {\n pendingHandlers.delete(promise)\n })\n}\n\nasync function handleResumableEventAsync(event: Event): Promise<void> {\n const path =\n typeof event.composedPath === 'function' ? event.composedPath() : buildEventPath(event)\n\n for (const node of path) {\n if (!(node instanceof Element)) continue\n const qrl = node.getAttribute(`on:${event.type}`)\n if (!qrl) continue\n\n const host = node.closest('[data-fict-s]') as Element | null\n if (!host) continue\n const scopeId = host.getAttribute('data-fict-s')\n if (!scopeId) continue\n\n const snapshot = __fictGetSSRScope(scopeId)\n if (!snapshot) {\n emitSnapshotIssue({\n code: 'scope_snapshot_missing',\n message: `[fict/loader] Missing scope snapshot for ${scopeId}; skipping resumable handler execution.`,\n source: 'event',\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n scopeId,\n })\n continue\n }\n __fictEnsureScope(scopeId, host, snapshot)\n\n const { url, exportName } = parseQrl(qrl)\n\n // Pre-emptively prevent default on navigations/forms while we await modules\n if (event.cancelable && (event.type === 'click' || event.type === 'submit')) {\n const tag = node.tagName.toLowerCase()\n if (tag === 'a' || tag === 'form') {\n event.preventDefault()\n }\n }\n\n // Resume FIRST to set up reactive bindings BEFORE the handler runs\n if (!hydratedScopes.has(scopeId)) {\n const resumeQrl = host.getAttribute('data-fict-h')\n if (resumeQrl) {\n const { url: resumeUrl, exportName: resumeExport } = parseQrl(resumeQrl)\n const resolvedResumeUrl = resolveModuleUrl(resumeUrl)\n // Load the module to ensure resume functions are registered\n await import(/* @vite-ignore */ resolvedResumeUrl)\n // Get resume function from registry (not module exports)\n const resumeFn = __fictGetResume(resumeExport)\n if (typeof resumeFn === 'function') {\n await (resumeFn as (scopeId: string, host: Element) => unknown)(scopeId, host)\n hydratedScopes.add(scopeId)\n }\n }\n }\n\n // THEN run the handler - now signal updates will trigger DOM updates\n const resolvedUrl = resolveModuleUrl(url)\n const mod = await import(/* @vite-ignore */ resolvedUrl)\n const handler = (mod as Record<string, unknown>)[exportName]\n if (typeof handler === 'function') {\n await (handler as (scopeId: string, ev: Event, el: Element) => unknown)(scopeId, event, node)\n }\n\n return\n }\n}\n\nfunction parseQrl(qrl: string): { url: string; exportName: string } {\n const [ref] = qrl.split('[')\n if (!ref) {\n return { url: '', exportName: 'default' }\n }\n const hashIndex = ref.lastIndexOf('#')\n if (hashIndex === -1) {\n return { url: ref, exportName: 'default' }\n }\n return { url: ref.slice(0, hashIndex), exportName: ref.slice(hashIndex + 1) }\n}\n\nfunction buildEventPath(event: Event): EventTarget[] {\n const path: EventTarget[] = []\n let node: EventTarget | null = event.target\n while (node) {\n path.push(node)\n node = (node as Node).parentNode\n }\n path.push(window)\n return path\n}\n\n// Re-export for handler authors (optional)\nexport { __fictUseLexicalScope } from './resume'\n"]}
1
+ {"version":3,"sources":["/home/runner/work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AACH,MAAA;AACF,IAAA;AAEA,IAAA;AACF,EAAA;AACF;AAES;AACG,EAAA;AACA,EAAA;AACD,IAAA;AACT,EAAA;AACM,EAAA;AACF,EAAA;AACK,IAAA;AACT,EAAA;AACS,EAAA;AACX;AAES;AACD,EAAA;AACF,EAAA;AACG,EAAA;AACA,IAAA;AACG,IAAA;AACV,EAAA;AACU,EAAA;AACH,EAAA;AACT;ADzOY;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/fict/fict/packages/runtime/dist/loader.cjs","sourcesContent":[null,"import { DelegatedEvents } from './constants'\nimport {\n FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n type SSRState,\n __fictEnableResumable,\n __fictEnsureScope,\n __fictGetResume,\n __fictGetSSRScope,\n __fictMergeSSRState,\n __fictSetSSRState,\n __fictUseLexicalScope,\n} from './resume'\n\n// ============================================================================\n// Module Resolution\n// ============================================================================\n\n/**\n * Resolve a module URL through the manifest if available.\n * In production, virtual module URLs (virtual:fict-handler:...) are mapped\n * to their built chunk URLs through the manifest.\n */\nfunction resolveModuleUrl(url: string): string {\n const manifest = (globalThis as Record<string, unknown>).__FICT_MANIFEST__ as\n | Record<string, string>\n | undefined\n\n if (manifest) {\n // Check if the URL (without #fragment) is in the manifest\n const resolved = manifest[url]\n if (resolved) {\n return resolved\n }\n }\n\n return url\n}\n\nfunction resolveAbsoluteModuleUrl(url: string, ownerDocument?: Document): string {\n const baseUrl =\n ownerDocument?.baseURI ?? (typeof document !== 'undefined' ? document.baseURI : undefined)\n if (!baseUrl) return url\n\n try {\n return new URL(url, baseUrl).href\n } catch {\n return url\n }\n}\n\nfunction normalizeImportUrl(url: string): string {\n if (!url.startsWith('data:')) {\n return url\n }\n\n const dataSeparatorIndex = url.indexOf(',')\n if (dataSeparatorIndex === -1) {\n return url\n }\n\n const metadata = url.slice(0, dataSeparatorIndex)\n const payload = url.slice(dataSeparatorIndex + 1)\n if (metadata.includes(';base64')) {\n return url\n }\n\n try {\n return `${metadata},${encodeURIComponent(decodeURIComponent(payload))}`\n } catch {\n return `${metadata},${encodeURIComponent(payload)}`\n }\n}\n\ninterface PreservedControlState {\n value?: string\n checked?: boolean\n selectedIndex?: number\n selectedValues?: string[]\n}\n\nfunction captureControlState(node: Element, event: Event): PreservedControlState | null {\n if (event.type !== 'input' && event.type !== 'change') return null\n\n if (node instanceof HTMLInputElement) {\n if (node.type === 'file') return null\n if (node.type === 'checkbox' || node.type === 'radio') {\n return { checked: node.checked }\n }\n return { value: node.value }\n }\n\n if (node instanceof HTMLTextAreaElement) {\n return { value: node.value }\n }\n\n if (node instanceof HTMLSelectElement) {\n return node.multiple\n ? {\n selectedValues: Array.from(node.selectedOptions).map(option => option.value),\n }\n : {\n value: node.value,\n selectedIndex: node.selectedIndex,\n }\n }\n\n return null\n}\n\nfunction restoreControlState(node: Element, state: PreservedControlState | null): void {\n if (!state) return\n\n if (node instanceof HTMLInputElement) {\n if (typeof state.checked === 'boolean') {\n node.checked = state.checked\n }\n if (typeof state.value === 'string' && node.type !== 'file') {\n node.value = state.value\n }\n return\n }\n\n if (node instanceof HTMLTextAreaElement) {\n if (typeof state.value === 'string') {\n node.value = state.value\n }\n return\n }\n\n if (node instanceof HTMLSelectElement) {\n if (Array.isArray(state.selectedValues) && node.multiple) {\n const selected = new Set(state.selectedValues)\n for (const option of Array.from(node.options)) {\n option.selected = selected.has(option.value)\n }\n return\n }\n\n if (typeof state.selectedIndex === 'number') {\n node.selectedIndex = state.selectedIndex\n }\n if (typeof state.value === 'string') {\n node.value = state.value\n }\n }\n}\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PrefetchStrategy {\n /**\n * Enable visibility-based prefetch using IntersectionObserver.\n * Prefetches modules when interactive elements come into view.\n * @default true\n */\n visibility?: boolean\n /**\n * Root margin for IntersectionObserver (e.g., '200px' to prefetch earlier).\n * @default '200px'\n */\n visibilityMargin?: string\n /**\n * Enable hover-based prefetch using pointerover events.\n * Prefetches modules when user hovers over interactive elements.\n * @default true\n */\n hover?: boolean\n /**\n * Delay in ms before prefetching on hover (debounce rapid movements).\n * @default 50\n */\n hoverDelay?: number\n}\n\nexport interface ResumableLoaderOptions {\n document?: Document\n snapshotScriptId?: string\n events?: string[]\n /**\n * Receives structured snapshot/resume issues detected by the loader.\n * Useful for telemetry and fail-safe fallback orchestration.\n */\n onSnapshotIssue?: (issue: SnapshotIssue) => void\n /**\n * Prefetch strategy configuration.\n * Set to false to disable all prefetching.\n * @default { visibility: true, hover: true }\n */\n prefetch?: PrefetchStrategy | false\n}\n\nexport type SnapshotIssueCode =\n | 'snapshot_parse_error'\n | 'snapshot_invalid_shape'\n | 'snapshot_unsupported_version'\n | 'scope_snapshot_missing'\n\nexport interface SnapshotIssue {\n code: SnapshotIssueCode\n message: string\n source: string\n expectedVersion: number\n actualVersion?: number\n scopeId?: string\n}\n\n// ============================================================================\n// State\n// ============================================================================\n\nconst hydratedScopes = new Set<string>()\nconst prefetchedUrls = new Set<string>()\nlet prefetchCleanup: (() => void) | null = null\nlet eventListenerCleanup: (() => void) | null = null\nlet snapshotObserver: MutationObserver | null = null\nconst processedSnapshots = new Set<HTMLScriptElement>()\nlet snapshotIssueHandler: ((issue: SnapshotIssue) => void) | null = null\nconst emittedIssueKeys = new Set<string>()\n\n/**\n * Reset the hydrated scopes set. Useful for testing.\n */\nexport function resetHydratedScopes(): void {\n hydratedScopes.clear()\n}\n\n/**\n * Reset the prefetched URLs set. Useful for testing.\n */\nexport function resetPrefetchedUrls(): void {\n prefetchedUrls.clear()\n}\n\n/**\n * Set of pending handler promises. Used for testing to wait for all handlers to complete.\n */\nconst pendingHandlers = new Set<Promise<void>>()\n\n/**\n * Wait for all pending event handlers to complete. Useful for testing.\n */\nexport async function waitForPendingHandlers(): Promise<void> {\n if (pendingHandlers.size === 0) return\n await Promise.allSettled([...pendingHandlers])\n}\n\n/**\n * Clean up all registered event listeners. Useful for testing.\n */\nexport function cleanupEventListeners(): void {\n if (eventListenerCleanup) {\n eventListenerCleanup()\n eventListenerCleanup = null\n }\n}\n\n// ============================================================================\n// Main Entry Point\n// ============================================================================\n\nexport function installResumableLoader(options: ResumableLoaderOptions = {}): void {\n const doc = options.document ?? window.document\n const scriptId = options.snapshotScriptId ?? '__FICT_SNAPSHOT__'\n snapshotIssueHandler = options.onSnapshotIssue ?? null\n\n // Reset hydrated scopes for fresh loader installation\n hydratedScopes.clear()\n prefetchedUrls.clear()\n processedSnapshots.clear()\n emittedIssueKeys.clear()\n __fictSetSSRState(null)\n\n // Clean up previous event listeners\n if (eventListenerCleanup) {\n eventListenerCleanup()\n eventListenerCleanup = null\n }\n\n // Clean up previous prefetch handlers\n if (prefetchCleanup) {\n prefetchCleanup()\n prefetchCleanup = null\n }\n\n if (snapshotObserver) {\n snapshotObserver.disconnect()\n snapshotObserver = null\n }\n\n const snapshotEl = doc.getElementById(scriptId)\n if (snapshotEl?.textContent) {\n const state = parseSnapshotText(snapshotEl.textContent, `#${scriptId}`)\n if (state) {\n __fictSetSSRState(state)\n }\n }\n\n const snapshotScripts = doc.querySelectorAll(\n 'script[type=\"application/json\"][data-fict-snapshot]',\n )\n for (const script of Array.from(snapshotScripts)) {\n parseSnapshotScript(script as HTMLScriptElement)\n }\n\n if (typeof MutationObserver !== 'undefined') {\n snapshotObserver = new MutationObserver(mutations => {\n for (const mutation of mutations) {\n for (const node of Array.from(mutation.addedNodes)) {\n if (!(node instanceof Element)) continue\n if (node.tagName === 'SCRIPT') {\n const script = node as HTMLScriptElement\n if (isSnapshotScript(script)) {\n parseSnapshotScript(script)\n }\n }\n const nested = node.querySelectorAll?.(\n 'script[type=\"application/json\"][data-fict-snapshot]',\n )\n if (nested && nested.length) {\n for (const script of Array.from(nested)) {\n parseSnapshotScript(script as HTMLScriptElement)\n }\n }\n }\n }\n })\n snapshotObserver.observe(doc.documentElement ?? doc, { childList: true, subtree: true })\n }\n\n __fictEnableResumable()\n\n const events = options.events ?? Array.from(DelegatedEvents)\n for (const eventName of events) {\n doc.addEventListener(eventName, handleResumableEvent, true)\n }\n\n // Store cleanup function for event listeners\n eventListenerCleanup = () => {\n for (const eventName of events) {\n doc.removeEventListener(eventName, handleResumableEvent, true)\n }\n }\n\n // Setup prefetch if enabled\n if (options.prefetch !== false) {\n prefetchCleanup = setupPrefetch(doc, options.prefetch ?? {})\n }\n}\n\nfunction isSnapshotScript(script: HTMLScriptElement): boolean {\n return script.type === 'application/json' && script.hasAttribute('data-fict-snapshot')\n}\n\nfunction parseSnapshotScript(script: HTMLScriptElement): void {\n if (processedSnapshots.has(script)) return\n processedSnapshots.add(script)\n const text = script.textContent\n if (!text) return\n const source = script.id ? `#${script.id}` : '<script[data-fict-snapshot]>'\n const state = parseSnapshotText(text, source)\n if (state) {\n __fictMergeSSRState(state)\n }\n}\n\nfunction parseSnapshotText(text: string, source: string): SSRState | null {\n let parsed: unknown\n try {\n parsed = JSON.parse(text)\n } catch {\n emitSnapshotIssue({\n code: 'snapshot_parse_error',\n message: '[fict/loader] Failed to parse SSR snapshot JSON.',\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n })\n return null\n }\n\n return normalizeSnapshotState(parsed, source)\n}\n\nfunction normalizeSnapshotState(value: unknown, source: string): SSRState | null {\n if (!isRecord(value)) {\n emitSnapshotIssue({\n code: 'snapshot_invalid_shape',\n message: '[fict/loader] Snapshot payload must be an object.',\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n })\n return null\n }\n\n const rawVersion = value.v\n const version = rawVersion === undefined ? FICT_SSR_SNAPSHOT_SCHEMA_VERSION : rawVersion\n if (!Number.isInteger(version) || version !== FICT_SSR_SNAPSHOT_SCHEMA_VERSION) {\n const versionIssue: SnapshotIssue = {\n code: 'snapshot_unsupported_version',\n message: `[fict/loader] Snapshot schema version ${String(version)} is not supported by this runtime.`,\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n }\n if (typeof version === 'number') {\n versionIssue.actualVersion = version\n }\n emitSnapshotIssue({\n ...versionIssue,\n })\n return null\n }\n\n const scopes = value.scopes\n if (!isRecord(scopes)) {\n emitSnapshotIssue({\n code: 'snapshot_invalid_shape',\n message: '[fict/loader] Snapshot payload is missing a valid `scopes` object.',\n source,\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n })\n return null\n }\n\n return { v: FICT_SSR_SNAPSHOT_SCHEMA_VERSION, scopes: scopes as SSRState['scopes'] }\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\nfunction emitSnapshotIssue(issue: SnapshotIssue): void {\n const key =\n `${issue.code}|${issue.source}|${issue.scopeId ?? ''}|` +\n `${issue.actualVersion ?? ''}|${issue.expectedVersion}`\n if (emittedIssueKeys.has(key)) return\n emittedIssueKeys.add(key)\n\n snapshotIssueHandler?.(issue)\n\n if (typeof console !== 'undefined' && typeof console.warn === 'function') {\n console.warn(issue.message)\n }\n}\n\n// ============================================================================\n// Prefetch Implementation\n// ============================================================================\n\nfunction setupPrefetch(doc: Document, strategy: PrefetchStrategy): () => void {\n const cleanupFns: (() => void)[] = []\n\n // Visibility-based prefetch\n if (strategy.visibility !== false) {\n const cleanup = setupVisibilityPrefetch(doc, strategy.visibilityMargin ?? '200px')\n cleanupFns.push(cleanup)\n }\n\n // Hover-based prefetch\n if (strategy.hover !== false) {\n const cleanup = setupHoverPrefetch(doc, strategy.hoverDelay ?? 50)\n cleanupFns.push(cleanup)\n }\n\n return () => {\n for (const cleanup of cleanupFns) {\n cleanup()\n }\n }\n}\n\nfunction setupVisibilityPrefetch(doc: Document, rootMargin: string): () => void {\n // Check if IntersectionObserver is available\n if (typeof IntersectionObserver === 'undefined') {\n return () => {}\n }\n\n const observer = new IntersectionObserver(\n entries => {\n for (const entry of entries) {\n if (entry.isIntersecting) {\n const el = entry.target as Element\n prefetchElementQrls(el)\n // Stop observing after prefetch\n observer.unobserve(el)\n }\n }\n },\n { rootMargin },\n )\n\n // Observe all elements with on:* attributes\n const interactiveElements = doc.querySelectorAll(\n '[on\\\\:click], [on\\\\:input], [on\\\\:change], [on\\\\:submit], [on\\\\:keydown], [on\\\\:keyup]',\n )\n interactiveElements.forEach(el => observer.observe(el))\n\n // Also observe elements with data-fict-h (resumable components)\n const resumableHosts = doc.querySelectorAll('[data-fict-h]')\n resumableHosts.forEach(el => observer.observe(el))\n\n return () => {\n observer.disconnect()\n }\n}\n\nfunction setupHoverPrefetch(doc: Document, delay: number): () => void {\n let hoverTimeout: ReturnType<typeof setTimeout> | null = null\n let lastHoveredElement: Element | null = null\n\n const handlePointerOver = (event: Event) => {\n const target = event.target\n if (!(target instanceof Element)) return\n\n // Find the closest element with interactive attributes\n const interactiveEl =\n target.closest('[on\\\\:click]') ||\n target.closest('[on\\\\:input]') ||\n target.closest('[on\\\\:change]') ||\n target.closest('[on\\\\:submit]') ||\n target.closest('[data-fict-h]')\n\n if (!interactiveEl || interactiveEl === lastHoveredElement) return\n\n lastHoveredElement = interactiveEl\n\n // Clear previous timeout\n if (hoverTimeout) {\n clearTimeout(hoverTimeout)\n }\n\n // Debounce prefetch\n hoverTimeout = setTimeout(() => {\n prefetchElementQrls(interactiveEl)\n }, delay)\n }\n\n const handlePointerOut = () => {\n if (hoverTimeout) {\n clearTimeout(hoverTimeout)\n hoverTimeout = null\n }\n lastHoveredElement = null\n }\n\n doc.addEventListener('pointerover', handlePointerOver, { passive: true })\n doc.addEventListener('pointerout', handlePointerOut, { passive: true })\n\n return () => {\n doc.removeEventListener('pointerover', handlePointerOver)\n doc.removeEventListener('pointerout', handlePointerOut)\n if (hoverTimeout) {\n clearTimeout(hoverTimeout)\n }\n }\n}\n\nfunction prefetchElementQrls(el: Element): void {\n const ownerDocument = el.ownerDocument ?? (typeof document !== 'undefined' ? document : undefined)\n // Prefetch event handler QRLs\n const eventAttrs = ['on:click', 'on:input', 'on:change', 'on:submit', 'on:keydown', 'on:keyup']\n for (const attr of eventAttrs) {\n const qrl = el.getAttribute(attr)\n if (qrl) {\n prefetchQrl(qrl, ownerDocument)\n }\n }\n\n // Prefetch resume handler QRL\n const resumeQrl = el.getAttribute('data-fict-h')\n if (resumeQrl) {\n prefetchQrl(resumeQrl, ownerDocument)\n }\n\n // Also check children for nested QRLs\n const children = el.querySelectorAll(\n '[on\\\\:click], [on\\\\:input], [on\\\\:change], [on\\\\:submit], [data-fict-h]',\n )\n children.forEach(child => {\n for (const attr of eventAttrs) {\n const qrl = child.getAttribute(attr)\n if (qrl) {\n prefetchQrl(qrl, ownerDocument)\n }\n }\n const childResumeQrl = child.getAttribute('data-fict-h')\n if (childResumeQrl) {\n prefetchQrl(childResumeQrl, ownerDocument)\n }\n })\n}\n\nfunction prefetchQrl(qrl: string, ownerDocument?: Document): void {\n const { url } = parseQrl(qrl)\n if (!url || prefetchedUrls.has(url)) return\n\n prefetchedUrls.add(url)\n\n // Resolve through manifest for production builds\n const resolvedUrl = resolveModuleUrl(url)\n\n // Use modulepreload link for best browser support\n const doc = ownerDocument ?? (typeof document !== 'undefined' ? document : undefined)\n if (doc) {\n const link = doc.createElement('link')\n link.rel = 'modulepreload'\n link.href = resolvedUrl\n link.crossOrigin = 'anonymous'\n doc.head?.appendChild(link)\n }\n}\n\n// ============================================================================\n\n/**\n * Wrapper that tracks the async handler promise for testing.\n */\nfunction handleResumableEvent(event: Event): void {\n const promise = handleResumableEventAsync(event)\n pendingHandlers.add(promise)\n void promise\n .catch(error => {\n if (typeof console !== 'undefined' && typeof console.error === 'function') {\n console.error('[fict/loader] Failed to handle resumable event.', error)\n }\n })\n .finally(() => {\n pendingHandlers.delete(promise)\n })\n}\n\nasync function handleResumableEventAsync(event: Event): Promise<void> {\n const path =\n typeof event.composedPath === 'function' ? event.composedPath() : buildEventPath(event)\n\n for (const node of path) {\n if (!(node instanceof Element)) continue\n const qrl = node.getAttribute(`on:${event.type}`)\n if (!qrl) continue\n\n const host = node.closest('[data-fict-s]') as Element | null\n if (!host) continue\n const scopeId = host.getAttribute('data-fict-s')\n if (!scopeId) continue\n\n const snapshot = __fictGetSSRScope(scopeId)\n if (!snapshot) {\n emitSnapshotIssue({\n code: 'scope_snapshot_missing',\n message: `[fict/loader] Missing scope snapshot for ${scopeId}; skipping resumable handler execution.`,\n source: 'event',\n expectedVersion: FICT_SSR_SNAPSHOT_SCHEMA_VERSION,\n scopeId,\n })\n continue\n }\n __fictEnsureScope(scopeId, host, snapshot)\n\n const { url, exportName } = parseQrl(qrl)\n\n // Pre-emptively prevent default on navigations/forms while we await modules\n if (event.cancelable && (event.type === 'click' || event.type === 'submit')) {\n const tag = node.tagName.toLowerCase()\n if (tag === 'a' || tag === 'form') {\n event.preventDefault()\n }\n }\n\n const preservedControlState = captureControlState(node, event)\n let resumedDuringEvent = false\n\n // Resume FIRST to set up reactive bindings BEFORE the handler runs\n if (!hydratedScopes.has(scopeId)) {\n const resumeQrl = host.getAttribute('data-fict-h')\n if (resumeQrl) {\n const { url: resumeUrl, exportName: resumeExport } = parseQrl(resumeQrl)\n const resolvedResumeUrl = resolveModuleUrl(resumeUrl)\n const resolvedAbsoluteResumeUrl = resolveAbsoluteModuleUrl(\n resolvedResumeUrl,\n host.ownerDocument ?? undefined,\n )\n const resolvedResumeQrl = `${resolvedResumeUrl}#${resumeExport}`\n const resolvedAbsoluteResumeQrl = `${resolvedAbsoluteResumeUrl}#${resumeExport}`\n const normalizedResumeImportUrl = normalizeImportUrl(resolvedResumeUrl)\n // Load the module to ensure resume functions are registered\n await import(/* @vite-ignore */ normalizedResumeImportUrl)\n // Get resume function from registry (not module exports)\n const resumeFn =\n __fictGetResume(resumeQrl) ??\n __fictGetResume(resolvedResumeQrl) ??\n __fictGetResume(resolvedAbsoluteResumeQrl) ??\n __fictGetResume(resumeExport)\n if (typeof resumeFn === 'function') {\n await (resumeFn as (scopeId: string, host: Element) => unknown)(scopeId, host)\n hydratedScopes.add(scopeId)\n resumedDuringEvent = true\n }\n }\n }\n\n if (resumedDuringEvent) {\n restoreControlState(node, preservedControlState)\n }\n\n // THEN run the handler - now signal updates will trigger DOM updates\n const resolvedUrl = resolveModuleUrl(url)\n const normalizedImportUrl = normalizeImportUrl(resolvedUrl)\n const mod = await import(/* @vite-ignore */ normalizedImportUrl)\n const handler = (mod as Record<string, unknown>)[exportName]\n if (typeof handler === 'function') {\n const originalCurrentTarget = event.currentTarget\n Object.defineProperty(event, 'currentTarget', {\n configurable: true,\n get() {\n return node\n },\n })\n try {\n await (handler as (scopeId: string, ev: Event, el: Element) => unknown)(\n scopeId,\n event,\n node,\n )\n } finally {\n Object.defineProperty(event, 'currentTarget', {\n configurable: true,\n get() {\n return originalCurrentTarget\n },\n })\n }\n }\n\n return\n }\n}\n\nfunction parseQrl(qrl: string): { url: string; exportName: string } {\n const [ref] = qrl.split('[')\n if (!ref) {\n return { url: '', exportName: 'default' }\n }\n const hashIndex = ref.lastIndexOf('#')\n if (hashIndex === -1) {\n return { url: ref, exportName: 'default' }\n }\n return { url: ref.slice(0, hashIndex), exportName: ref.slice(hashIndex + 1) }\n}\n\nfunction buildEventPath(event: Event): EventTarget[] {\n const path: EventTarget[] = []\n let node: EventTarget | null = event.target\n while (node) {\n path.push(node)\n node = (node as Node).parentNode\n }\n path.push(window)\n return path\n}\n\n// Re-export for handler authors (optional)\nexport { __fictUseLexicalScope } from './resume'\n"]}
package/dist/loader.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- export { _ as __fictUseLexicalScope } from './resume-DPZxmA95.cjs';
1
+ export { _ as __fictUseLexicalScope } from './resume-C20cRVj9.cjs';
2
2
  import './signal-Z4KkDk9h.cjs';
3
3
 
4
4
  interface PrefetchStrategy {
package/dist/loader.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { _ as __fictUseLexicalScope } from './resume-C5IKAIdh.js';
1
+ export { _ as __fictUseLexicalScope } from './resume-C166aAVg.js';
2
2
  import './signal-Z4KkDk9h.js';
3
3
 
4
4
  interface PrefetchStrategy {
package/dist/loader.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  __fictMergeSSRState,
9
9
  __fictSetSSRState,
10
10
  __fictUseLexicalScope
11
- } from "./chunk-MAHWGB55.js";
11
+ } from "./chunk-CFAWL76V.js";
12
12
 
13
13
  // src/loader.ts
14
14
  function resolveModuleUrl(url) {
@@ -21,6 +21,89 @@ function resolveModuleUrl(url) {
21
21
  }
22
22
  return url;
23
23
  }
24
+ function resolveAbsoluteModuleUrl(url, ownerDocument) {
25
+ const baseUrl = ownerDocument?.baseURI ?? (typeof document !== "undefined" ? document.baseURI : void 0);
26
+ if (!baseUrl) return url;
27
+ try {
28
+ return new URL(url, baseUrl).href;
29
+ } catch {
30
+ return url;
31
+ }
32
+ }
33
+ function normalizeImportUrl(url) {
34
+ if (!url.startsWith("data:")) {
35
+ return url;
36
+ }
37
+ const dataSeparatorIndex = url.indexOf(",");
38
+ if (dataSeparatorIndex === -1) {
39
+ return url;
40
+ }
41
+ const metadata = url.slice(0, dataSeparatorIndex);
42
+ const payload = url.slice(dataSeparatorIndex + 1);
43
+ if (metadata.includes(";base64")) {
44
+ return url;
45
+ }
46
+ try {
47
+ return `${metadata},${encodeURIComponent(decodeURIComponent(payload))}`;
48
+ } catch {
49
+ return `${metadata},${encodeURIComponent(payload)}`;
50
+ }
51
+ }
52
+ function captureControlState(node, event) {
53
+ if (event.type !== "input" && event.type !== "change") return null;
54
+ if (node instanceof HTMLInputElement) {
55
+ if (node.type === "file") return null;
56
+ if (node.type === "checkbox" || node.type === "radio") {
57
+ return { checked: node.checked };
58
+ }
59
+ return { value: node.value };
60
+ }
61
+ if (node instanceof HTMLTextAreaElement) {
62
+ return { value: node.value };
63
+ }
64
+ if (node instanceof HTMLSelectElement) {
65
+ return node.multiple ? {
66
+ selectedValues: Array.from(node.selectedOptions).map((option) => option.value)
67
+ } : {
68
+ value: node.value,
69
+ selectedIndex: node.selectedIndex
70
+ };
71
+ }
72
+ return null;
73
+ }
74
+ function restoreControlState(node, state) {
75
+ if (!state) return;
76
+ if (node instanceof HTMLInputElement) {
77
+ if (typeof state.checked === "boolean") {
78
+ node.checked = state.checked;
79
+ }
80
+ if (typeof state.value === "string" && node.type !== "file") {
81
+ node.value = state.value;
82
+ }
83
+ return;
84
+ }
85
+ if (node instanceof HTMLTextAreaElement) {
86
+ if (typeof state.value === "string") {
87
+ node.value = state.value;
88
+ }
89
+ return;
90
+ }
91
+ if (node instanceof HTMLSelectElement) {
92
+ if (Array.isArray(state.selectedValues) && node.multiple) {
93
+ const selected = new Set(state.selectedValues);
94
+ for (const option of Array.from(node.options)) {
95
+ option.selected = selected.has(option.value);
96
+ }
97
+ return;
98
+ }
99
+ if (typeof state.selectedIndex === "number") {
100
+ node.selectedIndex = state.selectedIndex;
101
+ }
102
+ if (typeof state.value === "string") {
103
+ node.value = state.value;
104
+ }
105
+ }
106
+ }
24
107
  var hydratedScopes = /* @__PURE__ */ new Set();
25
108
  var prefetchedUrls = /* @__PURE__ */ new Set();
26
109
  var prefetchCleanup = null;
@@ -357,30 +440,64 @@ async function handleResumableEventAsync(event) {
357
440
  event.preventDefault();
358
441
  }
359
442
  }
443
+ const preservedControlState = captureControlState(node, event);
444
+ let resumedDuringEvent = false;
360
445
  if (!hydratedScopes.has(scopeId)) {
361
446
  const resumeQrl = host.getAttribute("data-fict-h");
362
447
  if (resumeQrl) {
363
448
  const { url: resumeUrl, exportName: resumeExport } = parseQrl(resumeQrl);
364
449
  const resolvedResumeUrl = resolveModuleUrl(resumeUrl);
450
+ const resolvedAbsoluteResumeUrl = resolveAbsoluteModuleUrl(
451
+ resolvedResumeUrl,
452
+ host.ownerDocument ?? void 0
453
+ );
454
+ const resolvedResumeQrl = `${resolvedResumeUrl}#${resumeExport}`;
455
+ const resolvedAbsoluteResumeQrl = `${resolvedAbsoluteResumeUrl}#${resumeExport}`;
456
+ const normalizedResumeImportUrl = normalizeImportUrl(resolvedResumeUrl);
365
457
  await import(
366
458
  /* @vite-ignore */
367
- resolvedResumeUrl
459
+ normalizedResumeImportUrl
368
460
  );
369
- const resumeFn = __fictGetResume(resumeExport);
461
+ const resumeFn = __fictGetResume(resumeQrl) ?? __fictGetResume(resolvedResumeQrl) ?? __fictGetResume(resolvedAbsoluteResumeQrl) ?? __fictGetResume(resumeExport);
370
462
  if (typeof resumeFn === "function") {
371
463
  await resumeFn(scopeId, host);
372
464
  hydratedScopes.add(scopeId);
465
+ resumedDuringEvent = true;
373
466
  }
374
467
  }
375
468
  }
469
+ if (resumedDuringEvent) {
470
+ restoreControlState(node, preservedControlState);
471
+ }
376
472
  const resolvedUrl = resolveModuleUrl(url);
473
+ const normalizedImportUrl = normalizeImportUrl(resolvedUrl);
377
474
  const mod = await import(
378
475
  /* @vite-ignore */
379
- resolvedUrl
476
+ normalizedImportUrl
380
477
  );
381
478
  const handler = mod[exportName];
382
479
  if (typeof handler === "function") {
383
- await handler(scopeId, event, node);
480
+ const originalCurrentTarget = event.currentTarget;
481
+ Object.defineProperty(event, "currentTarget", {
482
+ configurable: true,
483
+ get() {
484
+ return node;
485
+ }
486
+ });
487
+ try {
488
+ await handler(
489
+ scopeId,
490
+ event,
491
+ node
492
+ );
493
+ } finally {
494
+ Object.defineProperty(event, "currentTarget", {
495
+ configurable: true,
496
+ get() {
497
+ return originalCurrentTarget;
498
+ }
499
+ });
500
+ }
384
501
  }
385
502
  return;
386
503
  }