Compare commits
No commits in common. "63137a7726c4b00a6be5666dff64e6340f00e963" and "26cb46923fead330d205950a7b3ac06707c159e7" have entirely different histories.
63137a7726
...
26cb46923f
@ -1,19 +1,16 @@
|
||||
import TWPreTail from "https://esm.sh/v115/@twind/preset-tailwind@1.1.4/es2022/preset-tailwind.mjs";
|
||||
import TWPreAuto from "https://esm.sh/v115/@twind/preset-autoprefix@1.0.7/es2022/preset-autoprefix.mjs";
|
||||
import React from "react";
|
||||
import Component from "./deep/component.tsx";
|
||||
import * as Iso from "@eno/iso";
|
||||
|
||||
const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
||||
|
||||
export default ()=>
|
||||
{
|
||||
return <div class="p-4 font-sans">
|
||||
<Iso.Metas title="Main Page!"/>
|
||||
<h1 class="my-2 font(bold serif) text(2xl)">Title!!</h1>
|
||||
<h2>suspended:</h2>
|
||||
<React.Suspense fallback={<div>Loading!</div>}>
|
||||
<Comp/>
|
||||
</React.Suspense>
|
||||
<h2>subtitle</h2>
|
||||
<Component/>
|
||||
<Iso.Switch>
|
||||
<Iso.Case value="page">
|
||||
<Iso.Switch>
|
||||
|
@ -7,15 +7,13 @@ export default ()=>
|
||||
const [routeGet, routeSet] = Iso.Router.Consumer();
|
||||
|
||||
type CatFact = {fact:string, length:number}|undefined;
|
||||
const [Data, Updating] = Iso.Fetch.Use(`https://catfact.ninja/fact`, undefined, 60, true, true, true);
|
||||
|
||||
|
||||
console.log("render!!")
|
||||
const [Data, Updating] = Iso.Fetch.Use(`https://catfact.ninja/fact`);
|
||||
|
||||
return <div class="p-4 text-red-500">
|
||||
<Iso.Metas title="Component!"/>
|
||||
Component Route is: {routeGet.Path.toString()}
|
||||
Route is: {routeGet.Path.toString()}
|
||||
<button className="p-4 bg-red-500 text-white" onClick={e=>{countSet(countGet+1); routeSet(["lol", "idk"], {count:countGet+1});}}>{countGet}</button>
|
||||
Component!!!
|
||||
<a href="/page/about-us" className="p-2 text(lg blue-500) font-bold">a link</a>
|
||||
<p>Data:{Data && (Data as CatFact)?.fact}</p>
|
||||
<p>Status:{Updating?'loading':'done'}</p>
|
||||
|
63
lib/iso.tsx
63
lib/iso.tsx
@ -160,22 +160,19 @@ export const Switch =({children}:{children:Children})=>
|
||||
export const Case =({children, value}:{children:Children, value?:string, default?:true})=>null;
|
||||
export const useRouteVars =()=> React.useContext(SwitchContext).keys;
|
||||
|
||||
export type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, CacheOnServer:boolean, Promise?:Promise<FetchRecord>, DelaySSR:boolean, Seed:boolean, Error?:string, JSON?:object};
|
||||
type FetchGuide = [Record:FetchRecord, Init:boolean, Listen:boolean];
|
||||
export type FetchHookState = [Data:undefined|object, Updating:boolean];
|
||||
export type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, CacheOnServer:boolean, Promise?:Promise<FetchRecord>, DelaySSR:boolean, Error?:string, JSON?:object};
|
||||
export const Fetch = {
|
||||
Cache:new Map() as Map<string, FetchRecord>,
|
||||
ServerBlocking:false as false|Promise<FetchRecord>[],
|
||||
ServerTouched:false as false|Set<FetchRecord>,
|
||||
ServerRemove:false as false|Set<FetchRecord>,
|
||||
ServerTouched:new Set() as Set<FetchRecord>,
|
||||
Seed(seed:FetchRecord[])
|
||||
{
|
||||
seed.forEach(r=>{
|
||||
//r.Promise = Promise.resolve(r);
|
||||
r.Promise = Promise.resolve(r);
|
||||
Fetch.Cache.set(r.URL, r)
|
||||
});
|
||||
},
|
||||
Request(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true, Seed:boolean = true):FetchGuide
|
||||
Request(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true)
|
||||
{
|
||||
let check = Fetch.Cache.get(URL);
|
||||
|
||||
@ -190,24 +187,17 @@ export const Fetch = {
|
||||
return inCheck;
|
||||
});
|
||||
return inCheck;
|
||||
};
|
||||
}
|
||||
|
||||
if(!check)
|
||||
{
|
||||
// not in the cache
|
||||
// - listen
|
||||
|
||||
console.log(`making new cache record...`);
|
||||
return [load({URL, CacheFor, CachedAt:0, CacheOnServer, DelaySSR, Seed}), false, true];
|
||||
return load({URL, CacheFor, CachedAt:0, CacheOnServer, DelaySSR});
|
||||
}
|
||||
else if(check.CachedAt == 0)
|
||||
{
|
||||
// loading started but not finished
|
||||
// - listen
|
||||
// - possibly init if there is something in JSON
|
||||
|
||||
console.log(`currently being cached...`);
|
||||
return [check, check.JSON ? true : false, true];
|
||||
return check;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -215,49 +205,38 @@ export const Fetch = {
|
||||
let secondsAge = (new Date().getTime() - check.CachedAt)/1000;
|
||||
if(secondsAge > check.CacheFor)
|
||||
{
|
||||
// cached but expired
|
||||
// - listen
|
||||
// - init
|
||||
console.log(`...outdated...`);
|
||||
return [load(check), true, true];
|
||||
return load(check);
|
||||
}
|
||||
else
|
||||
{
|
||||
// cached and ready
|
||||
// - init
|
||||
console.log(`...retrieved!`);
|
||||
return [check, true, false];
|
||||
return check;
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
Use(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true, Seed:boolean = true)
|
||||
Use(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true)
|
||||
{
|
||||
const [receipt, init, listen] = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR, Seed);
|
||||
const initialState:FetchHookState = init ? [receipt.JSON, listen] : [undefined, true];
|
||||
const [cacheGet, cacheSet] = React.useState(initialState);
|
||||
|
||||
if(Fetch.ServerBlocking && Fetch.ServerTouched && DelaySSR) // if server-side rendering
|
||||
type FetchHookState = [Data:undefined|object, Updating:boolean];
|
||||
const [cacheGet, cacheSet] = React.useState([undefined, true] as FetchHookState);
|
||||
if(Fetch.ServerBlocking && DelaySSR)
|
||||
{
|
||||
if(listen) // if the request is pending
|
||||
const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR);
|
||||
if(receipt.CachedAt === 0 && receipt.Promise)
|
||||
{
|
||||
receipt.Promise && Fetch.ServerBlocking.push(receipt.Promise); // add promise to blocking list
|
||||
return [undefined, listen] as FetchHookState; // no need to return any actual data while waiting server-side
|
||||
Fetch.ServerBlocking.push(receipt.Promise);
|
||||
}
|
||||
else // if request is ready
|
||||
else
|
||||
{
|
||||
receipt.Seed && Fetch.ServerTouched.add(receipt); // add record to client seed list (if specified in receipt.seed)
|
||||
return [receipt.JSON, false] as FetchHookState;
|
||||
Fetch.ServerTouched.add(receipt);
|
||||
}
|
||||
return [receipt.JSON, receipt.CachedAt === 0];
|
||||
}
|
||||
|
||||
React.useEffect(()=>
|
||||
{
|
||||
if(listen)
|
||||
{
|
||||
//const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR);
|
||||
receipt.Promise?.then(()=>cacheSet([receipt.JSON, receipt.CachedAt === 0]));
|
||||
}
|
||||
const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR);
|
||||
receipt.Promise?.then(()=>cacheSet([receipt.JSON, receipt.CachedAt === 0]));
|
||||
}
|
||||
, []);
|
||||
|
||||
|
23
server.tsx
23
server.tsx
@ -2,7 +2,6 @@ import * as ESBuild from 'https://deno.land/x/esbuild@v0.14.45/mod.js';
|
||||
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
|
||||
import { debounce } from "https://deno.land/std@0.151.0/async/debounce.ts";
|
||||
import SSR from "https://esm.sh/v113/preact-render-to-string@6.0.2";
|
||||
import Prepass from "https://esm.sh/preact-ssr-prepass@1.2.0";
|
||||
import * as Twind from "https://esm.sh/@twind/core@1.1.3";
|
||||
import React from "react";
|
||||
import * as Iso from "@eno/iso";
|
||||
@ -36,8 +35,7 @@ const Transpileable =(inFilePath:string):boolean=>
|
||||
};
|
||||
const Transpile =async(inCode:string, inKey:string):Promise<string>=>
|
||||
{
|
||||
const transpile = await ESBuild.transform(inCode, { loader: "tsx", sourcemap:"inline", minify:true, sourcefile:inKey});
|
||||
|
||||
const transpile = await ESBuild.transform(inCode, { loader: "tsx", minify:true});
|
||||
Transpiled.set(inKey, transpile.code);
|
||||
return transpile.code;
|
||||
};
|
||||
@ -224,29 +222,22 @@ FileListen("${url.pathname}", reloadHandler);`;
|
||||
else
|
||||
{
|
||||
Iso.Fetch.ServerBlocking = [];
|
||||
Iso.Fetch.ServerTouched = new Set();
|
||||
Iso.Fetch.ServerRemove = new Set();
|
||||
let app = <Iso.Router.Provider url={url}><App/></Iso.Router.Provider>;
|
||||
await Prepass(app)
|
||||
let bake = SSR(app);
|
||||
let bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
|
||||
while(Iso.Fetch.ServerBlocking.length)
|
||||
{
|
||||
await Promise.all(Iso.Fetch.ServerBlocking);
|
||||
Iso.Fetch.ServerBlocking = [];
|
||||
// at this point, anything that was requested that was not cached, has now been loaded and cached
|
||||
// this next render will use cached resources. using a cached resource (if its "Seed" is true) adds it to the "touched" set.
|
||||
app = <Iso.Router.Provider url={url}><App/></Iso.Router.Provider>;
|
||||
await Prepass(app)
|
||||
bake = SSR(app);
|
||||
bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
|
||||
}
|
||||
|
||||
const seed:Iso.FetchRecord[] = [];
|
||||
Iso.Fetch.ServerTouched.forEach((record)=>{
|
||||
|
||||
const r:Iso.FetchRecord = {...record};
|
||||
delete r.Promise;
|
||||
seed.push(r);
|
||||
});
|
||||
Iso.Fetch.ServerTouched = false;
|
||||
Iso.Fetch.ServerTouched = new Set();
|
||||
|
||||
const results = Twind.extract(bake, TwindInst);
|
||||
|
||||
@ -264,13 +255,13 @@ FileListen("${url.pathname}", reloadHandler);`;
|
||||
<body>
|
||||
<div id="app">${results.html}</div>
|
||||
<script type="module">
|
||||
import {hydrate, createElement as H} from "react";
|
||||
import {render, createElement as H} from "react";
|
||||
import * as Twind from "https://esm.sh/v115/@twind/core@1.1.3/es2022/core.mjs";
|
||||
import {default as App, CSS} from "@eno/app";
|
||||
import {Router, Fetch} from "@eno/iso";
|
||||
Twind.install(CSS);
|
||||
Fetch.Seed(${JSON.stringify(seed)});
|
||||
hydrate( H(Router.Provider, null, H(App)), document.querySelector("#app"));
|
||||
render( H(Router.Provider, null, H(App)), document.querySelector("#app"));
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
|
Loading…
Reference in New Issue
Block a user