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.
package/dist/gyos.js CHANGED
@@ -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
  */
@@ -8151,7 +8151,7 @@
8151
8151
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
8152
8152
  */
8153
8153
  const submittingForms = new WeakSet();
8154
- async function visit(url, options, fresh) {
8154
+ async function visit(url, options, refreshPolicy) {
8155
8155
  const destination = new URL(url, document.baseURI);
8156
8156
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
8157
8157
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -8168,18 +8168,23 @@
8168
8168
  url: destination.href, method: 'GET',
8169
8169
  changeState: options.history !== 'none', replace: options.history === 'replace',
8170
8170
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
8171
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
8171
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
8172
8172
  onEnd: context => { result = context; }
8173
8173
  });
8174
8174
  return result;
8175
8175
  }
8176
8176
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
8177
8177
  function navigate(url, options = {}) {
8178
- return visit(url, options, false);
8178
+ return visit(url, options);
8179
8179
  }
8180
8180
  /** Fetch the current URL again, preserving history and scroll by default. */
8181
- function refresh(options = {}) {
8182
- 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
+ });
8183
8188
  }
8184
8189
  const snapshots = new Map();
8185
8190
  const preloadCache = new Map();
@@ -8193,6 +8198,7 @@
8193
8198
  let currentUrl = canonicalUrl(window.location.href, true);
8194
8199
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
8195
8200
  let activeNavigation = null;
8201
+ let waitingNavigations = 0;
8196
8202
  // Global progress bar instance
8197
8203
  let progressBar = null;
8198
8204
  const beforeNavigate = [];
@@ -8586,19 +8592,27 @@
8586
8592
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
8587
8593
  }
8588
8594
  async function navigateRequest(opts) {
8589
- const finishUnstarted = () => {
8595
+ const finishUnstarted = (signal = opts.signal) => {
8590
8596
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
8591
8597
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
8592
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8598
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
8593
8599
  };
8594
8600
  if (opts.signal?.aborted) {
8595
8601
  finishUnstarted();
8596
8602
  return;
8597
8603
  }
8604
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
8605
+ const skipped = new AbortController();
8606
+ skipped.abort();
8607
+ finishUnstarted(skipped.signal);
8608
+ return;
8609
+ }
8598
8610
  while (activeNavigation) {
8599
8611
  const previous = activeNavigation;
8600
8612
  if (previous.committing) {
8601
8613
  log('[GyosRouter] Waiting for previous DOM commit');
8614
+ // End hooks can request a refresh before this waiter resumes.
8615
+ waitingNavigations++;
8602
8616
  try {
8603
8617
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
8604
8618
  }
@@ -8606,6 +8620,9 @@
8606
8620
  finishUnstarted();
8607
8621
  return;
8608
8622
  }
8623
+ finally {
8624
+ waitingNavigations--;
8625
+ }
8609
8626
  continue;
8610
8627
  }
8611
8628
  log('[GyosRouter] Cancelling previous navigation');
@@ -8640,14 +8657,16 @@
8640
8657
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
8641
8658
  : null;
8642
8659
  const fallback = (url, requestSent) => {
8643
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
8660
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
8644
8661
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
8645
- fallbackFromBoost(opts, url, requestSent);
8662
+ if (!opts.background)
8663
+ fallbackFromBoost(opts, url, requestSent);
8646
8664
  };
8665
+ const navigationProgress = opts.background ? null : progressBar;
8647
8666
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
8648
8667
  const setRand = () => {
8649
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
8650
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
8668
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
8669
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
8651
8670
  }
8652
8671
  };
8653
8672
  try {
@@ -8656,7 +8675,7 @@
8656
8675
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
8657
8676
  runNavigationHooks(beforeNavigate, opts.url, context);
8658
8677
  assertActiveNavigation(transaction);
8659
- progressBar?.start();
8678
+ navigationProgress?.start();
8660
8679
  // popstate already points history at the destination entry. Keep the visible
8661
8680
  // page's position under currentUrl without overwriting that destination state.
8662
8681
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -8745,15 +8764,16 @@
8745
8764
  else {
8746
8765
  try {
8747
8766
  // Update progress to rand 20 - 30%
8748
- progressBar?.setProgress(Math.random() * 10 + 20);
8749
- transaction.interval = setInterval(setRand, 300);
8767
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
8768
+ if (navigationProgress)
8769
+ transaction.interval = setInterval(setRand, 300);
8750
8770
  response = await fetch(finalUrl, init);
8751
8771
  assertActiveNavigation(transaction);
8752
8772
  // A successful write can change HTML at any URL, including after a redirect.
8753
8773
  if (response.ok && init.method !== 'GET')
8754
8774
  clearPreloads();
8755
8775
  // Update progress to rand 70 - 80% after fetch
8756
- progressBar?.setProgress(Math.random() * 10 + 70);
8776
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
8757
8777
  if (transaction.interval)
8758
8778
  clearInterval(transaction.interval);
8759
8779
  transaction.interval = null;
@@ -8791,6 +8811,13 @@
8791
8811
  const doc = html
8792
8812
  ? parseNavigationDocument(html)
8793
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
+ }
8794
8821
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
8795
8822
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
8796
8823
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -8805,7 +8832,9 @@
8805
8832
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
8806
8833
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
8807
8834
  // Pick source fragment
8808
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
8835
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
8836
+ ? matchedSource
8837
+ : pickSourceFragment(doc, target, targetOutletIndex));
8809
8838
  stripInertFallbackContent(src);
8810
8839
  assertActiveNavigation(transaction);
8811
8840
  // Final URL is response.url because in headers there could be redirects
@@ -8828,7 +8857,7 @@
8828
8857
  runNavigationHooks(beforeSwap, targetUrl, context);
8829
8858
  assertActiveNavigation(transaction);
8830
8859
  // Update progress to rand 80 - 90% before swap
8831
- progressBar?.setProgress(Math.random() * 10 + 80);
8860
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
8832
8861
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
8833
8862
  ? cloneHistoryTarget(target)
8834
8863
  : null;
@@ -8926,7 +8955,7 @@
8926
8955
  target = committedTarget;
8927
8956
  context.target = committedTarget;
8928
8957
  context.committed = true;
8929
- progressBar?.reconnect();
8958
+ navigationProgress?.reconnect();
8930
8959
  committedRoots = roots;
8931
8960
  }
8932
8961
  });
@@ -8958,7 +8987,7 @@
8958
8987
  }
8959
8988
  };
8960
8989
  // Apply view transition if available
8961
- if (document.startViewTransition) {
8990
+ if (!opts.background && document.startViewTransition) {
8962
8991
  const transition = document.startViewTransition(() => performUpdate());
8963
8992
  consumeViewTransitionRejection(transition?.ready);
8964
8993
  consumeViewTransitionRejection(transition?.finished);
@@ -9009,7 +9038,7 @@
9009
9038
  hideTargetSpinner(transaction.spinner);
9010
9039
  if (activeNavigation?.id === transaction.id) {
9011
9040
  activeNavigation = null;
9012
- progressBar?.complete();
9041
+ navigationProgress?.complete();
9013
9042
  }
9014
9043
  opts.signal?.removeEventListener('abort', abort);
9015
9044
  transaction.resolveDone();
@@ -9301,7 +9330,7 @@
9301
9330
  portalCreate,
9302
9331
  portalDestroy,
9303
9332
  setCspNonce,
9304
- version: '0.5.8'
9333
+ version: '0.6.0'
9305
9334
  };
9306
9335
 
9307
9336
  setExpressionRuntime(standardExpressionRuntime);