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