stx-router 0.2.309 → 0.2.311

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 +30 -8
  2. package/package.json +1 -1
package/dist/client.js CHANGED
@@ -832,8 +832,10 @@ else {
832
832
  // Before the swap: add each sheet the page does not have yet, as
833
833
  // media="print" so it downloads without restyling the page still on screen.
834
834
  // Returns a promise for them to finish (load OR error, capped at
835
- // cssLoadTimeout so a slow or missing sheet never stalls navigation), or null
836
- // when nothing new is needed and the swap can go ahead in the same turn.
835
+ // cssLoadTimeout so a slow sheet never stalls navigation), or null when
836
+ // nothing new is needed and the swap can go ahead in the same turn. The
837
+ // promise resolves false when a sheet failed outright, and the caller then
838
+ // navigates in full rather than swap in markup that has no styles.
837
839
  function preloadGeneratedCss(hrefs,base){
838
840
  var pending=[];
839
841
  hrefs.forEach(function(href){
@@ -854,14 +856,34 @@ else {
854
856
  link.setAttribute('data-stx-css-pending','');
855
857
  link.__stxCssReady=new Promise(function(resolve){
856
858
  var timer=setTimeout(finish,o.cssLoadTimeout);
857
- function finish(){clearTimeout(timer);link.onload=null;link.onerror=null;link.__stxCssReady=null;resolve()}
858
- link.onload=finish;
859
- link.onerror=finish;
859
+ function finish(failed){
860
+ clearTimeout(timer);link.onload=null;link.onerror=null;link.__stxCssReady=null;
861
+ // A sheet that failed (a 5xx, a dropped connection) is gone for good:
862
+ // swapping anyway painted the page with only the utilities the
863
+ // previous page happened to share, which reads as a broken layout.
864
+ // Drop the dead <link> so a retry fetches it again, and report it.
865
+ if(failed===true&&link.parentNode)link.parentNode.removeChild(link);
866
+ resolve(failed!==true);
867
+ }
868
+ link.onload=function(){finish(false)};
869
+ link.onerror=function(){finish(true)};
860
870
  });
861
871
  pending.push(link.__stxCssReady);
862
872
  document.head.appendChild(link);
863
873
  });
864
- return pending.length?Promise.all(pending):null;
874
+ return pending.length?Promise.all(pending).then(function(results){return results.every(function(ok){return ok!==false})}):null;
875
+ }
876
+
877
+ // Swap once the destination's sheets are in, or hand the navigation to the
878
+ // browser when one could not be fetched \u2014 a full load retries the sheet.
879
+ function afterCss(ready,url,swapNow){
880
+ if(!ready)return swapNow();
881
+ return ready.then(function(ok){
882
+ if(ok)return swapNow();
883
+ log('[router] stylesheet failed to load \u2014 full navigation to:',url);
884
+ location.href=url;
885
+ return false;
886
+ });
865
887
  }
866
888
 
867
889
  // At the swap: make the destination's sheets live and move them to the end
@@ -1136,7 +1158,7 @@ else {
1136
1158
  // jumps. Cleanup waits with it so the outgoing page stays live meanwhile.
1137
1159
  // With nothing new to load this runs in the same turn, as it always did.
1138
1160
  var fragCssReady=preloadGeneratedCss(fragmentCssHrefs(html),location.href);
1139
- return fragCssReady?fragCssReady.then(startFragSwap):startFragSwap();
1161
+ return afterCss(fragCssReady,url,startFragSwap);
1140
1162
  }
1141
1163
 
1142
1164
  // Full document mode: parse with DOMParser and extract container content
@@ -1578,7 +1600,7 @@ else {
1578
1600
  });
1579
1601
  }
1580
1602
  var docCssReady=preloadGeneratedCss(docCssHrefs,docCssBase);
1581
- return docCssReady?docCssReady.then(startSwap):startSwap();
1603
+ return afterCss(docCssReady,url,startSwap);
1582
1604
  }
1583
1605
 
1584
1606
  // \u2500\u2500 Link interception \u2500\u2500
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stx-router",
3
3
  "type": "module",
4
- "version": "0.2.309",
4
+ "version": "0.2.311",
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",