jsimple-spa
Version:
**jsimple-spa** es una librería ligera que permite implementar un sistema de rutas en aplicaciones creadas con **Vanilla JavaScript**, ideal para construir **Single Page Applications (SPA)** sin necesidad de frameworks pesados.
3 lines (2 loc) • 1.72 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SimpleSPA={})}(this,function(t){"use strict";function e(t,e=""){if(!t)throw new Error("Assert Error",e)}let n=new Map;function o(t){e(t);t.querySelectorAll("link").forEach(t=>{!function(t){if(n.has(t)){const e=n.get(t);return void(e.getAttribute("href")!==t&&e.setAttribute("href",t))}const e=document.createElement("link");e.rel="stylesheet",e.href=t,document.head.appendChild(e),n.set(t,e)}(t.getAttribute("href")),t.remove()})}let r,i=[],a={path:"",page:"",activeGuard:void 0},s={path:"",page:"",activeGuard:void 0};async function c(){const t=window.location.pathname,e=d(t);e||!Boolean(f())?e.activeGuard&&!await e.activeGuard()||(console.log(s,e,t),s.path!==t&&(u(e.page),s=e)):u(f())}async function u(t){return e("string"==typeof t),e(r),async function(t){e("string"==typeof t);const n=await fetch(t);if(!n.ok)throw new Error("error to get HTML");return await n.text()}(t).catch(t=>{console.error("Error loading page:",t)}).then(t=>{r.innerHTML=t,o(r)})}function d(t){return t!==a.path&&(a=i.find(e=>e.path===t)),a}function f(){return d("*").page}document.addEventListener("DOMContentLoaded",()=>{r=document.querySelector("router-slot")}),function(){const t=history.pushState;history.pushState=function(){const e=t.apply(this,arguments),n=new Event("pushstate");return window.dispatchEvent(n),e},window.addEventListener("popstate",()=>{c()}),window.addEventListener("pushstate",()=>{c()})}(),t.navigate=function(t){history.pushState({},"",t)},t.router=function(t){i=t}});
//# sourceMappingURL=simple-js.umd.js.map