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.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
  */
@@ -7699,8 +7699,10 @@
7699
7699
  while (idx < toChildren.length || idx < fromChildren.length) {
7700
7700
  const toChild = toChildren[idx] || null;
7701
7701
  let fromChild = fromChildren[idx] || null;
7702
- // Move an existing keyed child into the requested position before morphing it.
7703
- const requestedKey = explicitKeyFor(toChild);
7702
+ // Persist placeholders have their own placement path below, even when
7703
+ // they also carry an id or another explicit morph key.
7704
+ const toPersistKey = getPersistKey(toChild);
7705
+ const requestedKey = toPersistKey ? null : explicitKeyFor(toChild);
7704
7706
  if (requestedKey && explicitKeyFor(fromChild) !== requestedKey) {
7705
7707
  const matchingIndex = fromChildren.findIndex((candidate, candidateIndex) => candidateIndex > idx && explicitKeyFor(candidate) === requestedKey);
7706
7708
  if (matchingIndex !== -1) {
@@ -7715,7 +7717,6 @@
7715
7717
  }
7716
7718
  }
7717
7719
  // Check if either is persist comment/element
7718
- const toPersistKey = getPersistKey(toChild);
7719
7720
  const fromPersistKey = getPersistKey(fromChild);
7720
7721
  // If toChild is persist, check if we already have matching persist in fromChildren
7721
7722
  if (toPersistKey) {
@@ -8150,7 +8151,7 @@
8150
8151
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
8151
8152
  */
8152
8153
  const submittingForms = new WeakSet();
8153
- async function visit(url, options, fresh) {
8154
+ async function visit(url, options, refreshPolicy) {
8154
8155
  const destination = new URL(url, document.baseURI);
8155
8156
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
8156
8157
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -8167,21 +8168,27 @@
8167
8168
  url: destination.href, method: 'GET',
8168
8169
  changeState: options.history !== 'none', replace: options.history === 'replace',
8169
8170
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
8170
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
8171
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
8171
8172
  onEnd: context => { result = context; }
8172
8173
  });
8173
8174
  return result;
8174
8175
  }
8175
8176
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
8176
8177
  function navigate(url, options = {}) {
8177
- return visit(url, options, false);
8178
+ return visit(url, options);
8178
8179
  }
8179
8180
  /** Fetch the current URL again, preserving history and scroll by default. */
8180
- function refresh(options = {}) {
8181
- return visit(location.href, { history: 'none', scroll: false, ...options }, true);
8181
+ function refresh({ background, strictSource, ...options } = {}) {
8182
+ if (strictSource === true && options.target === undefined) {
8183
+ return Promise.reject(new TypeError('Gyos.refresh with strictSource requires a target selector.'));
8184
+ }
8185
+ return visit(location.href, { history: 'none', scroll: false, ...options }, {
8186
+ fresh: true, background: background === true, strictSource: strictSource === true
8187
+ });
8182
8188
  }
8183
8189
  const snapshots = new Map();
8184
8190
  const preloadCache = new Map();
8191
+ let preloadHover;
8185
8192
  const scrollPositions = new Map();
8186
8193
  const historyFragments = new Map();
8187
8194
  let routerStarted = false;
@@ -8191,6 +8198,7 @@
8191
8198
  let currentUrl = canonicalUrl(window.location.href, true);
8192
8199
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
8193
8200
  let activeNavigation = null;
8201
+ let waitingNavigations = 0;
8194
8202
  // Global progress bar instance
8195
8203
  let progressBar = null;
8196
8204
  const beforeNavigate = [];
@@ -8236,6 +8244,8 @@
8236
8244
  document.addEventListener('click', onClick);
8237
8245
  document.addEventListener('submit', onSubmit);
8238
8246
  document.addEventListener('mouseover', onMouseOverCapture, true);
8247
+ document.addEventListener('mousemove', onPreloadMouseMove, true);
8248
+ document.addEventListener('mouseleave', clearPreloadHover);
8239
8249
  window.addEventListener('popstate', onPopState);
8240
8250
  if ('scrollRestoration' in history) {
8241
8251
  history.scrollRestoration = 'manual';
@@ -8507,6 +8517,7 @@
8507
8517
  entry?.controller.abort();
8508
8518
  }
8509
8519
  function clearPreloads() {
8520
+ clearPreloadHover();
8510
8521
  for (const key of preloadCache.keys())
8511
8522
  discardPreload(key);
8512
8523
  }
@@ -8581,19 +8592,27 @@
8581
8592
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8582
8593
  }
8583
8594
  async function navigateRequest(opts) {
8584
- const finishUnstarted = () => {
8595
+ const finishUnstarted = (signal = opts.signal) => {
8585
8596
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8586
8597
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
8587
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8598
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8588
8599
  };
8589
8600
  if (opts.signal?.aborted) {
8590
8601
  finishUnstarted();
8591
8602
  return;
8592
8603
  }
8604
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
8605
+ const skipped = new AbortController();
8606
+ skipped.abort();
8607
+ finishUnstarted(skipped.signal);
8608
+ return;
8609
+ }
8593
8610
  while (activeNavigation) {
8594
8611
  const previous = activeNavigation;
8595
8612
  if (previous.committing) {
8596
8613
  log('[GyosRouter] Waiting for previous DOM commit');
8614
+ // End hooks can request a refresh before this waiter resumes.
8615
+ waitingNavigations++;
8597
8616
  try {
8598
8617
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8599
8618
  }
@@ -8601,6 +8620,9 @@
8601
8620
  finishUnstarted();
8602
8621
  return;
8603
8622
  }
8623
+ finally {
8624
+ waitingNavigations--;
8625
+ }
8604
8626
  continue;
8605
8627
  }
8606
8628
  log('[GyosRouter] Cancelling previous navigation');
@@ -8635,14 +8657,16 @@
8635
8657
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
8636
8658
  : null;
8637
8659
  const fallback = (url, requestSent) => {
8638
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
8660
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
8639
8661
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
8640
- fallbackFromBoost(opts, url, requestSent);
8662
+ if (!opts.background)
8663
+ fallbackFromBoost(opts, url, requestSent);
8641
8664
  };
8665
+ const navigationProgress = opts.background ? null : progressBar;
8642
8666
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
8643
8667
  const setRand = () => {
8644
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
8645
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
8668
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
8669
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
8646
8670
  }
8647
8671
  };
8648
8672
  try {
@@ -8651,7 +8675,7 @@
8651
8675
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8652
8676
  runNavigationHooks(beforeNavigate, opts.url, context);
8653
8677
  assertActiveNavigation(transaction);
8654
- progressBar?.start();
8678
+ navigationProgress?.start();
8655
8679
  // popstate already points history at the destination entry. Keep the visible
8656
8680
  // page's position under currentUrl without overwriting that destination state.
8657
8681
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -8720,6 +8744,8 @@
8720
8744
  signal.addEventListener('abort', discard, { once: true });
8721
8745
  try {
8722
8746
  response = await waitForNavigation(entry.request, signal);
8747
+ if (response.ok && preloadHover?.url === preloadKey)
8748
+ preloadHover.used = true;
8723
8749
  }
8724
8750
  catch (error) {
8725
8751
  if (error.name === 'AbortError')
@@ -8738,15 +8764,16 @@
8738
8764
  else {
8739
8765
  try {
8740
8766
  // Update progress to rand 20 - 30%
8741
- progressBar?.setProgress(Math.random() * 10 + 20);
8742
- transaction.interval = setInterval(setRand, 300);
8767
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
8768
+ if (navigationProgress)
8769
+ transaction.interval = setInterval(setRand, 300);
8743
8770
  response = await fetch(finalUrl, init);
8744
8771
  assertActiveNavigation(transaction);
8745
8772
  // A successful write can change HTML at any URL, including after a redirect.
8746
8773
  if (response.ok && init.method !== 'GET')
8747
8774
  clearPreloads();
8748
8775
  // Update progress to rand 70 - 80% after fetch
8749
- progressBar?.setProgress(Math.random() * 10 + 70);
8776
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
8750
8777
  if (transaction.interval)
8751
8778
  clearInterval(transaction.interval);
8752
8779
  transaction.interval = null;
@@ -8784,6 +8811,13 @@
8784
8811
  const doc = html
8785
8812
  ? parseNavigationDocument(html)
8786
8813
  : document.implementation.createHTMLDocument();
8814
+ const matchedSource = opts.strictSource && opts.targetSelector
8815
+ ? doc.querySelector(opts.targetSelector)
8816
+ : null;
8817
+ if (opts.strictSource && !(matchedSource instanceof HTMLElement) && isFullDocument(html || '')) {
8818
+ context.outcome = 'invalid-response';
8819
+ return; // A complete response must identify the requested region before any swap or snapshot.
8820
+ }
8787
8821
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
8788
8822
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
8789
8823
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -8798,7 +8832,9 @@
8798
8832
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
8799
8833
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
8800
8834
  // Pick source fragment
8801
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
8835
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
8836
+ ? matchedSource
8837
+ : pickSourceFragment(doc, target, targetOutletIndex));
8802
8838
  stripInertFallbackContent(src);
8803
8839
  assertActiveNavigation(transaction);
8804
8840
  // Final URL is response.url because in headers there could be redirects
@@ -8821,7 +8857,7 @@
8821
8857
  runNavigationHooks(beforeSwap, targetUrl, context);
8822
8858
  assertActiveNavigation(transaction);
8823
8859
  // Update progress to rand 80 - 90% before swap
8824
- progressBar?.setProgress(Math.random() * 10 + 80);
8860
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
8825
8861
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
8826
8862
  ? cloneHistoryTarget(target)
8827
8863
  : null;
@@ -8919,7 +8955,7 @@
8919
8955
  target = committedTarget;
8920
8956
  context.target = committedTarget;
8921
8957
  context.committed = true;
8922
- progressBar?.reconnect();
8958
+ navigationProgress?.reconnect();
8923
8959
  committedRoots = roots;
8924
8960
  }
8925
8961
  });
@@ -8951,7 +8987,7 @@
8951
8987
  }
8952
8988
  };
8953
8989
  // Apply view transition if available
8954
- if (document.startViewTransition) {
8990
+ if (!opts.background && document.startViewTransition) {
8955
8991
  const transition = document.startViewTransition(() => performUpdate());
8956
8992
  consumeViewTransitionRejection(transition?.ready);
8957
8993
  consumeViewTransitionRejection(transition?.finished);
@@ -9002,7 +9038,7 @@
9002
9038
  hideTargetSpinner(transaction.spinner);
9003
9039
  if (activeNavigation?.id === transaction.id) {
9004
9040
  activeNavigation = null;
9005
- progressBar?.complete();
9041
+ navigationProgress?.complete();
9006
9042
  }
9007
9043
  opts.signal?.removeEventListener('abort', abort);
9008
9044
  transaction.resolveDone();
@@ -9114,15 +9150,34 @@
9114
9150
  navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
9115
9151
  }
9116
9152
  function onMouseOverCapture(event) {
9117
- const target = event.target;
9118
- if (!target)
9119
- return;
9120
- const link = target.closest('a[g-preload]');
9153
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
9121
9154
  if (!link)
9122
9155
  return;
9123
9156
  const url = eligibleAnchorUrl(link);
9124
- if (url)
9125
- preload(url.href);
9157
+ if (!url)
9158
+ return;
9159
+ const hover = preloadHover;
9160
+ if (hover?.used && hover.url === url.href && ((event.clientX === hover.x && event.clientY === hover.y) ||
9161
+ (event.relatedTarget instanceof Node && link.contains(event.relatedTarget))))
9162
+ return;
9163
+ preloadHover = { url: url.href, x: event.clientX, y: event.clientY, used: false };
9164
+ preload(url.href);
9165
+ }
9166
+ function onPreloadMouseMove(event) {
9167
+ const hover = preloadHover;
9168
+ // The browser can retarget mouse events after a swap with the pointer still in place.
9169
+ if (!hover || (event.clientX === hover.x && event.clientY === hover.y))
9170
+ return;
9171
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
9172
+ if (!(link instanceof HTMLAnchorElement) || link.href !== hover.url) {
9173
+ clearPreloadHover();
9174
+ return;
9175
+ }
9176
+ hover.x = event.clientX;
9177
+ hover.y = event.clientY;
9178
+ }
9179
+ function clearPreloadHover() {
9180
+ preloadHover = undefined;
9126
9181
  }
9127
9182
  function onSubmit(event) {
9128
9183
  if (event.defaultPrevented)
@@ -9275,7 +9330,7 @@
9275
9330
  portalCreate,
9276
9331
  portalDestroy,
9277
9332
  setCspNonce,
9278
- version: '0.5.7'
9333
+ version: '0.6.0'
9279
9334
  };
9280
9335
 
9281
9336
  setExpressionRuntime(standardExpressionRuntime);