diff --git a/lib/iso.tsx b/lib/iso.tsx index ab1fb2e..071cbd7 100644 --- a/lib/iso.tsx +++ b/lib/iso.tsx @@ -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, DelaySSR:boolean, Error?:string, JSON?:object}; +export type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, CacheOnServer:boolean, Promise?:Promise, DelaySSR:boolean, Seed:boolean, Error?:string, JSON?:object}; export const Fetch = { Cache:new Map() as Map, ServerBlocking:false as false|Promise[], ServerTouched:false as false|Set, + ServerRemove:false as false|Set, 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); } diff --git a/server.tsx b/server.tsx index a6efd9a..2aae3d6 100644 --- a/server.tsx +++ b/server.tsx @@ -223,17 +223,19 @@ FileListen("${url.pathname}", reloadHandler);`; { Iso.Fetch.ServerBlocking = []; Iso.Fetch.ServerTouched = new Set(); + Iso.Fetch.ServerRemove = new Set(); let bake = SSR(); 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(); } const seed:Iso.FetchRecord[] = []; Iso.Fetch.ServerTouched.forEach((record)=>{ - const r:Iso.FetchRecord = {...record}; delete r.Promise; seed.push(r);