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.
package/dist/gyos.csp.js CHANGED
@@ -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
  */
@@ -14799,7 +14799,6 @@
14799
14799
  }
14800
14800
  const snapshots = new Map();
14801
14801
  const preloadCache = new Map();
14802
- const preloadControllers = new Map();
14803
14802
  const scrollPositions = new Map();
14804
14803
  const historyFragments = new Map();
14805
14804
  let routerStarted = false;
@@ -15119,6 +15118,15 @@
15119
15118
  }
15120
15119
  return { finalUrl: url.toString(), init };
15121
15120
  }
15121
+ function discardPreload(key) {
15122
+ const entry = preloadCache.get(key);
15123
+ preloadCache.delete(key);
15124
+ entry?.controller.abort();
15125
+ }
15126
+ function clearPreloads() {
15127
+ for (const key of preloadCache.keys())
15128
+ discardPreload(key);
15129
+ }
15122
15130
  function preload(url) {
15123
15131
  const cacheKey = canonicalUrl(url, true);
15124
15132
  if (preloadCache.has(cacheKey))
@@ -15133,11 +15141,11 @@
15133
15141
  signal: controller.signal,
15134
15142
  mode: 'same-origin'
15135
15143
  });
15136
- preloadCache.set(cacheKey, request);
15137
- preloadControllers.set(cacheKey, controller);
15144
+ const entry = { request, controller };
15145
+ preloadCache.set(cacheKey, entry);
15138
15146
  request.catch(() => {
15139
- preloadCache.delete(cacheKey);
15140
- preloadControllers.delete(cacheKey);
15147
+ if (preloadCache.get(cacheKey) === entry)
15148
+ preloadCache.delete(cacheKey);
15141
15149
  });
15142
15150
  }
15143
15151
  async function updateHead(fromDoc, signal, currentBaseUrl) {
@@ -15316,20 +15324,21 @@
15316
15324
  else {
15317
15325
  // Check preload cache if GET
15318
15326
  const preloadKey = canonicalUrl(finalUrl, true);
15319
- if (opts.fresh) {
15320
- preloadControllers.get(preloadKey)?.abort();
15321
- preloadControllers.delete(preloadKey);
15322
- preloadCache.delete(preloadKey);
15323
- }
15324
- if (!opts.fresh && opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
15327
+ if (opts.fresh)
15328
+ discardPreload(preloadKey);
15329
+ const entry = init.method === 'GET' ? preloadCache.get(preloadKey) : undefined;
15330
+ if (entry) {
15325
15331
  log('[GyosRouter] using preload cache for', finalUrl);
15332
+ const signal = transaction.controller.signal;
15333
+ const discard = () => {
15334
+ if (preloadCache.get(preloadKey) === entry)
15335
+ discardPreload(preloadKey);
15336
+ };
15337
+ signal.addEventListener('abort', discard, { once: true });
15326
15338
  try {
15327
- response = await waitForNavigation(preloadCache.get(preloadKey), transaction.controller.signal);
15339
+ response = await waitForNavigation(entry.request, signal);
15328
15340
  }
15329
15341
  catch (error) {
15330
- preloadCache.delete(preloadKey);
15331
- preloadControllers.get(preloadKey)?.abort();
15332
- preloadControllers.delete(preloadKey);
15333
15342
  if (error.name === 'AbortError')
15334
15343
  throw error;
15335
15344
  assertActiveNavigation(transaction);
@@ -15337,8 +15346,11 @@
15337
15346
  fallback(finalUrl, true);
15338
15347
  return;
15339
15348
  }
15340
- preloadCache.delete(preloadKey); // consume cache
15341
- preloadControllers.delete(preloadKey);
15349
+ finally {
15350
+ signal.removeEventListener('abort', discard);
15351
+ if (preloadCache.get(preloadKey) === entry)
15352
+ preloadCache.delete(preloadKey);
15353
+ }
15342
15354
  }
15343
15355
  else {
15344
15356
  try {
@@ -15347,6 +15359,9 @@
15347
15359
  transaction.interval = setInterval(setRand, 300);
15348
15360
  response = await fetch(finalUrl, init);
15349
15361
  assertActiveNavigation(transaction);
15362
+ // A successful write can change HTML at any URL, including after a redirect.
15363
+ if (response.ok && init.method !== 'GET')
15364
+ clearPreloads();
15350
15365
  // Update progress to rand 70 - 80% after fetch
15351
15366
  progressBar?.setProgress(Math.random() * 10 + 70);
15352
15367
  if (transaction.interval)
@@ -15486,6 +15501,15 @@
15486
15501
  // The global outlet is also the incoming layout root. Keep its node for
15487
15502
  // inner swaps, but update attributes such as g-scope and layout classes.
15488
15503
  try {
15504
+ if (syncDocumentAttributes && !document.querySelector('base[href]')) {
15505
+ document.head.querySelectorAll('base[target]').forEach(base => base.remove());
15506
+ const baseTarget = doc.head.querySelector('base[target]')?.getAttribute('target');
15507
+ if (baseTarget != null) {
15508
+ const base = document.createElement('base');
15509
+ base.target = baseTarget;
15510
+ document.head.prepend(base);
15511
+ }
15512
+ }
15489
15513
  target = await performSwap(target, src, swapMode, {
15490
15514
  syncRootAttributes: isFullOutletNavigation,
15491
15515
  signal: transaction.controller.signal,
@@ -15611,10 +15635,39 @@
15611
15635
  opts.onEnd?.({ ...context });
15612
15636
  }
15613
15637
  }
15614
- function shouldBoost(el) {
15615
- if (el.closest('[g-no-boost]'))
15616
- return false;
15617
- return el.hasAttribute('g-boost') || hasGlobalBoost();
15638
+ function eligibleAnchorUrl(link) {
15639
+ if (!(link instanceof HTMLAnchorElement) || !link.getAttribute('href')
15640
+ || link.hasAttribute('download') || link.closest('[g-no-boost]'))
15641
+ return null;
15642
+ const target = link.getAttribute('target') ?? document.querySelector('base[target]')?.getAttribute('target');
15643
+ if (target && target.toLowerCase() !== '_self')
15644
+ return null;
15645
+ let url;
15646
+ try {
15647
+ url = new URL(link.href);
15648
+ }
15649
+ catch {
15650
+ return null;
15651
+ }
15652
+ if (!['http:', 'https:'].includes(url.protocol) || url.origin !== location.origin
15653
+ || url.username || url.password)
15654
+ return null;
15655
+ // URL.hash is empty for both an absent fragment and a trailing "#".
15656
+ if (url.href.includes('#') && canonicalUrl(url.href) === canonicalUrl(location.href))
15657
+ return null;
15658
+ // A page visit would need to remove the outgoing base before inserting
15659
+ // relative resources. Let the browser switch documents and enforce base-uri.
15660
+ if (document.querySelector('base[href]') && !link.hasAttribute('g-current-head')
15661
+ && !['append', 'prepend'].includes(link.getAttribute('g-swap')?.toLowerCase() || '')) {
15662
+ try {
15663
+ if (resolveTarget(link) === findGlobalOutlet())
15664
+ return null;
15665
+ }
15666
+ catch {
15667
+ return null;
15668
+ }
15669
+ }
15670
+ return url;
15618
15671
  }
15619
15672
  function onClickRouter(element) {
15620
15673
  log('[GyosRouter] g-router-link clicked');
@@ -15668,23 +15721,10 @@
15668
15721
  return;
15669
15722
  }
15670
15723
  const link = target.closest('a');
15671
- if (!link)
15672
- return;
15673
- const href = link.getAttribute('href');
15674
- if (!href)
15724
+ if (!link || !(link.hasAttribute('g-boost') || hasGlobalBoost()))
15675
15725
  return;
15676
- const url = new URL(href, window.location.href);
15677
- if (url.origin !== window.location.origin)
15678
- return;
15679
- if (url.hash && canonicalUrl(url.href) === canonicalUrl(window.location.href))
15680
- return;
15681
- if (link.closest('[g-no-boost]'))
15682
- return;
15683
- if (link.hasAttribute('download'))
15684
- return;
15685
- if (link.getAttribute('target') && link.getAttribute('target') !== '_self')
15686
- return;
15687
- if (!shouldBoost(link))
15726
+ const url = eligibleAnchorUrl(link);
15727
+ if (!url)
15688
15728
  return;
15689
15729
  event.preventDefault();
15690
15730
  // The default changeState is true, as this is commonly used for: navigation, etc.
@@ -15697,13 +15737,9 @@
15697
15737
  const link = target.closest('a[g-preload]');
15698
15738
  if (!link)
15699
15739
  return;
15700
- const href = link.getAttribute('href');
15701
- if (!href)
15702
- return;
15703
- const url = new URL(href, window.location.href);
15704
- if (url.origin !== window.location.origin)
15705
- return;
15706
- preload(url.toString());
15740
+ const url = eligibleAnchorUrl(link);
15741
+ if (url)
15742
+ preload(url.href);
15707
15743
  }
15708
15744
  function onSubmit(event) {
15709
15745
  if (event.defaultPrevented)
@@ -15856,7 +15892,7 @@
15856
15892
  portalCreate,
15857
15893
  portalDestroy,
15858
15894
  setCspNonce,
15859
- version: '0.5.6'
15895
+ version: '0.5.7'
15860
15896
  };
15861
15897
 
15862
15898
  setExpressionRuntime(cspExpressionRuntime);