stx-router 0.2.340 → 0.2.342

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.
Files changed (2) hide show
  1. package/dist/client.js +51 -18
  2. package/package.json +1 -1
package/dist/client.js CHANGED
@@ -2039,26 +2039,58 @@ else {
2039
2039
  navigate(location.pathname+location.search+location.hash,false);
2040
2040
  });
2041
2041
 
2042
- // \u2500\u2500 Prefetch on hover \u2500\u2500
2042
+ // \u2500\u2500 Prefetch \u2500\u2500
2043
+ // A link's page, fetched before it is followed into the cache navigate
2044
+ // reads, so following it is a swap rather than a round trip.
2045
+ function prefetchLink(link){
2046
+ // Same opt-out set as the click path. Without it, hovering a link the
2047
+ // router is not allowed to claim still fired a real GET at it \u2014 a
2048
+ // logout or OAuth URL was requested on hover alone.
2049
+ if(isRouterExcluded(link))return;
2050
+ var href=withCurrentLocale(link.getAttribute('href'));
2051
+ var key=cacheKey(href);
2052
+ if(cache[key]||prefetching[key])return;
2053
+ prefetching[key]=true;
2054
+ var wantsFragment=shouldUseFragmentResponse();
2055
+ fetch(href,{headers:wantsFragment?{'X-STX-Router':'true','Accept':'text/html'}:{'Accept':'text/html'}}).then(function(r){
2056
+ return readPrefetchResponse(r,wantsFragment);
2057
+ }).then(function(result){
2058
+ if(result&&o.cache)setCache(key,result.html,result.layout,result.layoutGroup,result.title,result.containerAttrs);
2059
+ }).catch(function(){}).finally(function(){delete prefetching[key]});
2060
+ }
2061
+
2062
+ // Links marked data-stx-prefetch="eager", fetched once the page is idle, so
2063
+ // even their first tap is served from the cache: the way a phone's tab bar
2064
+ // switches instantly. Not on a connection that asked to save data.
2065
+ var eagerQueued=false;
2066
+ function prefetchEager(){
2067
+ if(!o.prefetch||!o.cache||eagerQueued)return;
2068
+ eagerQueued=true;
2069
+ var run=function(){
2070
+ eagerQueued=false;
2071
+ var connection=navigator.connection;
2072
+ if(connection&&(connection.saveData||/2g/.test(connection.effectiveType||'')))return;
2073
+ var here=cacheKey(location.pathname+location.search);
2074
+ var links=document.querySelectorAll('[data-stx-link][data-stx-prefetch="eager"]');
2075
+ for(var i=0;i<links.length;i++){
2076
+ var href=links[i].getAttribute('href');
2077
+ if(href&&cacheKey(withCurrentLocale(href))!==here)prefetchLink(links[i]);
2078
+ }
2079
+ };
2080
+ if(window.requestIdleCallback)window.requestIdleCallback(run,{timeout:2000});
2081
+ else setTimeout(run,300);
2082
+ }
2083
+
2043
2084
  if(o.prefetch){
2044
- document.addEventListener('mouseover',function(e){
2085
+ // A mouse hovers before it clicks. A finger does not hover, but it
2086
+ // touches down a tap's length before the click: start the fetch there.
2087
+ var onIntent=function(e){
2045
2088
  if(!e.target||!e.target.closest)return;
2046
- var link=e.target.closest('[data-stx-link]');
2047
- // Same opt-out set as the click path. Without it, hovering a link the
2048
- // router is not allowed to claim still fired a real GET at it \u2014 a
2049
- // logout or OAuth URL was requested on hover alone.
2050
- if(isRouterExcluded(link))return;
2051
- var href=withCurrentLocale(link.getAttribute('href'));
2052
- var key=cacheKey(href);
2053
- if(cache[key]||prefetching[key])return;
2054
- prefetching[key]=true;
2055
- var wantsFragment=shouldUseFragmentResponse();
2056
- fetch(href,{headers:wantsFragment?{'X-STX-Router':'true','Accept':'text/html'}:{'Accept':'text/html'}}).then(function(r){
2057
- return readPrefetchResponse(r,wantsFragment);
2058
- }).then(function(result){
2059
- if(result&&o.cache)setCache(key,result.html,result.layout,result.layoutGroup,result.title,result.containerAttrs);
2060
- }).catch(function(){}).finally(function(){delete prefetching[key]});
2061
- },true);
2089
+ prefetchLink(e.target.closest('[data-stx-link]'));
2090
+ };
2091
+ document.addEventListener('mouseover',onIntent,true);
2092
+ document.addEventListener('touchstart',onIntent,{capture:true,passive:true});
2093
+ window.addEventListener('stx:load',prefetchEager);
2062
2094
  }
2063
2095
 
2064
2096
  // \u2500\u2500 Active link management \u2500\u2500
@@ -2260,6 +2292,7 @@ else {
2260
2292
  injectStyles();
2261
2293
  injectViewTransitionCSS();
2262
2294
  refreshCurrentLinks();
2295
+ prefetchEager();
2263
2296
  }
2264
2297
 
2265
2298
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init);else init();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stx-router",
3
3
  "type": "module",
4
- "version": "0.2.340",
4
+ "version": "0.2.342",
5
5
  "description": "File-based router for STX — .stx template discovery, nested layouts, typed route params, middleware, and client-side SPA navigation.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",