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
  */
@@ -14310,8 +14310,10 @@ function morph(target, src) {
14310
14310
  while (idx < toChildren.length || idx < fromChildren.length) {
14311
14311
  const toChild = toChildren[idx] || null;
14312
14312
  let fromChild = fromChildren[idx] || null;
14313
- // Move an existing keyed child into the requested position before morphing it.
14314
- const requestedKey = explicitKeyFor(toChild);
14313
+ // Persist placeholders have their own placement path below, even when
14314
+ // they also carry an id or another explicit morph key.
14315
+ const toPersistKey = getPersistKey(toChild);
14316
+ const requestedKey = toPersistKey ? null : explicitKeyFor(toChild);
14315
14317
  if (requestedKey && explicitKeyFor(fromChild) !== requestedKey) {
14316
14318
  const matchingIndex = fromChildren.findIndex((candidate, candidateIndex) => candidateIndex > idx && explicitKeyFor(candidate) === requestedKey);
14317
14319
  if (matchingIndex !== -1) {
@@ -14326,7 +14328,6 @@ function morph(target, src) {
14326
14328
  }
14327
14329
  }
14328
14330
  // Check if either is persist comment/element
14329
- const toPersistKey = getPersistKey(toChild);
14330
14331
  const fromPersistKey = getPersistKey(fromChild);
14331
14332
  // If toChild is persist, check if we already have matching persist in fromChildren
14332
14333
  if (toPersistKey) {
@@ -14761,7 +14762,7 @@ function prepareNavigationHtml(html) {
14761
14762
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
14762
14763
  */
14763
14764
  const submittingForms = new WeakSet();
14764
- async function visit(url, options, fresh) {
14765
+ async function visit(url, options, refreshPolicy) {
14765
14766
  const destination = new URL(url, document.baseURI);
14766
14767
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
14767
14768
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -14778,21 +14779,27 @@ async function visit(url, options, fresh) {
14778
14779
  url: destination.href, method: 'GET',
14779
14780
  changeState: options.history !== 'none', replace: options.history === 'replace',
14780
14781
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
14781
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
14782
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
14782
14783
  onEnd: context => { result = context; }
14783
14784
  });
14784
14785
  return result;
14785
14786
  }
14786
14787
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
14787
14788
  function navigate(url, options = {}) {
14788
- return visit(url, options, false);
14789
+ return visit(url, options);
14789
14790
  }
14790
14791
  /** Fetch the current URL again, preserving history and scroll by default. */
14791
- function refresh(options = {}) {
14792
- return visit(location.href, { history: 'none', scroll: false, ...options }, true);
14792
+ function refresh({ background, strictSource, ...options } = {}) {
14793
+ if (strictSource === true && options.target === undefined) {
14794
+ return Promise.reject(new TypeError('Gyos.refresh with strictSource requires a target selector.'));
14795
+ }
14796
+ return visit(location.href, { history: 'none', scroll: false, ...options }, {
14797
+ fresh: true, background: background === true, strictSource: strictSource === true
14798
+ });
14793
14799
  }
14794
14800
  const snapshots = new Map();
14795
14801
  const preloadCache = new Map();
14802
+ let preloadHover;
14796
14803
  const scrollPositions = new Map();
14797
14804
  const historyFragments = new Map();
14798
14805
  let routerStarted = false;
@@ -14802,6 +14809,7 @@ let progressBarEnabled = true;
14802
14809
  let currentUrl = canonicalUrl(window.location.href, true);
14803
14810
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
14804
14811
  let activeNavigation = null;
14812
+ let waitingNavigations = 0;
14805
14813
  // Global progress bar instance
14806
14814
  let progressBar = null;
14807
14815
  const beforeNavigate = [];
@@ -14847,6 +14855,8 @@ function startRouter(options) {
14847
14855
  document.addEventListener('click', onClick);
14848
14856
  document.addEventListener('submit', onSubmit);
14849
14857
  document.addEventListener('mouseover', onMouseOverCapture, true);
14858
+ document.addEventListener('mousemove', onPreloadMouseMove, true);
14859
+ document.addEventListener('mouseleave', clearPreloadHover);
14850
14860
  window.addEventListener('popstate', onPopState);
14851
14861
  if ('scrollRestoration' in history) {
14852
14862
  history.scrollRestoration = 'manual';
@@ -15118,6 +15128,7 @@ function discardPreload(key) {
15118
15128
  entry?.controller.abort();
15119
15129
  }
15120
15130
  function clearPreloads() {
15131
+ clearPreloadHover();
15121
15132
  for (const key of preloadCache.keys())
15122
15133
  discardPreload(key);
15123
15134
  }
@@ -15192,19 +15203,27 @@ function fallbackFromBoost(opts, url, requestSent) {
15192
15203
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
15193
15204
  }
15194
15205
  async function navigateRequest(opts) {
15195
- const finishUnstarted = () => {
15206
+ const finishUnstarted = (signal = opts.signal) => {
15196
15207
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
15197
15208
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
15198
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
15209
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
15199
15210
  };
15200
15211
  if (opts.signal?.aborted) {
15201
15212
  finishUnstarted();
15202
15213
  return;
15203
15214
  }
15215
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
15216
+ const skipped = new AbortController();
15217
+ skipped.abort();
15218
+ finishUnstarted(skipped.signal);
15219
+ return;
15220
+ }
15204
15221
  while (activeNavigation) {
15205
15222
  const previous = activeNavigation;
15206
15223
  if (previous.committing) {
15207
15224
  log('[GyosRouter] Waiting for previous DOM commit');
15225
+ // End hooks can request a refresh before this waiter resumes.
15226
+ waitingNavigations++;
15208
15227
  try {
15209
15228
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
15210
15229
  }
@@ -15212,6 +15231,9 @@ async function navigateRequest(opts) {
15212
15231
  finishUnstarted();
15213
15232
  return;
15214
15233
  }
15234
+ finally {
15235
+ waitingNavigations--;
15236
+ }
15215
15237
  continue;
15216
15238
  }
15217
15239
  log('[GyosRouter] Cancelling previous navigation');
@@ -15246,14 +15268,16 @@ async function navigateRequest(opts) {
15246
15268
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
15247
15269
  : null;
15248
15270
  const fallback = (url, requestSent) => {
15249
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
15271
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
15250
15272
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
15251
- fallbackFromBoost(opts, url, requestSent);
15273
+ if (!opts.background)
15274
+ fallbackFromBoost(opts, url, requestSent);
15252
15275
  };
15276
+ const navigationProgress = opts.background ? null : progressBar;
15253
15277
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
15254
15278
  const setRand = () => {
15255
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
15256
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
15279
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
15280
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
15257
15281
  }
15258
15282
  };
15259
15283
  try {
@@ -15262,7 +15286,7 @@ async function navigateRequest(opts) {
15262
15286
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
15263
15287
  runNavigationHooks(beforeNavigate, opts.url, context);
15264
15288
  assertActiveNavigation(transaction);
15265
- progressBar?.start();
15289
+ navigationProgress?.start();
15266
15290
  // popstate already points history at the destination entry. Keep the visible
15267
15291
  // page's position under currentUrl without overwriting that destination state.
15268
15292
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -15331,6 +15355,8 @@ async function navigateRequest(opts) {
15331
15355
  signal.addEventListener('abort', discard, { once: true });
15332
15356
  try {
15333
15357
  response = await waitForNavigation(entry.request, signal);
15358
+ if (response.ok && preloadHover?.url === preloadKey)
15359
+ preloadHover.used = true;
15334
15360
  }
15335
15361
  catch (error) {
15336
15362
  if (error.name === 'AbortError')
@@ -15349,15 +15375,16 @@ async function navigateRequest(opts) {
15349
15375
  else {
15350
15376
  try {
15351
15377
  // Update progress to rand 20 - 30%
15352
- progressBar?.setProgress(Math.random() * 10 + 20);
15353
- transaction.interval = setInterval(setRand, 300);
15378
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
15379
+ if (navigationProgress)
15380
+ transaction.interval = setInterval(setRand, 300);
15354
15381
  response = await fetch(finalUrl, init);
15355
15382
  assertActiveNavigation(transaction);
15356
15383
  // A successful write can change HTML at any URL, including after a redirect.
15357
15384
  if (response.ok && init.method !== 'GET')
15358
15385
  clearPreloads();
15359
15386
  // Update progress to rand 70 - 80% after fetch
15360
- progressBar?.setProgress(Math.random() * 10 + 70);
15387
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
15361
15388
  if (transaction.interval)
15362
15389
  clearInterval(transaction.interval);
15363
15390
  transaction.interval = null;
@@ -15395,6 +15422,13 @@ async function navigateRequest(opts) {
15395
15422
  const doc = html
15396
15423
  ? parseNavigationDocument(html)
15397
15424
  : document.implementation.createHTMLDocument();
15425
+ const matchedSource = opts.strictSource && opts.targetSelector
15426
+ ? doc.querySelector(opts.targetSelector)
15427
+ : null;
15428
+ if (opts.strictSource && !(matchedSource instanceof HTMLElement) && isFullDocument(html || '')) {
15429
+ context.outcome = 'invalid-response';
15430
+ return; // A complete response must identify the requested region before any swap or snapshot.
15431
+ }
15398
15432
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
15399
15433
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
15400
15434
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -15409,7 +15443,9 @@ async function navigateRequest(opts) {
15409
15443
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
15410
15444
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
15411
15445
  // Pick source fragment
15412
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
15446
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
15447
+ ? matchedSource
15448
+ : pickSourceFragment(doc, target, targetOutletIndex));
15413
15449
  stripInertFallbackContent(src);
15414
15450
  assertActiveNavigation(transaction);
15415
15451
  // Final URL is response.url because in headers there could be redirects
@@ -15432,7 +15468,7 @@ async function navigateRequest(opts) {
15432
15468
  runNavigationHooks(beforeSwap, targetUrl, context);
15433
15469
  assertActiveNavigation(transaction);
15434
15470
  // Update progress to rand 80 - 90% before swap
15435
- progressBar?.setProgress(Math.random() * 10 + 80);
15471
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
15436
15472
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
15437
15473
  ? cloneHistoryTarget(target)
15438
15474
  : null;
@@ -15530,7 +15566,7 @@ async function navigateRequest(opts) {
15530
15566
  target = committedTarget;
15531
15567
  context.target = committedTarget;
15532
15568
  context.committed = true;
15533
- progressBar?.reconnect();
15569
+ navigationProgress?.reconnect();
15534
15570
  committedRoots = roots;
15535
15571
  }
15536
15572
  });
@@ -15562,7 +15598,7 @@ async function navigateRequest(opts) {
15562
15598
  }
15563
15599
  };
15564
15600
  // Apply view transition if available
15565
- if (document.startViewTransition) {
15601
+ if (!opts.background && document.startViewTransition) {
15566
15602
  const transition = document.startViewTransition(() => performUpdate());
15567
15603
  consumeViewTransitionRejection(transition?.ready);
15568
15604
  consumeViewTransitionRejection(transition?.finished);
@@ -15613,7 +15649,7 @@ async function navigateRequest(opts) {
15613
15649
  hideTargetSpinner(transaction.spinner);
15614
15650
  if (activeNavigation?.id === transaction.id) {
15615
15651
  activeNavigation = null;
15616
- progressBar?.complete();
15652
+ navigationProgress?.complete();
15617
15653
  }
15618
15654
  opts.signal?.removeEventListener('abort', abort);
15619
15655
  transaction.resolveDone();
@@ -15725,15 +15761,34 @@ function onClick(event) {
15725
15761
  navigateRequest({ url: url.toString(), method: 'GET', trigger: link, changeState: true });
15726
15762
  }
15727
15763
  function onMouseOverCapture(event) {
15728
- const target = event.target;
15729
- if (!target)
15730
- return;
15731
- const link = target.closest('a[g-preload]');
15764
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
15732
15765
  if (!link)
15733
15766
  return;
15734
15767
  const url = eligibleAnchorUrl(link);
15735
- if (url)
15736
- preload(url.href);
15768
+ if (!url)
15769
+ return;
15770
+ const hover = preloadHover;
15771
+ if (hover?.used && hover.url === url.href && ((event.clientX === hover.x && event.clientY === hover.y) ||
15772
+ (event.relatedTarget instanceof Node && link.contains(event.relatedTarget))))
15773
+ return;
15774
+ preloadHover = { url: url.href, x: event.clientX, y: event.clientY, used: false };
15775
+ preload(url.href);
15776
+ }
15777
+ function onPreloadMouseMove(event) {
15778
+ const hover = preloadHover;
15779
+ // The browser can retarget mouse events after a swap with the pointer still in place.
15780
+ if (!hover || (event.clientX === hover.x && event.clientY === hover.y))
15781
+ return;
15782
+ const link = event.target instanceof Element ? event.target.closest('a[g-preload]') : null;
15783
+ if (!(link instanceof HTMLAnchorElement) || link.href !== hover.url) {
15784
+ clearPreloadHover();
15785
+ return;
15786
+ }
15787
+ hover.x = event.clientX;
15788
+ hover.y = event.clientY;
15789
+ }
15790
+ function clearPreloadHover() {
15791
+ preloadHover = undefined;
15737
15792
  }
15738
15793
  function onSubmit(event) {
15739
15794
  if (event.defaultPrevented)
@@ -15886,7 +15941,7 @@ const Gyos = {
15886
15941
  portalCreate,
15887
15942
  portalDestroy,
15888
15943
  setCspNonce,
15889
- version: '0.5.7'
15944
+ version: '0.6.0'
15890
15945
  };
15891
15946
 
15892
15947
  setExpressionRuntime(cspExpressionRuntime);