gyosjs 0.5.8 → 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.8
2
+ * GyosJS v0.6.0
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -8133,7 +8133,7 @@
8133
8133
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
8134
8134
  */
8135
8135
  const submittingForms = new WeakSet();
8136
- async function visit(url, options, fresh) {
8136
+ async function visit(url, options, refreshPolicy) {
8137
8137
  const destination = new URL(url, document.baseURI);
8138
8138
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
8139
8139
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -8150,18 +8150,23 @@
8150
8150
  url: destination.href, method: 'GET',
8151
8151
  changeState: options.history !== 'none', replace: options.history === 'replace',
8152
8152
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
8153
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
8153
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
8154
8154
  onEnd: context => { result = context; }
8155
8155
  });
8156
8156
  return result;
8157
8157
  }
8158
8158
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
8159
8159
  function navigate(url, options = {}) {
8160
- return visit(url, options, false);
8160
+ return visit(url, options);
8161
8161
  }
8162
8162
  /** Fetch the current URL again, preserving history and scroll by default. */
8163
- function refresh(options = {}) {
8164
- return visit(location.href, { history: 'none', scroll: false, ...options }, true);
8163
+ function refresh({ background, strictSource, ...options } = {}) {
8164
+ if (strictSource === true && options.target === undefined) {
8165
+ return Promise.reject(new TypeError('Gyos.refresh with strictSource requires a target selector.'));
8166
+ }
8167
+ return visit(location.href, { history: 'none', scroll: false, ...options }, {
8168
+ fresh: true, background: background === true, strictSource: strictSource === true
8169
+ });
8165
8170
  }
8166
8171
  const snapshots = new Map();
8167
8172
  const preloadCache = new Map();
@@ -8175,6 +8180,7 @@
8175
8180
  let currentUrl = canonicalUrl(window.location.href, true);
8176
8181
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
8177
8182
  let activeNavigation = null;
8183
+ let waitingNavigations = 0;
8178
8184
  // Global progress bar instance
8179
8185
  let progressBar = null;
8180
8186
  const beforeNavigate = [];
@@ -8568,19 +8574,27 @@
8568
8574
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8569
8575
  }
8570
8576
  async function navigateRequest(opts) {
8571
- const finishUnstarted = () => {
8577
+ const finishUnstarted = (signal = opts.signal) => {
8572
8578
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8573
8579
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
8574
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8580
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8575
8581
  };
8576
8582
  if (opts.signal?.aborted) {
8577
8583
  finishUnstarted();
8578
8584
  return;
8579
8585
  }
8586
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
8587
+ const skipped = new AbortController();
8588
+ skipped.abort();
8589
+ finishUnstarted(skipped.signal);
8590
+ return;
8591
+ }
8580
8592
  while (activeNavigation) {
8581
8593
  const previous = activeNavigation;
8582
8594
  if (previous.committing) {
8583
8595
  log('[GyosRouter] Waiting for previous DOM commit');
8596
+ // End hooks can request a refresh before this waiter resumes.
8597
+ waitingNavigations++;
8584
8598
  try {
8585
8599
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8586
8600
  }
@@ -8588,6 +8602,9 @@
8588
8602
  finishUnstarted();
8589
8603
  return;
8590
8604
  }
8605
+ finally {
8606
+ waitingNavigations--;
8607
+ }
8591
8608
  continue;
8592
8609
  }
8593
8610
  log('[GyosRouter] Cancelling previous navigation');
@@ -8622,14 +8639,16 @@
8622
8639
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
8623
8640
  : null;
8624
8641
  const fallback = (url, requestSent) => {
8625
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
8642
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
8626
8643
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
8627
- fallbackFromBoost(opts, url, requestSent);
8644
+ if (!opts.background)
8645
+ fallbackFromBoost(opts, url, requestSent);
8628
8646
  };
8647
+ const navigationProgress = opts.background ? null : progressBar;
8629
8648
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
8630
8649
  const setRand = () => {
8631
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
8632
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
8650
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
8651
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
8633
8652
  }
8634
8653
  };
8635
8654
  try {
@@ -8638,7 +8657,7 @@
8638
8657
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8639
8658
  runNavigationHooks(beforeNavigate, opts.url, context);
8640
8659
  assertActiveNavigation(transaction);
8641
- progressBar?.start();
8660
+ navigationProgress?.start();
8642
8661
  // popstate already points history at the destination entry. Keep the visible
8643
8662
  // page's position under currentUrl without overwriting that destination state.
8644
8663
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -8727,15 +8746,16 @@
8727
8746
  else {
8728
8747
  try {
8729
8748
  // Update progress to rand 20 - 30%
8730
- progressBar?.setProgress(Math.random() * 10 + 20);
8731
- transaction.interval = setInterval(setRand, 300);
8749
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
8750
+ if (navigationProgress)
8751
+ transaction.interval = setInterval(setRand, 300);
8732
8752
  response = await fetch(finalUrl, init);
8733
8753
  assertActiveNavigation(transaction);
8734
8754
  // A successful write can change HTML at any URL, including after a redirect.
8735
8755
  if (response.ok && init.method !== 'GET')
8736
8756
  clearPreloads();
8737
8757
  // Update progress to rand 70 - 80% after fetch
8738
- progressBar?.setProgress(Math.random() * 10 + 70);
8758
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
8739
8759
  if (transaction.interval)
8740
8760
  clearInterval(transaction.interval);
8741
8761
  transaction.interval = null;
@@ -8773,6 +8793,13 @@
8773
8793
  const doc = html
8774
8794
  ? parseNavigationDocument(html)
8775
8795
  : document.implementation.createHTMLDocument();
8796
+ const matchedSource = opts.strictSource && opts.targetSelector
8797
+ ? doc.querySelector(opts.targetSelector)
8798
+ : null;
8799
+ if (opts.strictSource && !(matchedSource instanceof HTMLElement) && isFullDocument(html || '')) {
8800
+ context.outcome = 'invalid-response';
8801
+ return; // A complete response must identify the requested region before any swap or snapshot.
8802
+ }
8776
8803
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
8777
8804
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
8778
8805
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -8787,7 +8814,9 @@
8787
8814
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
8788
8815
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
8789
8816
  // Pick source fragment
8790
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
8817
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
8818
+ ? matchedSource
8819
+ : pickSourceFragment(doc, target, targetOutletIndex));
8791
8820
  stripInertFallbackContent(src);
8792
8821
  assertActiveNavigation(transaction);
8793
8822
  // Final URL is response.url because in headers there could be redirects
@@ -8810,7 +8839,7 @@
8810
8839
  runNavigationHooks(beforeSwap, targetUrl, context);
8811
8840
  assertActiveNavigation(transaction);
8812
8841
  // Update progress to rand 80 - 90% before swap
8813
- progressBar?.setProgress(Math.random() * 10 + 80);
8842
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
8814
8843
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
8815
8844
  ? cloneHistoryTarget(target)
8816
8845
  : null;
@@ -8908,7 +8937,7 @@
8908
8937
  target = committedTarget;
8909
8938
  context.target = committedTarget;
8910
8939
  context.committed = true;
8911
- progressBar?.reconnect();
8940
+ navigationProgress?.reconnect();
8912
8941
  committedRoots = roots;
8913
8942
  }
8914
8943
  });
@@ -8940,7 +8969,7 @@
8940
8969
  }
8941
8970
  };
8942
8971
  // Apply view transition if available
8943
- if (document.startViewTransition) {
8972
+ if (!opts.background && document.startViewTransition) {
8944
8973
  const transition = document.startViewTransition(() => performUpdate());
8945
8974
  consumeViewTransitionRejection(transition?.ready);
8946
8975
  consumeViewTransitionRejection(transition?.finished);
@@ -8991,7 +9020,7 @@
8991
9020
  hideTargetSpinner(transaction.spinner);
8992
9021
  if (activeNavigation?.id === transaction.id) {
8993
9022
  activeNavigation = null;
8994
- progressBar?.complete();
9023
+ navigationProgress?.complete();
8995
9024
  }
8996
9025
  opts.signal?.removeEventListener('abort', abort);
8997
9026
  transaction.resolveDone();
@@ -9283,7 +9312,7 @@
9283
9312
  portalCreate,
9284
9313
  portalDestroy,
9285
9314
  setCspNonce,
9286
- version: '0.5.8'
9315
+ version: '0.6.0'
9287
9316
  };
9288
9317
 
9289
9318
  // This file was generated. Do not modify manually!