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.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
  */
@@ -8182,7 +8182,6 @@
8182
8182
  }
8183
8183
  const snapshots = new Map();
8184
8184
  const preloadCache = new Map();
8185
- const preloadControllers = new Map();
8186
8185
  const scrollPositions = new Map();
8187
8186
  const historyFragments = new Map();
8188
8187
  let routerStarted = false;
@@ -8502,6 +8501,15 @@
8502
8501
  }
8503
8502
  return { finalUrl: url.toString(), init };
8504
8503
  }
8504
+ function discardPreload(key) {
8505
+ const entry = preloadCache.get(key);
8506
+ preloadCache.delete(key);
8507
+ entry?.controller.abort();
8508
+ }
8509
+ function clearPreloads() {
8510
+ for (const key of preloadCache.keys())
8511
+ discardPreload(key);
8512
+ }
8505
8513
  function preload(url) {
8506
8514
  const cacheKey = canonicalUrl(url, true);
8507
8515
  if (preloadCache.has(cacheKey))
@@ -8516,11 +8524,11 @@
8516
8524
  signal: controller.signal,
8517
8525
  mode: 'same-origin'
8518
8526
  });
8519
- preloadCache.set(cacheKey, request);
8520
- preloadControllers.set(cacheKey, controller);
8527
+ const entry = { request, controller };
8528
+ preloadCache.set(cacheKey, entry);
8521
8529
  request.catch(() => {
8522
- preloadCache.delete(cacheKey);
8523
- preloadControllers.delete(cacheKey);
8530
+ if (preloadCache.get(cacheKey) === entry)
8531
+ preloadCache.delete(cacheKey);
8524
8532
  });
8525
8533
  }
8526
8534
  async function updateHead(fromDoc, signal, currentBaseUrl) {
@@ -8699,20 +8707,21 @@
8699
8707
  else {
8700
8708
  // Check preload cache if GET
8701
8709
  const preloadKey = canonicalUrl(finalUrl, true);
8702
- if (opts.fresh) {
8703
- preloadControllers.get(preloadKey)?.abort();
8704
- preloadControllers.delete(preloadKey);
8705
- preloadCache.delete(preloadKey);
8706
- }
8707
- if (!opts.fresh && opts.method.toUpperCase() === 'GET' && preloadCache.has(preloadKey)) {
8710
+ if (opts.fresh)
8711
+ discardPreload(preloadKey);
8712
+ const entry = init.method === 'GET' ? preloadCache.get(preloadKey) : undefined;
8713
+ if (entry) {
8708
8714
  log('[GyosRouter] using preload cache for', finalUrl);
8715
+ const signal = transaction.controller.signal;
8716
+ const discard = () => {
8717
+ if (preloadCache.get(preloadKey) === entry)
8718
+ discardPreload(preloadKey);
8719
+ };
8720
+ signal.addEventListener('abort', discard, { once: true });
8709
8721
  try {
8710
- response = await waitForNavigation(preloadCache.get(preloadKey), transaction.controller.signal);
8722
+ response = await waitForNavigation(entry.request, signal);
8711
8723
  }
8712
8724
  catch (error) {
8713
- preloadCache.delete(preloadKey);
8714
- preloadControllers.get(preloadKey)?.abort();
8715
- preloadControllers.delete(preloadKey);
8716
8725
  if (error.name === 'AbortError')
8717
8726
  throw error;
8718
8727
  assertActiveNavigation(transaction);
@@ -8720,8 +8729,11 @@
8720
8729
  fallback(finalUrl, true);
8721
8730
  return;
8722
8731
  }
8723
- preloadCache.delete(preloadKey); // consume cache
8724
- preloadControllers.delete(preloadKey);
8732
+ finally {
8733
+ signal.removeEventListener('abort', discard);
8734
+ if (preloadCache.get(preloadKey) === entry)
8735
+ preloadCache.delete(preloadKey);
8736
+ }
8725
8737
  }
8726
8738
  else {
8727
8739
  try {
@@ -8730,6 +8742,9 @@
8730
8742
  transaction.interval = setInterval(setRand, 300);
8731
8743
  response = await fetch(finalUrl, init);
8732
8744
  assertActiveNavigation(transaction);
8745
+ // A successful write can change HTML at any URL, including after a redirect.
8746
+ if (response.ok && init.method !== 'GET')
8747
+ clearPreloads();
8733
8748
  // Update progress to rand 70 - 80% after fetch
8734
8749
  progressBar?.setProgress(Math.random() * 10 + 70);
8735
8750
  if (transaction.interval)
@@ -8869,6 +8884,15 @@
8869
8884
  // The global outlet is also the incoming layout root. Keep its node for
8870
8885
  // inner swaps, but update attributes such as g-scope and layout classes.
8871
8886
  try {
8887
+ if (syncDocumentAttributes && !document.querySelector('base[href]')) {
8888
+ document.head.querySelectorAll('base[target]').forEach(base => base.remove());
8889
+ const baseTarget = doc.head.querySelector('base[target]')?.getAttribute('target');
8890
+ if (baseTarget != null) {
8891
+ const base = document.createElement('base');
8892
+ base.target = baseTarget;
8893
+ document.head.prepend(base);
8894
+ }
8895
+ }
8872
8896
  target = await performSwap(target, src, swapMode, {
8873
8897
  syncRootAttributes: isFullOutletNavigation,
8874
8898
  signal: transaction.controller.signal,
@@ -8994,10 +9018,39 @@
8994
9018
  opts.onEnd?.({ ...context });
8995
9019
  }
8996
9020
  }
8997
- function shouldBoost(el) {
8998
- if (el.closest('[g-no-boost]'))
8999
- return false;
9000
- return el.hasAttribute('g-boost') || hasGlobalBoost();
9021
+ function eligibleAnchorUrl(link) {
9022
+ if (!(link instanceof HTMLAnchorElement) || !link.getAttribute('href')
9023
+ || link.hasAttribute('download') || link.closest('[g-no-boost]'))
9024
+ return null;
9025
+ const target = link.getAttribute('target') ?? document.querySelector('base[target]')?.getAttribute('target');
9026
+ if (target && target.toLowerCase() !== '_self')
9027
+ return null;
9028
+ let url;
9029
+ try {
9030
+ url = new URL(link.href);
9031
+ }
9032
+ catch {
9033
+ return null;
9034
+ }
9035
+ if (!['http:', 'https:'].includes(url.protocol) || url.origin !== location.origin
9036
+ || url.username || url.password)
9037
+ return null;
9038
+ // URL.hash is empty for both an absent fragment and a trailing "#".
9039
+ if (url.href.includes('#') && canonicalUrl(url.href) === canonicalUrl(location.href))
9040
+ return null;
9041
+ // A page visit would need to remove the outgoing base before inserting
9042
+ // relative resources. Let the browser switch documents and enforce base-uri.
9043
+ if (document.querySelector('base[href]') && !link.hasAttribute('g-current-head')
9044
+ && !['append', 'prepend'].includes(link.getAttribute('g-swap')?.toLowerCase() || '')) {
9045
+ try {
9046
+ if (resolveTarget(link) === findGlobalOutlet())
9047
+ return null;
9048
+ }
9049
+ catch {
9050
+ return null;
9051
+ }
9052
+ }
9053
+ return url;
9001
9054
  }
9002
9055
  function onClickRouter(element) {
9003
9056
  log('[GyosRouter] g-router-link clicked');
@@ -9051,23 +9104,10 @@
9051
9104
  return;
9052
9105
  }
9053
9106
  const link = target.closest('a');
9054
- if (!link)
9055
- return;
9056
- const href = link.getAttribute('href');
9057
- if (!href)
9107
+ if (!link || !(link.hasAttribute('g-boost') || hasGlobalBoost()))
9058
9108
  return;
9059
- const url = new URL(href, window.location.href);
9060
- if (url.origin !== window.location.origin)
9061
- return;
9062
- if (url.hash && canonicalUrl(url.href) === canonicalUrl(window.location.href))
9063
- return;
9064
- if (link.closest('[g-no-boost]'))
9065
- return;
9066
- if (link.hasAttribute('download'))
9067
- return;
9068
- if (link.getAttribute('target') && link.getAttribute('target') !== '_self')
9069
- return;
9070
- if (!shouldBoost(link))
9109
+ const url = eligibleAnchorUrl(link);
9110
+ if (!url)
9071
9111
  return;
9072
9112
  event.preventDefault();
9073
9113
  // The default changeState is true, as this is commonly used for: navigation, etc.
@@ -9080,13 +9120,9 @@
9080
9120
  const link = target.closest('a[g-preload]');
9081
9121
  if (!link)
9082
9122
  return;
9083
- const href = link.getAttribute('href');
9084
- if (!href)
9085
- return;
9086
- const url = new URL(href, window.location.href);
9087
- if (url.origin !== window.location.origin)
9088
- return;
9089
- preload(url.toString());
9123
+ const url = eligibleAnchorUrl(link);
9124
+ if (url)
9125
+ preload(url.href);
9090
9126
  }
9091
9127
  function onSubmit(event) {
9092
9128
  if (event.defaultPrevented)
@@ -9239,7 +9275,7 @@
9239
9275
  portalCreate,
9240
9276
  portalDestroy,
9241
9277
  setCspNonce,
9242
- version: '0.5.6'
9278
+ version: '0.5.7'
9243
9279
  };
9244
9280
 
9245
9281
  setExpressionRuntime(standardExpressionRuntime);