control client seeding

This commit is contained in:
Seth Trowbridge 2023-04-22 06:22:53 -04:00
parent 1ed3e0abaa
commit bdeb51a940
2 changed files with 12 additions and 9 deletions

View File

@ -160,11 +160,12 @@ 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, Error?:string, JSON?:object};
export type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, CacheOnServer:boolean, Promise?:Promise<FetchRecord>, DelaySSR:boolean, Seed: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>,
Seed(seed:FetchRecord[])
{
seed.forEach(r=>{
@ -172,7 +173,7 @@ export const Fetch = {
Fetch.Cache.set(r.URL, r)
});
},
Request(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true)
Request(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true, Seed:boolean = true)
{
let check = Fetch.Cache.get(URL);
@ -192,7 +193,7 @@ export const Fetch = {
if(!check)
{
console.log(`making new cache record...`);
return load({URL, CacheFor, CachedAt:0, CacheOnServer, DelaySSR});
return load({URL, CacheFor, CachedAt:0, CacheOnServer, DelaySSR, Seed});
}
else if(check.CachedAt == 0)
{
@ -216,18 +217,18 @@ export const Fetch = {
}
},
Use(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true)
Use(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true, Seed:boolean = true)
{
type FetchHookState = [Data:undefined|object, Updating:boolean];
const [cacheGet, cacheSet] = React.useState([undefined, true] as FetchHookState);
if(Fetch.ServerBlocking && DelaySSR)
if(Fetch.ServerBlocking && Fetch.ServerTouched && DelaySSR)
{
const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR);
if(receipt.CachedAt === 0 && receipt.Promise)
const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR, Seed);
if(receipt.CachedAt === 0 && receipt.Promise)// request for something that hasnt been cached yet
{
Fetch.ServerBlocking.push(receipt.Promise);
}
else
else if(receipt.Seed)
{
Fetch.ServerTouched.add(receipt);
}

View File

@ -223,17 +223,19 @@ FileListen("${url.pathname}", reloadHandler);`;
{
Iso.Fetch.ServerBlocking = [];
Iso.Fetch.ServerTouched = new Set();
Iso.Fetch.ServerRemove = new Set();
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.
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);