pranx
Version:
The next of preact. A light-weight, Next.js-like fullstack metaframework
2 lines (1 loc) • 4.88 kB
JavaScript
var S=Object.defineProperty;var r=(e,o)=>S(e,"name",{value:o,configurable:!0});import{createContext as M}from"preact";import{useCallback as V,useContext as R,useState as A}from"preact/hooks";var w=r((e,o,t={})=>{e=e.split("/").filter(Boolean),o=(o||"").split("/").filter(Boolean),t.params||(t.params={});for(let p=0,n,c;p<Math.max(e.length,o.length);p++){let[,m,s,a]=(o[p]||"").match(/^(:?)(.*?)([+*?]?)$/);if(n=e[p],!(!m&&s===n)){if(!m&&n&&a==="*"){t.rest=`/${e.slice(p).map(decodeURIComponent).join("/")}`;break}if(!m||!n&&a!=="?"&&a!=="*")return;if(c=a==="+"||a==="*",c?n=e.slice(p).map(decodeURIComponent).join("/")||void 0:n&&(n=decodeURIComponent(n)),t.params[s]=n,s in t||(t[s]=n),c)break}}return t},"exec_route_match");var _=r(e=>{let o=null;for(let t of window.__PRANX_HYDRATE_DATA__.routes)if(w(e,t.path_parsed_for_routing)){o=t;break}return o},"find_route");import{useHead as U}from"unhead";import{createHead as D}from"unhead/client";var T=D(),x=null,y=r(e=>{let t=e?.css.map(p=>({href:p,rel:"stylesheet"}));x!==null?x.patch({link:t}):x=U(T,{link:t})},"onRouteChangeUpdateHead");import{jsx as F}from"preact/jsx-runtime";var C=M({_props:{},_props_status:"ready",set(){},onRouteWillChange:r(async()=>!1,"onRouteWillChange")}),k=r(e=>{let[o,t]=A(()=>_(window.location.pathname)?.props||null),[p,n]=A("ready"),c=V((s,a)=>{s==="props"&&t(a),s==="prop_status"&&n(a)},[]),m=r(async s=>{n("loading");let a=_(s);if(a===null)return console.error(`Route with value ${s} do not exits or not match`),n("error"),!1;if(a.rendering_kind==="static")return n("loading"),y(a),!0;try{let f=new URL(window.location.href);f.searchParams.set("props","only");let N=await(await fetch(f)).json();return t(N.props),y(a),n("ready"),!0}catch(f){return f instanceof Error&&(console.error("Failed to fetch props:",f),n("error")),!1}},"onRouteWillChange");return F(C.Provider,{value:{_props:o,_props_status:p,set:c,onRouteWillChange:m},children:e.children})},"AppContextProvider"),u=r(()=>{let e=R(C);if(!e)throw new Error("_useAppContext must be used within a AppContextProvider");return e},"_useAppContext"),W=r(()=>{let e=R(C);if(!e)throw new Error("useAppContext must be used within a AppContextProvider");return{props:e._props,props_status:e._props_status}},"publicUseAppContext");import{forwardRef as h}from"preact/compat";import{jsx as d}from"preact/jsx-runtime";var B=h((e,o)=>{if(!e.to)throw new Error("Link element must provide a `to` property");return typeof window>"u"||window.pranx.router_mode==="mpa"?d($,{ref:o,...e}):window.pranx.router_mode==="spa"?d(I,{ref:o,...e}):d(z,{})}),I=h((e,o)=>{let{set:t}=u();return d("a",{ref:o,...e,"data-to":e.to,href:e.to,onClick:()=>{t("prop_status","loading")},children:e.children})}),$=h((e,o)=>d("a",{ref:o,...e,"data-to":e.to,href:e.to,children:e.children})),z=h((e,o)=>d("a",{ref:o,...e}));import{hydrate as X}from"preact";function Y(e,o){document.addEventListener("DOMContentLoaded",()=>{X(e,o)})}r(Y,"mount");import{lazy as O}from"preact-iso";import{Suspense as g}from"preact/compat";import{jsx as l}from"preact/jsx-runtime";function v(){let e=_(window.location.pathname);if(e===null)throw new Error("Route not found");let o=O(()=>import(e.module)),t=e.props;if(e.rendering_kind==="server-side")return l(g,{fallback:!0,children:l(o,{...t})});if(e.static_generated_routes.length===0)return l(g,{fallback:!0,children:l(o,{...t})});for(let p of e.static_generated_routes)if(p.path===window.location.pathname){t=p.props;break}return l(g,{fallback:!0,children:l(o,{...t})})}r(v,"RouterMPA");import{ErrorBoundary as K,lazy as Q,LocationProvider as Z,Route as b,Router as j}from"preact-iso";import{jsx as q}from"preact/jsx-runtime";var E=r(()=>q("h1",{children:"Not Found"}),"NotFoundPage");import{cloneElement as G}from"preact";import{Children as J}from"preact/compat";var L=r(e=>{let o=J.only(e.children),{_props:t}=u();return G(o,t??{})},"ServerPage");import{jsx as i,jsxs as ee}from"preact/jsx-runtime";function H(){let{onRouteWillChange:e}=u();return i(Z,{children:i(K,{onError:console.error,children:ee(j,{onRouteChange:o=>e(o),children:[window.__PRANX_HYDRATE_DATA__.routes.map(o=>{let t=Q(()=>import(o.module));return i(b,{path:o.path_parsed_for_routing,component:()=>{let p=o.props;if(o.rendering_kind==="server-side")return i(L,{children:i(t,{})});if(o.static_generated_routes.length===0)return i(t,{...p});for(let n of o.static_generated_routes)if(n.path===window.location.pathname){p=n.props;break}return i(t,{...p})}},o.path)}),i(b,{default:!0,component:E})]})})})}r(H,"RouterSPA");import{jsx as P}from"preact/jsx-runtime";function oe(e){let{mode:o="spa",children:t}=e;if(typeof window>"u")return t;if(window.pranx={router_mode:o},o==="mpa")return P(v,{});if(o==="spa")return P(k,{children:P(H,{})});throw new Error("The mode should be 'spa' or 'mpa'")}r(oe,"PranxRouter");export{B as Link,oe as PranxRouter,Y as mount,W as useAppContext};