bun-plugin-stx 0.2.324 → 0.2.326

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.
@@ -1699,8 +1699,7 @@ else {
1699
1699
  // Remove old page scripts
1700
1700
  document.querySelectorAll('script[data-stx-page]').forEach(function(s){s.remove()});
1701
1701
  if(pushState!==false)writeHistory(pushState,url+(hash||''));
1702
- updateNav();
1703
- updateActiveLinks();
1702
+ refreshCurrentLinks();
1704
1703
  if(o.scrollToTop&&!hash)window.scrollTo({top:0,behavior:'instant'});
1705
1704
  else if(hash){var el=document.querySelector(hash);if(el)el.scrollIntoView({behavior:'smooth'})}
1706
1705
  window.dispatchEvent(new CustomEvent('stx:navigate',{detail:{url:url}}));
@@ -2150,8 +2149,7 @@ else {
2150
2149
  if(pushState!==false)writeHistory(pushState,url+(hash||''));
2151
2150
 
2152
2151
  // Update active nav links
2153
- updateNav();
2154
- updateActiveLinks();
2152
+ refreshCurrentLinks();
2155
2153
 
2156
2154
  // Scroll
2157
2155
  if(o.scrollToTop&&!hash)window.scrollTo({top:0,behavior:'instant'});
@@ -2559,7 +2557,14 @@ else {
2559
2557
  // Null for links the router does not own (another origin, a bare #fragment,
2560
2558
  // anything unparseable), which are left exactly as the server rendered them.
2561
2559
  function normPath(p){return p.length>1&&p.charAt(p.length-1)==='/'?p.slice(0,-1):p}
2562
- function linkState(href){
2560
+ // Paths beyond the link's own that make it current: a tab bar's Calendar
2561
+ // tab stays lit on a workout opened from it (data-stx-active-match, a
2562
+ // space-separated list of path prefixes).
2563
+ function matchesAny(list,cur){
2564
+ if(!list)return false;
2565
+ return list.split(/\\s+/).some(function(p){p=normPath(p);return p&&(cur===p||cur.indexOf(p+'/')===0)});
2566
+ }
2567
+ function linkState(href,also){
2563
2568
  if(!href||href.charAt(0)==='#')return null;
2564
2569
  var u;
2565
2570
  try{u=new URL(href,location.href)}catch(e){return null}
@@ -2571,27 +2576,39 @@ else {
2571
2576
  // point at this path, exactly one is.
2572
2577
  var have=new URLSearchParams(location.search),q=true;
2573
2578
  u.searchParams.forEach(function(v,k){if(have.getAll(k).indexOf(v)===-1)q=false});
2579
+ var matched=matchesAny(also,cur);
2574
2580
  return {
2575
2581
  exact:q&&path===cur,
2576
- active:q&&(path==='/'?cur==='/':(cur===path||cur.indexOf(path+'/')===0))
2582
+ active:q&&(path==='/'?cur==='/':(cur===path||cur.indexOf(path+'/')===0))||matched,
2583
+ matched:matched
2577
2584
  };
2578
2585
  }
2579
2586
 
2580
2587
  function updateNav(){
2581
2588
  document.querySelectorAll('nav a[href], #mobileNav a[href], [data-stx-nav] a[href]').forEach(function(a){
2582
2589
  if(!a.hasAttribute('data-stx-link'))return;
2583
- var st=linkState(a.getAttribute('href'));
2590
+ var st=linkState(a.getAttribute('href'),a.getAttribute('data-stx-active-match'));
2584
2591
  if(!st)return;
2585
2592
  var ac=a.getAttribute('data-stx-active-class')||'active';
2586
- if(st.exact)ac.split(' ').forEach(function(cls){if(cls)a.classList.add(cls)});else ac.split(' ').forEach(function(cls){if(cls)a.classList.remove(cls)});
2593
+ if(st.exact||st.matched)ac.split(' ').forEach(function(cls){if(cls)a.classList.add(cls)});else ac.split(' ').forEach(function(cls){if(cls)a.classList.remove(cls)});
2587
2594
  });
2588
2595
  }
2589
2596
 
2597
+ // Both passes, always in this order. A link inside a <nav> is current only on
2598
+ // its own page (or one it names in data-stx-active-match), so updateNav has
2599
+ // the last word. Navigation used to run them the other way round, which lit
2600
+ // a nav's parent links by prefix after an in-app navigation but not after a
2601
+ // full load of the same URL.
2602
+ function refreshCurrentLinks(){
2603
+ updateActiveLinks();
2604
+ updateNav();
2605
+ }
2606
+
2590
2607
  function updateActiveLinks(){
2591
2608
  // Update active classes on <stx-link> elements (and legacy data-stx-link)
2592
2609
  var links=document.querySelectorAll('[data-stx-link]');
2593
2610
  links.forEach(function(link){
2594
- var st=linkState(link.getAttribute('to')||link.getAttribute('href')||'');
2611
+ var st=linkState(link.getAttribute('to')||link.getAttribute('href')||'',link.getAttribute('data-stx-active-match'));
2595
2612
  if(!st)return;
2596
2613
  var ac=link.getAttribute('active-class')||link.getAttribute('data-stx-active-class')||'active';
2597
2614
  var eac=link.getAttribute('exact-active-class')||link.getAttribute('data-stx-exact-active-class')||'exact-active';
@@ -2722,8 +2739,7 @@ else {
2722
2739
  function init(){
2723
2740
  injectStyles();
2724
2741
  injectViewTransitionCSS();
2725
- updateActiveLinks();
2726
- updateNav();
2742
+ refreshCurrentLinks();
2727
2743
  }
2728
2744
 
2729
2745
  if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',init);else init();
package/dist/serve.js CHANGED
@@ -9328,8 +9328,11 @@ After installing: bun add bun-plugin-stx`);
9328
9328
  const cssCssCache = boundedCache(options.cssCacheLimit ?? 512);
9329
9329
  const hmrClients = new Set;
9330
9330
  const hmrEncoder = new TextEncoder;
9331
+ let hmrVersion = 0;
9331
9332
  function broadcastHmr(event) {
9332
- const payload = hmrEncoder.encode(`data: ${JSON.stringify(event)}
9333
+ if (event.type !== "build-error")
9334
+ hmrVersion++;
9335
+ const payload = hmrEncoder.encode(`data: ${JSON.stringify({ ...event, version: hmrVersion })}
9333
9336
 
9334
9337
  `);
9335
9338
  for (const c of hmrClients) {
@@ -9375,16 +9378,17 @@ function __stxOverlay(errs){
9375
9378
  el.innerHTML=h;
9376
9379
  }
9377
9380
  `;
9378
- const HMR_CLIENT_SCRIPT = `<script data-stx-hmr>(()=>{if(window.__stxHmr)return;window.__stxHmr=1;${HMR_OVERLAY_JS}function bust(){var ls=document.querySelectorAll('link[rel="stylesheet"]');for(var i=0;i<ls.length;i++){var l=ls[i];var u=new URL(l.href,location.href);u.searchParams.set('v',Date.now().toString(36));l.href=u.toString()}}var es=new EventSource('/_stx/hmr');function swapFragment(){var r=window.stxRouter;if(r&&typeof r.refresh==='function'){try{return r.refresh().then(function(ok){if(!ok)location.reload()},function(){location.reload()})}catch(_){location.reload()}}else{location.reload()}}function reloadStores(){var s=window.stx;if(!s||typeof s.__hmrReplaceStores!=='function'){location.reload();return}fetch('/_stx/stores.js',{cache:'no-store'}).then(function(r){return r.ok?r.text():null}).then(function(code){if(code===null){location.reload();return}s.__hmrReplaceStores(code)}).catch(function(){location.reload()})}es.onmessage=function(e){try{var m=JSON.parse(e.data);if(m.type==='reload')location.reload();else if(m.type==='css')bust();else if(m.type==='fragment')swapFragment();else if(m.type==='store')reloadStores();else if(m.type==='build-error')__stxOverlay(m.errors)}catch(_){}};es.onerror=function(){if(es.readyState===2){setTimeout(function(){location.reload()},400)}}})()</script>`;
9381
+ const HMR_CLIENT_SCRIPT = `<script data-stx-hmr>(()=>{if(window.__stxHmr)return;window.__stxHmr=1;${HMR_OVERLAY_JS}function bust(){var ls=document.querySelectorAll('link[rel="stylesheet"]');for(var i=0;i<ls.length;i++){var l=ls[i];var u=new URL(l.href,location.href);u.searchParams.set('v',Date.now().toString(36));l.href=u.toString()}}var version=__STX_HMR_VERSION__,es=null;function swapFragment(){var r=window.stxRouter;if(r&&typeof r.refresh==='function'){try{return r.refresh().then(function(ok){if(!ok)location.reload()},function(){location.reload()})}catch(_){location.reload()}}else{location.reload()}}function reloadStores(){var s=window.stx;if(!s||typeof s.__hmrReplaceStores!=='function'){location.reload();return}fetch('/_stx/stores.js',{cache:'no-store'}).then(function(r){return r.ok?r.text():null}).then(function(code){if(code===null){location.reload();return}s.__hmrReplaceStores(code)}).catch(function(){location.reload()})}function connect(){var source=new EventSource('/_stx/hmr');es=source;source.onmessage=function(e){try{var m=JSON.parse(e.data);if(m.type==='connected'){if(typeof m.version==='number'&&m.version!==version)location.reload();return}if(typeof m.version==='number')version=m.version;if(m.type==='reload')location.reload();else if(m.type==='css')bust();else if(m.type==='fragment')swapFragment();else if(m.type==='store')reloadStores();else if(m.type==='build-error')__stxOverlay(m.errors)}catch(_){}};source.onerror=function(){if(es===source&&source.readyState===2){setTimeout(function(){location.reload()},400)}}}connect();addEventListener('pagehide',function(){if(es){es.close();es=null}});addEventListener('pageshow',function(e){if(e.persisted&&!es)connect()})})()</script>`;
9379
9382
  function injectHmrClient(html) {
9380
9383
  if (!html || production)
9381
9384
  return html;
9382
9385
  refreshBuildErrors();
9386
+ const client = HMR_CLIENT_SCRIPT.replace("__STX_HMR_VERSION__", String(hmrVersion));
9383
9387
  const closeBody = html.lastIndexOf("</body>");
9384
9388
  if (closeBody === -1) {
9385
- return html + HMR_CLIENT_SCRIPT;
9389
+ return html + client;
9386
9390
  }
9387
- return html.slice(0, closeBody) + HMR_CLIENT_SCRIPT + html.slice(closeBody);
9391
+ return html.slice(0, closeBody) + client + html.slice(closeBody);
9388
9392
  }
9389
9393
  let currentBuildErrors = [];
9390
9394
  async function refreshBuildErrors() {
@@ -9787,7 +9791,7 @@ function __stxOverlay(errs){
9787
9791
  rebuildDiscoveredPagePaths(kept);
9788
9792
  await indexPageMiddleware(kept);
9789
9793
  try {
9790
- const { Router } = await import("./chunk-37vp8yms.js");
9794
+ const { Router } = await import("./chunk-y6mw5qdq.js");
9791
9795
  const pagesDirs = patterns.map((p) => p.replace(/\/$/, "")).filter(Boolean);
9792
9796
  const router = new Router(process19.cwd(), { pagesDirs, stateDir: stateDirName(), exclude: options.exclude });
9793
9797
  if (!options.quiet)
@@ -10531,7 +10535,7 @@ ${block}`;
10531
10535
  start(c) {
10532
10536
  controller = c;
10533
10537
  hmrClients.add(c);
10534
- c.enqueue(hmrEncoder.encode(`data: {"type":"connected"}
10538
+ c.enqueue(hmrEncoder.encode(`data: {"type":"connected","version":${hmrVersion}}
10535
10539
 
10536
10540
  `));
10537
10541
  if (currentBuildErrors.length > 0) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.324",
4
+ "version": "0.2.326",
5
5
  "description": "A Bun plugin that allows for using Laravel Blade-like syntax.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",