issue/fetch #21
17
lib/iso.tsx
17
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 Case =({children, value}:{children:Children, value?:string, default?:true})=>null;
|
||||||
export const useRouteVars =()=> React.useContext(SwitchContext).keys;
|
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 = {
|
export const Fetch = {
|
||||||
Cache:new Map() as Map<string, FetchRecord>,
|
Cache:new Map() as Map<string, FetchRecord>,
|
||||||
ServerBlocking:false as false|Promise<FetchRecord>[],
|
ServerBlocking:false as false|Promise<FetchRecord>[],
|
||||||
ServerTouched:false as false|Set<FetchRecord>,
|
ServerTouched:false as false|Set<FetchRecord>,
|
||||||
|
ServerRemove:false as false|Set<FetchRecord>,
|
||||||
Seed(seed:FetchRecord[])
|
Seed(seed:FetchRecord[])
|
||||||
{
|
{
|
||||||
seed.forEach(r=>{
|
seed.forEach(r=>{
|
||||||
@ -172,7 +173,7 @@ export const Fetch = {
|
|||||||
Fetch.Cache.set(r.URL, r)
|
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);
|
let check = Fetch.Cache.get(URL);
|
||||||
|
|
||||||
@ -192,7 +193,7 @@ export const Fetch = {
|
|||||||
if(!check)
|
if(!check)
|
||||||
{
|
{
|
||||||
console.log(`making new cache record...`);
|
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)
|
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];
|
type FetchHookState = [Data:undefined|object, Updating:boolean];
|
||||||
const [cacheGet, cacheSet] = React.useState([undefined, true] as FetchHookState);
|
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);
|
const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR, Seed);
|
||||||
if(receipt.CachedAt === 0 && receipt.Promise)
|
if(receipt.CachedAt === 0 && receipt.Promise)// request for something that hasnt been cached yet
|
||||||
{
|
{
|
||||||
Fetch.ServerBlocking.push(receipt.Promise);
|
Fetch.ServerBlocking.push(receipt.Promise);
|
||||||
}
|
}
|
||||||
else
|
else if(receipt.Seed)
|
||||||
{
|
{
|
||||||
Fetch.ServerTouched.add(receipt);
|
Fetch.ServerTouched.add(receipt);
|
||||||
}
|
}
|
||||||
|
@ -223,17 +223,19 @@ FileListen("${url.pathname}", reloadHandler);`;
|
|||||||
{
|
{
|
||||||
Iso.Fetch.ServerBlocking = [];
|
Iso.Fetch.ServerBlocking = [];
|
||||||
Iso.Fetch.ServerTouched = new Set();
|
Iso.Fetch.ServerTouched = new Set();
|
||||||
|
Iso.Fetch.ServerRemove = new Set();
|
||||||
let bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
|
let bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
|
||||||
while(Iso.Fetch.ServerBlocking.length)
|
while(Iso.Fetch.ServerBlocking.length)
|
||||||
{
|
{
|
||||||
await Promise.all(Iso.Fetch.ServerBlocking);
|
await Promise.all(Iso.Fetch.ServerBlocking);
|
||||||
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>);
|
bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
|
||||||
}
|
}
|
||||||
|
|
||||||
const seed:Iso.FetchRecord[] = [];
|
const seed:Iso.FetchRecord[] = [];
|
||||||
Iso.Fetch.ServerTouched.forEach((record)=>{
|
Iso.Fetch.ServerTouched.forEach((record)=>{
|
||||||
|
|
||||||
const r:Iso.FetchRecord = {...record};
|
const r:Iso.FetchRecord = {...record};
|
||||||
delete r.Promise;
|
delete r.Promise;
|
||||||
seed.push(r);
|
seed.push(r);
|
||||||
|
Loading…
Reference in New Issue
Block a user