LiveReload in 100 Lines of C and JS

How we added dev server with live reload using pure C file watcher and 10 lines of JS.

LiveReload in 100 Lines of C and JS

Dev UX Matters

cax serve --watch should reload.

Implementation

C watcher:

time_t last = scan_dir("content");
while(1){
  sleep(1);
  if(scan_dir("content") != last) rebuild();
}

JS injection:

(function(){let h=null;setInterval(async()=>{
  let t=await fetch(location.href).then(r=>r.text());
  if(h===null) h=t.length; else if(h!=t.length) location.reload();
},1000)})();

No WebSocket. Just content-length compare. 100 lines total, works on Windows + Linux. Simple > fancy.