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.esm.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
  */
@@ -7693,8 +7693,10 @@ function morph(target, src) {
7693
7693
  while (idx < toChildren.length || idx < fromChildren.length) {
7694
7694
  const toChild = toChildren[idx] || null;
7695
7695
  let fromChild = fromChildren[idx] || null;
7696
- // Move an existing keyed child into the requested position before morphing it.
7697
- const requestedKey = explicitKeyFor(toChild);
7696
+ // Persist placeholders have their own placement path below, even when
7697
+ // they also carry an id or another explicit morph key.
7698
+ const toPersistKey = getPersistKey(toChild);
7699
+ const requestedKey = toPersistKey ? null : explicitKeyFor(toChild);
7698
7700
  if (requestedKey && explicitKeyFor(fromChild) !== requestedKey) {
7699
7701
  const matchingIndex = fromChildren.findIndex((candidate, candidateIndex) => candidateIndex > idx && explicitKeyFor(candidate) === requestedKey);
7700
7702
  if (matchingIndex !== -1) {
@@ -7709,7 +7711,6 @@ function morph(target, src) {
7709
7711
  }
7710
7712
  }
7711
7713
  // Check if either is persist comment/element
7712
- const toPersistKey = getPersistKey(toChild);
7713
7714
  const fromPersistKey = getPersistKey(fromChild);
7714
7715
  // If toChild is persist, check if we already have matching persist in fromChildren
7715
7716
  if (toPersistKey) {
@@ -8144,7 +8145,7 @@ function prepareNavigationHtml(html) {
8144
8145
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
8145
8146
  */
8146
8147
  const submittingForms = new WeakSet();
8147
- async function visit(url, options, fresh) {
8148
+ async function visit(url, options, refreshPolicy) {
8148
8149
  const destination = new URL(url, document.baseURI);
8149
8150
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
8150
8151
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -8161,21 +8162,27 @@ async function visit(url, options, fresh) {
8161
8162
  url: destination.href, method: 'GET',
8162
8163
  changeState: options.history !== 'none', replace: options.history === 'replace',
8163
8164
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
8164
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
8165
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
8165
8166
  onEnd: context => { result = context; }
8166
8167
  });
8167
8168
  return result;
8168
8169
  }
8169
8170
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
8170
8171
  function navigate(url, options = {}) {
8171
- return visit(url, options, false);
8172
+ return visit(url, options);
8172
8173
  }
8173
8174
  /** Fetch the current URL again, preserving history and scroll by default. */
8174
- function refresh(options = {}) {
8175
- return visit(location.href, { history: 'none', scroll: false, ...options }, true);
8175
+ function refresh({ background, strictSource, ...options } = {}) {
8176
+ if (strictSource === true && options.target === undefined) {
8177
+ return Promise.reject(new TypeError('Gyos.refresh with strictSource requires a target selector.'));
8178
+ }
8179
+ return visit(location.href, { history: 'none', scroll: false, ...options }, {
8180
+ fresh: true, background: background === true, strictSource: strictSource === true
8181
+ });
8176
8182
  }
8177
8183
  const snapshots = new Map();
8178
8184
  const preloadCache = new Map();
8185
+ let preloadHover;
8179
8186
  const scrollPositions = new Map();
8180
8187
  const historyFragments = new Map();
8181
8188
  let routerStarted = false;
@@ -8185,6 +8192,7 @@ let progressBarEnabled = true;
8185
8192
  let currentUrl = canonicalUrl(window.location.href, true);
8186
8193
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
8187
8194
  let activeNavigation = null;
8195
+ let waitingNavigations = 0;
8188
8196
  // Global progress bar instance
8189
8197
  let progressBar = null;
8190
8198
  const beforeNavigate = [];
@@ -8230,6 +8238,8 @@ function startRouter(options) {
8230
8238
  document.addEventListener('click', onClick);
8231
8239
  document.addEventListener('submit', onSubmit);
8232
8240
  document.addEventListener('mouseover', onMouseOverCapture, true);
8241
+ document.addEventListener('mousemove', onPreloadMouseMove, true);
8242
+ document.addEventListener('mouseleave', clearPreloadHover);
8233
8243
  window.addEventListener('popstate', onPopState);
8234
8244
  if ('scrollRestoration' in history) {
8235
8245
  history.scrollRestoration = 'manual';
@@ -8501,6 +8511,7 @@ function discardPreload(key) {
8501
8511
  entry?.controller.abort();
8502
8512
  }
8503
8513
  function clearPreloads() {
8514
+ clearPreloadHover();
8504
8515
  for (const key of preloadCache.keys())
8505
8516
  discardPreload(key);
8506
8517
  }
@@ -8575,19 +8586,27 @@ function fallbackFromBoost(opts, url, requestSent) {
8575
8586
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8576
8587
  }
8577
8588
  async function navigateRequest(opts) {
8578
- const finishUnstarted = () => {
8589
+ const finishUnstarted = (signal = opts.signal) => {
8579
8590
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8580
8591
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
8581
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8592
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8582
8593
  };
8583
8594
  if (opts.signal?.aborted) {
8584
8595
  finishUnstarted();
8585
8596
  return;
8586
8597
  }
8598
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
8599
+ const skipped = new AbortController();
8600
+ skipped.abort();
8601
+ finishUnstarted(skipped.signal);
8602
+ return;
8603
+ }
8587
8604
  while (activeNavigation) {
8588
8605
  const previous = activeNavigation;
8589
8606
  if (previous.committing) {
8590
8607
  log('[GyosRouter] Waiting for previous DOM commit');
8608
+ // End hooks can request a refresh before this waiter resumes.
8609
+ waitingNavigations++;
8591
8610
  try {
8592
8611
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8593
8612
  }
@@ -8595,6 +8614,9 @@ async function navigateRequest(opts) {
8595
8614
  finishUnstarted();
8596
8615
  return;
8597
8616
  }
8617
+ finally {
8618
+ waitingNavigations--;
8619
+ }
8598
8620
  continue;
8599
8621
  }
8600
8622
  log('[GyosRouter] Cancelling previous navigation');
@@ -8629,14 +8651,16 @@ async function navigateRequest(opts) {
8629
8651
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
8630
8652
  : null;
8631
8653
  const fallback = (url, requestSent) => {
8632
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
8654
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
8633
8655
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
8634
- fallbackFromBoost(opts, url, requestSent);
8656
+ if (!opts.background)
8657
+ fallbackFromBoost(opts, url, requestSent);
8635
8658
  };
8659
+ const navigationProgress = opts.background ? null : progressBar;
8636
8660
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
8637
8661
  const setRand = () => {
8638
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
8639
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
8662
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
8663
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
8640
8664
  }
8641
8665
  };
8642
8666
  try {
@@ -8645,7 +8669,7 @@ async function navigateRequest(opts) {
8645
8669
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8646
8670
  runNavigationHooks(beforeNavigate, opts.url, context);
8647
8671
  assertActiveNavigation(transaction);
8648
- progressBar?.start();
8672
+ navigationProgress?.start();
8649
8673
  // popstate already points history at the destination entry. Keep the visible
8650
8674
  // page's position under currentUrl without overwriting that destination state.
8651
8675
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -8714,6 +8738,8 @@ async function navigateRequest(opts) {
8714
8738
  signal.addEventListener('abort', discard, { once: true });
8715
8739
  try {
8716
8740
  response = await waitForNavigation(entry.request, signal);
8741
+ if (response.ok && preloadHover?.url === preloadKey)
8742
+ preloadHover.used = true;
8717
8743
  }
8718
8744
  catch (error) {
8719
8745
  if (error.name === 'AbortError')
@@ -8732,15 +8758,16 @@ async function navigateRequest(opts) {
8732
8758
  else {
8733
8759
  try {
8734
8760
  // Update progress to rand 20 - 30%
8735
- progressBar?.setProgress(Math.random() * 10 + 20);
8736
- transaction.interval = setInterval(setRand, 300);
8761
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
8762
+ if (navigationProgress)
8763
+ transaction.interval = setInterval(setRand, 300);
8737
8764
  response = await fetch(finalUrl, init);
8738
8765
  assertActiveNavigation(transaction);
8739
8766
  // A successful write can change HTML at any URL, including after a redirect.
8740
8767
  if (response.ok && init.method !== 'GET')
8741
8768
  clearPreloads();
8742
8769
  // Update progress to rand 70 - 80% after fetch
8743
- progressBar?.setProgress(Math.random() * 10 + 70);
8770
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
8744
8771
  if (transaction.interval)
8745
8772
  clearInterval(transaction.interval);
8746
8773
  transaction.interval = null;
@@ -8778,6 +8805,13 @@ async function navigateRequest(opts) {
8778
8805
  const doc = html
8779
8806
  ? parseNavigationDocument(html)
8780
8807
  : document.implementation.createHTMLDocument();
8808
+ const matchedSource = opts.strictSource && opts.targetSelector
8809
+ ? doc.querySelector(opts.targetSelector)
8810
+ : null;
8811
+ if (opts.strictSource && !(matchedSource instanceof HTMLElement) && isFullDocument(html || '')) {
8812
+ context.outcome = 'invalid-response';
8813
+ return; // A complete response must identify the requested region before any swap or snapshot.
8814
+ }
8781
8815
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
8782
8816
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
8783
8817
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -8792,7 +8826,9 @@ async function navigateRequest(opts) {
8792
8826
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
8793
8827
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
8794
8828
  // Pick source fragment
8795
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
8829
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
8830
+ ? matchedSource
8831
+ : pickSourceFragment(doc, target, targetOutletIndex));
8796
8832
  stripInertFallbackContent(src);
8797
8833
  assertActiveNavigation(transaction);
8798
8834
  // Final URL is response.url because in headers there could be redirects
@@ -8815,7 +8851,7 @@ async function navigateRequest(opts) {
8815
8851
  runNavigationHooks(beforeSwap, targetUrl, context);
8816
8852
  assertActiveNavigation(transaction);
8817
8853
  // Update progress to rand 80 - 90% before swap
8818
- progressBar?.setProgress(Math.random() * 10 + 80);
8854
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
8819
8855
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
8820
8856
  ? cloneHistoryTarget(target)
8821
8857
  : null;
@@ -8913,7 +8949,7 @@ async function navigateRequest(opts) {
8913
8949
  target = committedTarget;
8914
8950
  context.target = committedTarget;
8915
8951
  context.committed = true;
8916
- progressBar?.reconnect();
8952
+ navigationProgress?.reconnect();
8917
8953
  committedRoots = roots;
8918
8954
  }
8919
8955
  });
@@ -8945,7 +8981,7 @@ async function navigateRequest(opts) {
8945
8981
  }
8946
8982
  };
8947
8983
  // Apply view transition if available
8948
- if (document.startViewTransition) {
8984
+ if (!opts.background && document.startViewTransition) {
8949
8985
  const transition = document.startViewTransition(() => performUpdate());
8950
8986
  consumeViewTransitionRejection(transition?.ready);
8951
8987
  consumeViewTransitionRejection(transition?.finished);
@@ -8996,7 +9032,7 @@ async function navigateRequest(opts) {
8996
9032
  hideTargetSpinner(transaction.spinner);
8997
9033
  if (activeNavigation?.id === transaction.id) {
8998
9034
  activeNavigation = null;
8999
- progressBar?.complete();
9035
+ navigationProgress?.complete();
9000
9036
  }
9001
9037
  opts.signal?.removeEventListener('abort', abort);
9002
9038
  transaction.resolveDone();
@@ -9108,15 +9144,34 @@ function onClick(event) {
9108
9144
  navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
9109
9145
  }
9110
9146
  function onMouseOverCapture(event) {
9111
- const target = event.target;
9112
- if (!target)
9113
- return;
9114
- const link = target.closest('a[g-preload]');
9147
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
9115
9148
  if (!link)
9116
9149
  return;
9117
9150
  const url = eligibleAnchorUrl(link);
9118
- if (url)
9119
- preload(url.href);
9151
+ if (!url)
9152
+ return;
9153
+ const hover = preloadHover;
9154
+ if (hover?.used && hover.url === url.href && ((event.clientX === hover.x && event.clientY === hover.y) ||
9155
+ (event.relatedTarget instanceof Node && link.contains(event.relatedTarget))))
9156
+ return;
9157
+ preloadHover = { url: url.href, x: event.clientX, y: event.clientY, used: false };
9158
+ preload(url.href);
9159
+ }
9160
+ function onPreloadMouseMove(event) {
9161
+ const hover = preloadHover;
9162
+ // The browser can retarget mouse events after a swap with the pointer still in place.
9163
+ if (!hover || (event.clientX === hover.x && event.clientY === hover.y))
9164
+ return;
9165
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
9166
+ if (!(link instanceof HTMLAnchorElement) || link.href !== hover.url) {
9167
+ clearPreloadHover();
9168
+ return;
9169
+ }
9170
+ hover.x = event.clientX;
9171
+ hover.y = event.clientY;
9172
+ }
9173
+ function clearPreloadHover() {
9174
+ preloadHover = undefined;
9120
9175
  }
9121
9176
  function onSubmit(event) {
9122
9177
  if (event.defaultPrevented)
@@ -9269,7 +9324,7 @@ const Gyos = {
9269
9324
  portalCreate,
9270
9325
  portalDestroy,
9271
9326
  setCspNonce,
9272
- version: '0.5.7'
9327
+ version: '0.6.0'
9273
9328
  };
9274
9329
 
9275
9330
  setExpressionRuntime(standardExpressionRuntime);