gyosjs 0.5.6 → 0.5.7

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.6
2
+ * GyosJS v0.5.7
3
3
  * (c) 2026
4
4
  * @license MIT
5
5
  */
@@ -8164,7 +8164,6 @@
8164
8164
  }
8165
8165
  const snapshots = new Map();
8166
8166
  const preloadCache = new Map();
8167
- const preloadControllers = new Map();
8168
8167
  const scrollPositions = new Map();
8169
8168
  const historyFragments = new Map();
8170
8169
  let routerStarted = false;
@@ -8484,6 +8483,15 @@
8484
8483
  }
8485
8484
  return { finalUrl: url.toString(), init };
8486
8485
  }
8486
+ function discardPreload(key) {
8487
+ const entry = preloadCache.get(key);
8488
+ preloadCache.delete(key);
8489
+ entry?.controller.abort();
8490
+ }
8491
+ function clearPreloads() {
8492
+ for (const key of preloadCache.keys())
8493
+ discardPreload(key);
8494
+ }
8487
8495
  function preload(url) {
8488
8496
  const cacheKey = canonicalUrl(url, true);
8489
8497
  if (preloadCache.has(cacheKey))
@@ -8498,11 +8506,11 @@
8498
8506
  signal: controller.signal,
8499
8507
  mode: 'same-origin'
8500
8508
  });
8501
- preloadCache.set(cacheKey, request);
8502
- preloadControllers.set(cacheKey, controller);
8509
+ const entry = { request, controller };
8510
+ preloadCache.set(cacheKey, entry);
8503
8511
  request.catch(() => {
8504
- preloadCache.delete(cacheKey);
8505
- preloadControllers.delete(cacheKey);
8512
+ if (preloadCache.get(cacheKey) === entry)
8513
+ preloadCache.delete(cacheKey);
8506
8514
  });
8507
8515
  }
8508
8516
  async function updateHead(fromDoc, signal, currentBaseUrl) {
@@ -8681,20 +8689,21 @@
8681
8689
  else {
8682
8690
  // Check preload cache if GET
8683
8691
  const preloadKey = canonicalUrl(finalUrl, true);
8684
- if (opts.fresh) {
8685
- preloadControllers.get(preloadKey)?.abort();
8686
- preloadControllers.delete(preloadKey);
8687
- preloadCache.delete(preloadKey);
8688
- }
8689
- if (!opts.fresh && opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
8692
+ if (opts.fresh)
8693
+ discardPreload(preloadKey);
8694
+ const entry = init.method === 'GET' ? preloadCache.get(preloadKey) : undefined;
8695
+ if (entry) {
8690
8696
  log('[GyosRouter] using preload cache for', finalUrl);
8697
+ const signal = transaction.controller.signal;
8698
+ const discard = () => {
8699
+ if (preloadCache.get(preloadKey) === entry)
8700
+ discardPreload(preloadKey);
8701
+ };
8702
+ signal.addEventListener('abort', discard, { once: true });
8691
8703
  try {
8692
- response = await waitForNavigation(preloadCache.get(preloadKey), transaction.controller.signal);
8704
+ response = await waitForNavigation(entry.request, signal);
8693
8705
  }
8694
8706
  catch (error) {
8695
- preloadCache.delete(preloadKey);
8696
- preloadControllers.get(preloadKey)?.abort();
8697
- preloadControllers.delete(preloadKey);
8698
8707
  if (error.name === 'AbortError')
8699
8708
  throw error;
8700
8709
  assertActiveNavigation(transaction);
@@ -8702,8 +8711,11 @@
8702
8711
  fallback(finalUrl, true);
8703
8712
  return;
8704
8713
  }
8705
- preloadCache.delete(preloadKey); // consume cache
8706
- preloadControllers.delete(preloadKey);
8714
+ finally {
8715
+ signal.removeEventListener('abort', discard);
8716
+ if (preloadCache.get(preloadKey) === entry)
8717
+ preloadCache.delete(preloadKey);
8718
+ }
8707
8719
  }
8708
8720
  else {
8709
8721
  try {
@@ -8712,6 +8724,9 @@
8712
8724
  transaction.interval = setInterval(setRand, 300);
8713
8725
  response = await fetch(finalUrl, init);
8714
8726
  assertActiveNavigation(transaction);
8727
+ // A successful write can change HTML at any URL, including after a redirect.
8728
+ if (response.ok && init.method !== 'GET')
8729
+ clearPreloads();
8715
8730
  // Update progress to rand 70 - 80% after fetch
8716
8731
  progressBar?.setProgress(Math.random() * 10 + 70);
8717
8732
  if (transaction.interval)
@@ -8851,6 +8866,15 @@
8851
8866
  // The global outlet is also the incoming layout root. Keep its node for
8852
8867
  // inner swaps, but update attributes such as g-scope and layout classes.
8853
8868
  try {
8869
+ if (syncDocumentAttributes && !document.querySelector('base[href]')) {
8870
+ document.head.querySelectorAll('base[target]').forEach(base => base.remove());
8871
+ const baseTarget = doc.head.querySelector('base[target]')?.getAttribute('target');
8872
+ if (baseTarget != null) {
8873
+ const base = document.createElement('base');
8874
+ base.target = baseTarget;
8875
+ document.head.prepend(base);
8876
+ }
8877
+ }
8854
8878
  target = await performSwap(target, src, swapMode, {
8855
8879
  syncRootAttributes: isFullOutletNavigation,
8856
8880
  signal: transaction.controller.signal,
@@ -8976,10 +9000,39 @@
8976
9000
  opts.onEnd?.({ ...context });
8977
9001
  }
8978
9002
  }
8979
- function shouldBoost(el) {
8980
- if (el.closest('[g-no-boost]'))
8981
- return false;
8982
- return el.hasAttribute('g-boost') || hasGlobalBoost();
9003
+ function eligibleAnchorUrl(link) {
9004
+ if (!(link instanceof HTMLAnchorElement) || !link.getAttribute('href')
9005
+ || link.hasAttribute('download') || link.closest('[g-no-boost]'))
9006
+ return null;
9007
+ const target = link.getAttribute('target') ?? document.querySelector('base[target]')?.getAttribute('target');
9008
+ if (target && target.toLowerCase() !== '_self')
9009
+ return null;
9010
+ let url;
9011
+ try {
9012
+ url = new URL(link.href);
9013
+ }
9014
+ catch {
9015
+ return null;
9016
+ }
9017
+ if (!['http:', 'https:'].includes(url.protocol) || url.origin !== location.origin
9018
+ || url.username || url.password)
9019
+ return null;
9020
+ // URL.hash is empty for both an absent fragment and a trailing "#".
9021
+ if (url.href.includes('#') && canonicalUrl(url.href) === canonicalUrl(location.href))
9022
+ return null;
9023
+ // A page visit would need to remove the outgoing base before inserting
9024
+ // relative resources. Let the browser switch documents and enforce base-uri.
9025
+ if (document.querySelector('base[href]') && !link.hasAttribute('g-current-head')
9026
+ && !['append', 'prepend'].includes(link.getAttribute('g-swap')?.toLowerCase() || '')) {
9027
+ try {
9028
+ if (resolveTarget(link) === findGlobalOutlet())
9029
+ return null;
9030
+ }
9031
+ catch {
9032
+ return null;
9033
+ }
9034
+ }
9035
+ return url;
8983
9036
  }
8984
9037
  function onClickRouter(element) {
8985
9038
  log('[GyosRouter] g-router-link clicked');
@@ -9033,23 +9086,10 @@
9033
9086
  return;
9034
9087
  }
9035
9088
  const link = target.closest('a');
9036
- if (!link)
9037
- return;
9038
- const href = link.getAttribute('href');
9039
- if (!href)
9089
+ if (!link || !(link.hasAttribute('g-boost') || hasGlobalBoost()))
9040
9090
  return;
9041
- const url = new URL(href, window.location.href);
9042
- if (url.origin !== window.location.origin)
9043
- return;
9044
- if (url.hash && canonicalUrl(url.href) === canonicalUrl(window.location.href))
9045
- return;
9046
- if (link.closest('[g-no-boost]'))
9047
- return;
9048
- if (link.hasAttribute('download'))
9049
- return;
9050
- if (link.getAttribute('target') && link.getAttribute('target') !== '_self')
9051
- return;
9052
- if (!shouldBoost(link))
9091
+ const url = eligibleAnchorUrl(link);
9092
+ if (!url)
9053
9093
  return;
9054
9094
  event.preventDefault();
9055
9095
  // The default changeState is true, as this is commonly used for: navigation, etc.
@@ -9062,13 +9102,9 @@
9062
9102
  const link = target.closest('a[g-preload]');
9063
9103
  if (!link)
9064
9104
  return;
9065
- const href = link.getAttribute('href');
9066
- if (!href)
9067
- return;
9068
- const url = new URL(href, window.location.href);
9069
- if (url.origin !== window.location.origin)
9070
- return;
9071
- preload(url.toString());
9105
+ const url = eligibleAnchorUrl(link);
9106
+ if (url)
9107
+ preload(url.href);
9072
9108
  }
9073
9109
  function onSubmit(event) {
9074
9110
  if (event.defaultPrevented)
@@ -9221,7 +9257,7 @@
9221
9257
  portalCreate,
9222
9258
  portalDestroy,
9223
9259
  setCspNonce,
9224
- version: '0.5.6'
9260
+ version: '0.5.7'
9225
9261
  };
9226
9262
 
9227
9263
  // This file was generated. Do not modify manually!