gyosjs 0.5.7 → 0.6.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.
package/dist/gyos.csp.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * GyosJS v0.5.7
2
+ * GyosJS v0.6.0
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -14316,8 +14316,10 @@
14316
14316
  while (idx < toChildren.length || idx < fromChildren.length) {
14317
14317
  const toChild = toChildren[idx] || null;
14318
14318
  let fromChild = fromChildren[idx] || null;
14319
- // Move an existing keyed child into the requested position before morphing it.
14320
- const requestedKey = explicitKeyFor(toChild);
14319
+ // Persist placeholders have their own placement path below, even when
14320
+ // they also carry an id or another explicit morph key.
14321
+ const toPersistKey = getPersistKey(toChild);
14322
+ const requestedKey = toPersistKey ? null : explicitKeyFor(toChild);
14321
14323
  if (requestedKey && explicitKeyFor(fromChild) !== requestedKey) {
14322
14324
  const matchingIndex = fromChildren.findIndex((candidate, candidateIndex) => candidateIndex > idx && explicitKeyFor(candidate) === requestedKey);
14323
14325
  if (matchingIndex !== -1) {
@@ -14332,7 +14334,6 @@
14332
14334
  }
14333
14335
  }
14334
14336
  // Check if either is persist comment/element
14335
- const toPersistKey = getPersistKey(toChild);
14336
14337
  const fromPersistKey = getPersistKey(fromChild);
14337
14338
  // If toChild is persist, check if we already have matching persist in fromChildren
14338
14339
  if (toPersistKey) {
@@ -14767,7 +14768,7 @@
14767
14768
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
14768
14769
  */
14769
14770
  const submittingForms = new WeakSet();
14770
- async function visit(url, options, fresh) {
14771
+ async function visit(url, options, refreshPolicy) {
14771
14772
  const destination = new URL(url, document.baseURI);
14772
14773
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
14773
14774
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -14784,21 +14785,27 @@
14784
14785
  url: destination.href, method: 'GET',
14785
14786
  changeState: options.history !== 'none', replace: options.history === 'replace',
14786
14787
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
14787
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
14788
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
14788
14789
  onEnd: context => { result = context; }
14789
14790
  });
14790
14791
  return result;
14791
14792
  }
14792
14793
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
14793
14794
  function navigate(url, options = {}) {
14794
- return visit(url, options, false);
14795
+ return visit(url, options);
14795
14796
  }
14796
14797
  /** Fetch the current URL again, preserving history and scroll by default. */
14797
- function refresh(options = {}) {
14798
- return visit(location.href, { history: 'none', scroll: false, ...options }, true);
14798
+ function refresh({ background, strictSource, ...options } = {}) {
14799
+ if (strictSource === true && options.target === undefined) {
14800
+ return Promise.reject(new TypeError('Gyos.refresh with strictSource requires a target selector.'));
14801
+ }
14802
+ return visit(location.href, { history: 'none', scroll: false, ...options }, {
14803
+ fresh: true, background: background === true, strictSource: strictSource === true
14804
+ });
14799
14805
  }
14800
14806
  const snapshots = new Map();
14801
14807
  const preloadCache = new Map();
14808
+ let preloadHover;
14802
14809
  const scrollPositions = new Map();
14803
14810
  const historyFragments = new Map();
14804
14811
  let routerStarted = false;
@@ -14808,6 +14815,7 @@
14808
14815
  let currentUrl = canonicalUrl(window.location.href, true);
14809
14816
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
14810
14817
  let activeNavigation = null;
14818
+ let waitingNavigations = 0;
14811
14819
  // Global progress bar instance
14812
14820
  let progressBar = null;
14813
14821
  const beforeNavigate = [];
@@ -14853,6 +14861,8 @@
14853
14861
  document.addEventListener('click', onClick);
14854
14862
  document.addEventListener('submit', onSubmit);
14855
14863
  document.addEventListener('mouseover', onMouseOverCapture, true);
14864
+ document.addEventListener('mousemove', onPreloadMouseMove, true);
14865
+ document.addEventListener('mouseleave', clearPreloadHover);
14856
14866
  window.addEventListener('popstate', onPopState);
14857
14867
  if ('scrollRestoration' in history) {
14858
14868
  history.scrollRestoration = 'manual';
@@ -15124,6 +15134,7 @@
15124
15134
  entry?.controller.abort();
15125
15135
  }
15126
15136
  function clearPreloads() {
15137
+ clearPreloadHover();
15127
15138
  for (const key of preloadCache.keys())
15128
15139
  discardPreload(key);
15129
15140
  }
@@ -15198,19 +15209,27 @@
15198
15209
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
15199
15210
  }
15200
15211
  async function navigateRequest(opts) {
15201
- const finishUnstarted = () => {
15212
+ const finishUnstarted = (signal = opts.signal) => {
15202
15213
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
15203
15214
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
15204
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
15215
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
15205
15216
  };
15206
15217
  if (opts.signal?.aborted) {
15207
15218
  finishUnstarted();
15208
15219
  return;
15209
15220
  }
15221
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
15222
+ const skipped = new AbortController();
15223
+ skipped.abort();
15224
+ finishUnstarted(skipped.signal);
15225
+ return;
15226
+ }
15210
15227
  while (activeNavigation) {
15211
15228
  const previous = activeNavigation;
15212
15229
  if (previous.committing) {
15213
15230
  log('[GyosRouter] Waiting for previous DOM commit');
15231
+ // End hooks can request a refresh before this waiter resumes.
15232
+ waitingNavigations++;
15214
15233
  try {
15215
15234
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
15216
15235
  }
@@ -15218,6 +15237,9 @@
15218
15237
  finishUnstarted();
15219
15238
  return;
15220
15239
  }
15240
+ finally {
15241
+ waitingNavigations--;
15242
+ }
15221
15243
  continue;
15222
15244
  }
15223
15245
  log('[GyosRouter] Cancelling previous navigation');
@@ -15252,14 +15274,16 @@
15252
15274
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
15253
15275
  : null;
15254
15276
  const fallback = (url, requestSent) => {
15255
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
15277
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
15256
15278
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
15257
- fallbackFromBoost(opts, url, requestSent);
15279
+ if (!opts.background)
15280
+ fallbackFromBoost(opts, url, requestSent);
15258
15281
  };
15282
+ const navigationProgress = opts.background ? null : progressBar;
15259
15283
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
15260
15284
  const setRand = () => {
15261
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
15262
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
15285
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
15286
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
15263
15287
  }
15264
15288
  };
15265
15289
  try {
@@ -15268,7 +15292,7 @@
15268
15292
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
15269
15293
  runNavigationHooks(beforeNavigate, opts.url, context);
15270
15294
  assertActiveNavigation(transaction);
15271
- progressBar?.start();
15295
+ navigationProgress?.start();
15272
15296
  // popstate already points history at the destination entry. Keep the visible
15273
15297
  // page's position under currentUrl without overwriting that destination state.
15274
15298
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -15337,6 +15361,8 @@
15337
15361
  signal.addEventListener('abort', discard, { once: true });
15338
15362
  try {
15339
15363
  response = await waitForNavigation(entry.request, signal);
15364
+ if (response.ok && preloadHover?.url === preloadKey)
15365
+ preloadHover.used = true;
15340
15366
  }
15341
15367
  catch (error) {
15342
15368
  if (error.name === 'AbortError')
@@ -15355,15 +15381,16 @@
15355
15381
  else {
15356
15382
  try {
15357
15383
  // Update progress to rand 20 - 30%
15358
- progressBar?.setProgress(Math.random() * 10 + 20);
15359
- transaction.interval = setInterval(setRand, 300);
15384
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
15385
+ if (navigationProgress)
15386
+ transaction.interval = setInterval(setRand, 300);
15360
15387
  response = await fetch(finalUrl, init);
15361
15388
  assertActiveNavigation(transaction);
15362
15389
  // A successful write can change HTML at any URL, including after a redirect.
15363
15390
  if (response.ok && init.method !== 'GET')
15364
15391
  clearPreloads();
15365
15392
  // Update progress to rand 70 - 80% after fetch
15366
- progressBar?.setProgress(Math.random() * 10 + 70);
15393
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
15367
15394
  if (transaction.interval)
15368
15395
  clearInterval(transaction.interval);
15369
15396
  transaction.interval = null;
@@ -15401,6 +15428,13 @@
15401
15428
  const doc = html
15402
15429
  ? parseNavigationDocument(html)
15403
15430
  : document.implementation.createHTMLDocument();
15431
+ const matchedSource = opts.strictSource && opts.targetSelector
15432
+ ? doc.querySelector(opts.targetSelector)
15433
+ : null;
15434
+ if (opts.strictSource && !(matchedSource instanceof HTMLElement) && isFullDocument(html || '')) {
15435
+ context.outcome = 'invalid-response';
15436
+ return; // A complete response must identify the requested region before any swap or snapshot.
15437
+ }
15404
15438
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
15405
15439
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
15406
15440
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -15415,7 +15449,9 @@
15415
15449
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
15416
15450
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
15417
15451
  // Pick source fragment
15418
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
15452
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
15453
+ ? matchedSource
15454
+ : pickSourceFragment(doc, target, targetOutletIndex));
15419
15455
  stripInertFallbackContent(src);
15420
15456
  assertActiveNavigation(transaction);
15421
15457
  // Final URL is response.url because in headers there could be redirects
@@ -15438,7 +15474,7 @@
15438
15474
  runNavigationHooks(beforeSwap, targetUrl, context);
15439
15475
  assertActiveNavigation(transaction);
15440
15476
  // Update progress to rand 80 - 90% before swap
15441
- progressBar?.setProgress(Math.random() * 10 + 80);
15477
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
15442
15478
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
15443
15479
  ? cloneHistoryTarget(target)
15444
15480
  : null;
@@ -15536,7 +15572,7 @@
15536
15572
  target = committedTarget;
15537
15573
  context.target = committedTarget;
15538
15574
  context.committed = true;
15539
- progressBar?.reconnect();
15575
+ navigationProgress?.reconnect();
15540
15576
  committedRoots = roots;
15541
15577
  }
15542
15578
  });
@@ -15568,7 +15604,7 @@
15568
15604
  }
15569
15605
  };
15570
15606
  // Apply view transition if available
15571
- if (document.startViewTransition) {
15607
+ if (!opts.background && document.startViewTransition) {
15572
15608
  const transition = document.startViewTransition(() => performUpdate());
15573
15609
  consumeViewTransitionRejection(transition?.ready);
15574
15610
  consumeViewTransitionRejection(transition?.finished);
@@ -15619,7 +15655,7 @@
15619
15655
  hideTargetSpinner(transaction.spinner);
15620
15656
  if (activeNavigation?.id === transaction.id) {
15621
15657
  activeNavigation = null;
15622
- progressBar?.complete();
15658
+ navigationProgress?.complete();
15623
15659
  }
15624
15660
  opts.signal?.removeEventListener('abort', abort);
15625
15661
  transaction.resolveDone();
@@ -15731,15 +15767,34 @@
15731
15767
  navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
15732
15768
  }
15733
15769
  function onMouseOverCapture(event) {
15734
- const target = event.target;
15735
- if (!target)
15736
- return;
15737
- const link = target.closest('a[g-preload]');
15770
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
15738
15771
  if (!link)
15739
15772
  return;
15740
15773
  const url = eligibleAnchorUrl(link);
15741
- if (url)
15742
- preload(url.href);
15774
+ if (!url)
15775
+ return;
15776
+ const hover = preloadHover;
15777
+ if (hover?.used && hover.url === url.href && ((event.clientX === hover.x && event.clientY === hover.y) ||
15778
+ (event.relatedTarget instanceof Node && link.contains(event.relatedTarget))))
15779
+ return;
15780
+ preloadHover = { url: url.href, x: event.clientX, y: event.clientY, used: false };
15781
+ preload(url.href);
15782
+ }
15783
+ function onPreloadMouseMove(event) {
15784
+ const hover = preloadHover;
15785
+ // The browser can retarget mouse events after a swap with the pointer still in place.
15786
+ if (!hover || (event.clientX === hover.x && event.clientY === hover.y))
15787
+ return;
15788
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
15789
+ if (!(link instanceof HTMLAnchorElement) || link.href !== hover.url) {
15790
+ clearPreloadHover();
15791
+ return;
15792
+ }
15793
+ hover.x = event.clientX;
15794
+ hover.y = event.clientY;
15795
+ }
15796
+ function clearPreloadHover() {
15797
+ preloadHover = undefined;
15743
15798
  }
15744
15799
  function onSubmit(event) {
15745
15800
  if (event.defaultPrevented)
@@ -15892,7 +15947,7 @@
15892
15947
  portalCreate,
15893
15948
  portalDestroy,
15894
15949
  setCspNonce,
15895
- version: '0.5.7'
15950
+ version: '0.6.0'
15896
15951
  };
15897
15952
 
15898
15953
  setExpressionRuntime(cspExpressionRuntime);