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
  */
@@ -14762,7 +14762,7 @@ function prepareNavigationHtml(html) {
14762
14762
  * Implements attribute-driven routing, snapshot caching, and persistent islands.
14763
14763
  */
14764
14764
  const submittingForms = new WeakSet();
14765
- async function visit(url, options, fresh) {
14765
+ async function visit(url, options, refreshPolicy) {
14766
14766
  const destination = new URL(url, document.baseURI);
14767
14767
  if (!['http:', 'https:'].includes(destination.protocol) || destination.origin !== location.origin || destination.username || destination.password) {
14768
14768
  throw new TypeError('Gyos.navigate requires a same-origin HTTP(S) URL without credentials.');
@@ -14779,18 +14779,23 @@ async function visit(url, options, fresh) {
14779
14779
  url: destination.href, method: 'GET',
14780
14780
  changeState: options.history !== 'none', replace: options.history === 'replace',
14781
14781
  targetSelector: options.target, strictTarget: options.target !== undefined, swapMode: options.swap,
14782
- currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, fresh,
14782
+ currentHead: options.currentHead, scroll: options.scroll, signal: options.signal, ...refreshPolicy,
14783
14783
  onEnd: context => { result = context; }
14784
14784
  });
14785
14785
  return result;
14786
14786
  }
14787
14787
  /** Navigate through the boost pipeline. Resolves after navigation-end hooks. */
14788
14788
  function navigate(url, options = {}) {
14789
- return visit(url, options, false);
14789
+ return visit(url, options);
14790
14790
  }
14791
14791
  /** Fetch the current URL again, preserving history and scroll by default. */
14792
- function refresh(options = {}) {
14793
- 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
+ });
14794
14799
  }
14795
14800
  const snapshots = new Map();
14796
14801
  const preloadCache = new Map();
@@ -14804,6 +14809,7 @@ let progressBarEnabled = true;
14804
14809
  let currentUrl = canonicalUrl(window.location.href, true);
14805
14810
  let currentHistoryEntryId = history.state?.gyosEntryId || null;
14806
14811
  let activeNavigation = null;
14812
+ let waitingNavigations = 0;
14807
14813
  // Global progress bar instance
14808
14814
  let progressBar = null;
14809
14815
  const beforeNavigate = [];
@@ -15197,19 +15203,27 @@ function fallbackFromBoost(opts, url, requestSent) {
15197
15203
  console.error(`[GyosRouter] ${method} navigation could not be boosted; the request will not be replayed.`);
15198
15204
  }
15199
15205
  async function navigateRequest(opts) {
15200
- const finishUnstarted = () => {
15206
+ const finishUnstarted = (signal = opts.signal) => {
15201
15207
  opts.onEnd?.({ from: currentUrl, url: opts.url, method: opts.method,
15202
15208
  trigger: opts.trigger || null, target: null, form: null, popstate: false,
15203
- signal: opts.signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
15209
+ signal, status: null, committed: false, fallback: false, outcome: 'aborted' });
15204
15210
  };
15205
15211
  if (opts.signal?.aborted) {
15206
15212
  finishUnstarted();
15207
15213
  return;
15208
15214
  }
15215
+ if (opts.background && (activeNavigation || waitingNavigations > 0)) {
15216
+ const skipped = new AbortController();
15217
+ skipped.abort();
15218
+ finishUnstarted(skipped.signal);
15219
+ return;
15220
+ }
15209
15221
  while (activeNavigation) {
15210
15222
  const previous = activeNavigation;
15211
15223
  if (previous.committing) {
15212
15224
  log('[GyosRouter] Waiting for previous DOM commit');
15225
+ // End hooks can request a refresh before this waiter resumes.
15226
+ waitingNavigations++;
15213
15227
  try {
15214
15228
  await (opts.signal ? waitForNavigation(previous.done, opts.signal) : previous.done);
15215
15229
  }
@@ -15217,6 +15231,9 @@ async function navigateRequest(opts) {
15217
15231
  finishUnstarted();
15218
15232
  return;
15219
15233
  }
15234
+ finally {
15235
+ waitingNavigations--;
15236
+ }
15220
15237
  continue;
15221
15238
  }
15222
15239
  log('[GyosRouter] Cancelling previous navigation');
@@ -15251,14 +15268,16 @@ async function navigateRequest(opts) {
15251
15268
  ? { form: opts.form, url: opts.url, status: null, outcome: null, committed: false, fallback: false }
15252
15269
  : null;
15253
15270
  const fallback = (url, requestSent) => {
15254
- context.fallback = context.method === 'GET' || (!requestSent && !!opts.form);
15271
+ context.fallback = !opts.background && (context.method === 'GET' || (!requestSent && !!opts.form));
15255
15272
  context.outcome ?? (context.outcome = context.fallback ? 'fallback' : 'invalid-response');
15256
- fallbackFromBoost(opts, url, requestSent);
15273
+ if (!opts.background)
15274
+ fallbackFromBoost(opts, url, requestSent);
15257
15275
  };
15276
+ const navigationProgress = opts.background ? null : progressBar;
15258
15277
  const hasTargetSpinner = opts.trigger?.hasAttribute('g-router-spin');
15259
15278
  const setRand = () => {
15260
- if (isActiveNavigation(transaction) && progressBar?.getCurrentProgress() < 70) {
15261
- progressBar?.setProgress(progressBar?.getCurrentProgress() + Math.random() * 8);
15279
+ if (isActiveNavigation(transaction) && navigationProgress && navigationProgress.getCurrentProgress() < 70) {
15280
+ navigationProgress.setProgress(navigationProgress.getCurrentProgress() + Math.random() * 8);
15262
15281
  }
15263
15282
  };
15264
15283
  try {
@@ -15267,7 +15286,7 @@ async function navigateRequest(opts) {
15267
15286
  document.dispatchEvent(new CustomEvent('gyos:form-start', { detail: { ...formEvent } }));
15268
15287
  runNavigationHooks(beforeNavigate, opts.url, context);
15269
15288
  assertActiveNavigation(transaction);
15270
- progressBar?.start();
15289
+ navigationProgress?.start();
15271
15290
  // popstate already points history at the destination entry. Keep the visible
15272
15291
  // page's position under currentUrl without overwriting that destination state.
15273
15292
  rememberScrollPosition(opts.scrollSourceEntryId ?? currentHistoryEntryId);
@@ -15356,15 +15375,16 @@ async function navigateRequest(opts) {
15356
15375
  else {
15357
15376
  try {
15358
15377
  // Update progress to rand 20 - 30%
15359
- progressBar?.setProgress(Math.random() * 10 + 20);
15360
- transaction.interval = setInterval(setRand, 300);
15378
+ navigationProgress?.setProgress(Math.random() * 10 + 20);
15379
+ if (navigationProgress)
15380
+ transaction.interval = setInterval(setRand, 300);
15361
15381
  response = await fetch(finalUrl, init);
15362
15382
  assertActiveNavigation(transaction);
15363
15383
  // A successful write can change HTML at any URL, including after a redirect.
15364
15384
  if (response.ok && init.method !== 'GET')
15365
15385
  clearPreloads();
15366
15386
  // Update progress to rand 70 - 80% after fetch
15367
- progressBar?.setProgress(Math.random() * 10 + 70);
15387
+ navigationProgress?.setProgress(Math.random() * 10 + 70);
15368
15388
  if (transaction.interval)
15369
15389
  clearInterval(transaction.interval);
15370
15390
  transaction.interval = null;
@@ -15402,6 +15422,13 @@ async function navigateRequest(opts) {
15402
15422
  const doc = html
15403
15423
  ? parseNavigationDocument(html)
15404
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
+ }
15405
15432
  // Detached parsing cannot tell whether the live CSP permits an incoming base.
15406
15433
  // Let native navigation enforce base-uri instead of authorizing its URLs ourselves.
15407
15434
  if (doc.querySelector('base[href]') || opts.historyFragment?.querySelector('base[href]')) {
@@ -15416,7 +15443,9 @@ async function navigateRequest(opts) {
15416
15443
  && !opts.historyFragment && (opts.popstate && snapshots.has(urlWithoutHash)
15417
15444
  ? snapshots.get(urlWithoutHash).fullDocument : isFullDocument(html || ''));
15418
15445
  // Pick source fragment
15419
- const src = opts.historyFragment || pickSourceFragment(doc, target, targetOutletIndex);
15446
+ const src = opts.historyFragment || (matchedSource instanceof HTMLElement
15447
+ ? matchedSource
15448
+ : pickSourceFragment(doc, target, targetOutletIndex));
15420
15449
  stripInertFallbackContent(src);
15421
15450
  assertActiveNavigation(transaction);
15422
15451
  // Final URL is response.url because in headers there could be redirects
@@ -15439,7 +15468,7 @@ async function navigateRequest(opts) {
15439
15468
  runNavigationHooks(beforeSwap, targetUrl, context);
15440
15469
  assertActiveNavigation(transaction);
15441
15470
  // Update progress to rand 80 - 90% before swap
15442
- progressBar?.setProgress(Math.random() * 10 + 80);
15471
+ navigationProgress?.setProgress(Math.random() * 10 + 80);
15443
15472
  const outgoingHistoryFragment = changesState && !opts.replace && !isFullOutletNavigation
15444
15473
  ? cloneHistoryTarget(target)
15445
15474
  : null;
@@ -15537,7 +15566,7 @@ async function navigateRequest(opts) {
15537
15566
  target = committedTarget;
15538
15567
  context.target = committedTarget;
15539
15568
  context.committed = true;
15540
- progressBar?.reconnect();
15569
+ navigationProgress?.reconnect();
15541
15570
  committedRoots = roots;
15542
15571
  }
15543
15572
  });
@@ -15569,7 +15598,7 @@ async function navigateRequest(opts) {
15569
15598
  }
15570
15599
  };
15571
15600
  // Apply view transition if available
15572
- if (document.startViewTransition) {
15601
+ if (!opts.background && document.startViewTransition) {
15573
15602
  const transition = document.startViewTransition(() => performUpdate());
15574
15603
  consumeViewTransitionRejection(transition?.ready);
15575
15604
  consumeViewTransitionRejection(transition?.finished);
@@ -15620,7 +15649,7 @@ async function navigateRequest(opts) {
15620
15649
  hideTargetSpinner(transaction.spinner);
15621
15650
  if (activeNavigation?.id === transaction.id) {
15622
15651
  activeNavigation = null;
15623
- progressBar?.complete();
15652
+ navigationProgress?.complete();
15624
15653
  }
15625
15654
  opts.signal?.removeEventListener('abort', abort);
15626
15655
  transaction.resolveDone();
@@ -15912,7 +15941,7 @@ const Gyos = {
15912
15941
  portalCreate,
15913
15942
  portalDestroy,
15914
15943
  setCspNonce,
15915
- version: '0.5.8'
15944
+ version: '0.6.0'
15916
15945
  };
15917
15946
 
15918
15947
  setExpressionRuntime(cspExpressionRuntime);