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.
@@ -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
  */
@@ -7675,8 +7675,10 @@ function morph(target, src) {
7675
7675
  while (idx < toChildren.length || idx < fromChildren.length) {
7676
7676
  const toChild = toChildren[idx] || null;
7677
7677
  let fromChild = fromChildren[idx] || null;
7678
- // Move an existing keyed child into the requested position before morphing it.
7679
- const requestedKey = explicitKeyFor(toChild);
7678
+ // Persist placeholders have their own placement path below, even when
7679
+ // they also carry an id or another explicit morph key.
7680
+ const toPersistKey = getPersistKey(toChild);
7681
+ const requestedKey = toPersistKey ? null : explicitKeyFor(toChild);
7680
7682
  if (requestedKey && explicitKeyFor(fromChild) !== requestedKey) {
7681
7683
  const matchingIndex = fromChildren.findIndex((candidate, candidateIndex) => candidateIndex > idx && explicitKeyFor(candidate) === requestedKey);
7682
7684
  if (matchingIndex !== -1) {
@@ -7691,7 +7693,6 @@ function morph(target, src) {
7691
7693
  }
7692
7694
  }
7693
7695
  // Check if either is persist comment/element
7694
- const toPersistKey = getPersistKey(toChild);
7695
7696
  const fromPersistKey = getPersistKey(fromChild);
7696
7697
  // If toChild is persist, check if we already have matching persist in fromChildren
7697
7698
  if (toPersistKey) {
@@ -8126,7 +8127,7 @@ function prepareNavigationHtml(html) {
8126
8127
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
8127
8128
  */
8128
8129
  const submittingForms = new WeakSet();
8129
- async function visit(url, options, fresh) {
8130
+ async function visit(url, options, refreshPolicy) {
8130
8131
  const destination = new URL(url, document.baseURI);
8131
8132
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
8132
8133
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -8143,21 +8144,27 @@ async function visit(url, options, fresh) {
8143
8144
  url: destination.href, method: 'GET',
8144
8145
  changeState: options.history !== 'none', replace: options.history === 'replace',
8145
8146
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
8146
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
8147
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
8147
8148
  onEnd: context => { result = context; }
8148
8149
  });
8149
8150
  return result;
8150
8151
  }
8151
8152
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
8152
8153
  function navigate(url, options = {}) {
8153
- return visit(url, options, false);
8154
+ return visit(url, options);
8154
8155
  }
8155
8156
  /** Fetch the current URL again, preserving history and scroll by default. */
8156
- function refresh(options = {}) {
8157
- return visit(location.href, { history: 'none', scroll: false, ...options }, true);
8157
+ function refresh({ background, strictSource, ...options } = {}) {
8158
+ if (strictSource === true && options.target === undefined) {
8159
+ return Promise.reject(new TypeError('Gyos.refresh with strictSource requires a target selector.'));
8160
+ }
8161
+ return visit(location.href, { history: 'none', scroll: false, ...options }, {
8162
+ fresh: true, background: background === true, strictSource: strictSource === true
8163
+ });
8158
8164
  }
8159
8165
  const snapshots = new Map();
8160
8166
  const preloadCache = new Map();
8167
+ let preloadHover;
8161
8168
  const scrollPositions = new Map();
8162
8169
  const historyFragments = new Map();
8163
8170
  let routerStarted = false;
@@ -8167,6 +8174,7 @@ let progressBarEnabled = true;
8167
8174
  let currentUrl = canonicalUrl(window.location.href, true);
8168
8175
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
8169
8176
  let activeNavigation = null;
8177
+ let waitingNavigations = 0;
8170
8178
  // Global progress bar instance
8171
8179
  let progressBar = null;
8172
8180
  const beforeNavigate = [];
@@ -8212,6 +8220,8 @@ function startRouter(options) {
8212
8220
  document.addEventListener('click', onClick);
8213
8221
  document.addEventListener('submit', onSubmit);
8214
8222
  document.addEventListener('mouseover', onMouseOverCapture, true);
8223
+ document.addEventListener('mousemove', onPreloadMouseMove, true);
8224
+ document.addEventListener('mouseleave', clearPreloadHover);
8215
8225
  window.addEventListener('popstate', onPopState);
8216
8226
  if ('scrollRestoration' in history) {
8217
8227
  history.scrollRestoration = 'manual';
@@ -8483,6 +8493,7 @@ function discardPreload(key) {
8483
8493
  entry?.controller.abort();
8484
8494
  }
8485
8495
  function clearPreloads() {
8496
+ clearPreloadHover();
8486
8497
  for (const key of preloadCache.keys())
8487
8498
  discardPreload(key);
8488
8499
  }
@@ -8557,19 +8568,27 @@ function fallbackFromBoost(opts, url, requestSent) {
8557
8568
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8558
8569
  }
8559
8570
  async function navigateRequest(opts) {
8560
- const finishUnstarted = () => {
8571
+ const finishUnstarted = (signal = opts.signal) => {
8561
8572
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8562
8573
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
8563
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8574
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8564
8575
  };
8565
8576
  if (opts.signal?.aborted) {
8566
8577
  finishUnstarted();
8567
8578
  return;
8568
8579
  }
8580
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
8581
+ const skipped = new AbortController();
8582
+ skipped.abort();
8583
+ finishUnstarted(skipped.signal);
8584
+ return;
8585
+ }
8569
8586
  while (activeNavigation) {
8570
8587
  const previous = activeNavigation;
8571
8588
  if (previous.committing) {
8572
8589
  log('[GyosRouter] Waiting for previous DOM commit');
8590
+ // End hooks can request a refresh before this waiter resumes.
8591
+ waitingNavigations++;
8573
8592
  try {
8574
8593
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8575
8594
  }
@@ -8577,6 +8596,9 @@ async function navigateRequest(opts) {
8577
8596
  finishUnstarted();
8578
8597
  return;
8579
8598
  }
8599
+ finally {
8600
+ waitingNavigations--;
8601
+ }
8580
8602
  continue;
8581
8603
  }
8582
8604
  log('[GyosRouter] Cancelling previous navigation');
@@ -8611,14 +8633,16 @@ async function navigateRequest(opts) {
8611
8633
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
8612
8634
  : null;
8613
8635
  const fallback = (url, requestSent) => {
8614
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
8636
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
8615
8637
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
8616
- fallbackFromBoost(opts, url, requestSent);
8638
+ if (!opts.background)
8639
+ fallbackFromBoost(opts, url, requestSent);
8617
8640
  };
8641
+ const navigationProgress = opts.background ? null : progressBar;
8618
8642
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
8619
8643
  const setRand = () => {
8620
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
8621
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
8644
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
8645
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
8622
8646
  }
8623
8647
  };
8624
8648
  try {
@@ -8627,7 +8651,7 @@ async function navigateRequest(opts) {
8627
8651
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8628
8652
  runNavigationHooks(beforeNavigate, opts.url, context);
8629
8653
  assertActiveNavigation(transaction);
8630
- progressBar?.start();
8654
+ navigationProgress?.start();
8631
8655
  // popstate already points history at the destination entry. Keep the visible
8632
8656
  // page's position under currentUrl without overwriting that destination state.
8633
8657
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -8696,6 +8720,8 @@ async function navigateRequest(opts) {
8696
8720
  signal.addEventListener('abort', discard, { once: true });
8697
8721
  try {
8698
8722
  response = await waitForNavigation(entry.request, signal);
8723
+ if (response.ok && preloadHover?.url === preloadKey)
8724
+ preloadHover.used = true;
8699
8725
  }
8700
8726
  catch (error) {
8701
8727
  if (error.name === 'AbortError')
@@ -8714,15 +8740,16 @@ async function navigateRequest(opts) {
8714
8740
  else {
8715
8741
  try {
8716
8742
  // Update progress to rand 20 - 30%
8717
- progressBar?.setProgress(Math.random() * 10 + 20);
8718
- transaction.interval = setInterval(setRand, 300);
8743
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
8744
+ if (navigationProgress)
8745
+ transaction.interval = setInterval(setRand, 300);
8719
8746
  response = await fetch(finalUrl, init);
8720
8747
  assertActiveNavigation(transaction);
8721
8748
  // A successful write can change HTML at any URL, including after a redirect.
8722
8749
  if (response.ok && init.method !== 'GET')
8723
8750
  clearPreloads();
8724
8751
  // Update progress to rand 70 - 80% after fetch
8725
- progressBar?.setProgress(Math.random() * 10 + 70);
8752
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
8726
8753
  if (transaction.interval)
8727
8754
  clearInterval(transaction.interval);
8728
8755
  transaction.interval = null;
@@ -8760,6 +8787,13 @@ async function navigateRequest(opts) {
8760
8787
  const doc = html
8761
8788
  ? parseNavigationDocument(html)
8762
8789
  : document.implementation.createHTMLDocument();
8790
+ const matchedSource = opts.strictSource && opts.targetSelector
8791
+ ? doc.querySelector(opts.targetSelector)
8792
+ : null;
8793
+ if (opts.strictSource && !(matchedSource instanceof HTMLElement) && isFullDocument(html || '')) {
8794
+ context.outcome = 'invalid-response';
8795
+ return; // A complete response must identify the requested region before any swap or snapshot.
8796
+ }
8763
8797
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
8764
8798
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
8765
8799
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -8774,7 +8808,9 @@ async function navigateRequest(opts) {
8774
8808
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
8775
8809
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
8776
8810
  // Pick source fragment
8777
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
8811
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
8812
+ ? matchedSource
8813
+ : pickSourceFragment(doc, target, targetOutletIndex));
8778
8814
  stripInertFallbackContent(src);
8779
8815
  assertActiveNavigation(transaction);
8780
8816
  // Final URL is response.url because in headers there could be redirects
@@ -8797,7 +8833,7 @@ async function navigateRequest(opts) {
8797
8833
  runNavigationHooks(beforeSwap, targetUrl, context);
8798
8834
  assertActiveNavigation(transaction);
8799
8835
  // Update progress to rand 80 - 90% before swap
8800
- progressBar?.setProgress(Math.random() * 10 + 80);
8836
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
8801
8837
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
8802
8838
  ? cloneHistoryTarget(target)
8803
8839
  : null;
@@ -8895,7 +8931,7 @@ async function navigateRequest(opts) {
8895
8931
  target = committedTarget;
8896
8932
  context.target = committedTarget;
8897
8933
  context.committed = true;
8898
- progressBar?.reconnect();
8934
+ navigationProgress?.reconnect();
8899
8935
  committedRoots = roots;
8900
8936
  }
8901
8937
  });
@@ -8927,7 +8963,7 @@ async function navigateRequest(opts) {
8927
8963
  }
8928
8964
  };
8929
8965
  // Apply view transition if available
8930
- if (document.startViewTransition) {
8966
+ if (!opts.background && document.startViewTransition) {
8931
8967
  const transition = document.startViewTransition(() => performUpdate());
8932
8968
  consumeViewTransitionRejection(transition?.ready);
8933
8969
  consumeViewTransitionRejection(transition?.finished);
@@ -8978,7 +9014,7 @@ async function navigateRequest(opts) {
8978
9014
  hideTargetSpinner(transaction.spinner);
8979
9015
  if (activeNavigation?.id === transaction.id) {
8980
9016
  activeNavigation = null;
8981
- progressBar?.complete();
9017
+ navigationProgress?.complete();
8982
9018
  }
8983
9019
  opts.signal?.removeEventListener('abort', abort);
8984
9020
  transaction.resolveDone();
@@ -9090,15 +9126,34 @@ function onClick(event) {
9090
9126
  navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
9091
9127
  }
9092
9128
  function onMouseOverCapture(event) {
9093
- const target = event.target;
9094
- if (!target)
9095
- return;
9096
- const link = target.closest('a[g-preload]');
9129
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
9097
9130
  if (!link)
9098
9131
  return;
9099
9132
  const url = eligibleAnchorUrl(link);
9100
- if (url)
9101
- preload(url.href);
9133
+ if (!url)
9134
+ return;
9135
+ const hover = preloadHover;
9136
+ if (hover?.used && hover.url === url.href && ((event.clientX === hover.x && event.clientY === hover.y) ||
9137
+ (event.relatedTarget instanceof Node && link.contains(event.relatedTarget))))
9138
+ return;
9139
+ preloadHover = { url: url.href, x: event.clientX, y: event.clientY, used: false };
9140
+ preload(url.href);
9141
+ }
9142
+ function onPreloadMouseMove(event) {
9143
+ const hover = preloadHover;
9144
+ // The browser can retarget mouse events after a swap with the pointer still in place.
9145
+ if (!hover || (event.clientX === hover.x && event.clientY === hover.y))
9146
+ return;
9147
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
9148
+ if (!(link instanceof HTMLAnchorElement) || link.href !== hover.url) {
9149
+ clearPreloadHover();
9150
+ return;
9151
+ }
9152
+ hover.x = event.clientX;
9153
+ hover.y = event.clientY;
9154
+ }
9155
+ function clearPreloadHover() {
9156
+ preloadHover = undefined;
9102
9157
  }
9103
9158
  function onSubmit(event) {
9104
9159
  if (event.defaultPrevented)
@@ -9251,7 +9306,7 @@ const Gyos = {
9251
9306
  portalCreate,
9252
9307
  portalDestroy,
9253
9308
  setCspNonce,
9254
- version: '0.5.7'
9309
+ version: '0.6.0'
9255
9310
  };
9256
9311
 
9257
9312
  // This file was generated. Do not modify manually!