Compare commits
No commits in common. "6e4b7af16f88ba29b53cd2fa9873e124fff74d45" and "8ab67be4d8adc60e0c9db55d803a309ac78b8867" have entirely different histories.
6e4b7af16f
...
8ab67be4d8
@ -8,7 +8,7 @@ const Comp = React.lazy(()=>import("./deep/component.tsx"));
|
||||
export default ()=>
|
||||
{
|
||||
return <div class="p-4 font-sans">
|
||||
<Iso.Meta.Metas title="Main Page!"/>
|
||||
<Iso.Metas title="Main Page!"/>
|
||||
<nav class="p-4">
|
||||
<a class="text-red-500" href="/">Home</a>
|
||||
<a href="/about">About</a>
|
||||
@ -22,12 +22,7 @@ export default ()=>
|
||||
<Iso.Switch>
|
||||
<Iso.Case value="page">
|
||||
<Iso.Switch>
|
||||
<Iso.Case value="about-us">
|
||||
<>
|
||||
<Iso.Meta.Metas title="About US"/>
|
||||
About us!
|
||||
</>
|
||||
</Iso.Case>
|
||||
<Iso.Case value="about-us">About us!</Iso.Case>
|
||||
<Iso.Case default>sorry no page</Iso.Case>
|
||||
</Iso.Switch>
|
||||
</Iso.Case>
|
||||
|
@ -10,10 +10,10 @@ export default ()=>
|
||||
const [Data, Updating] = Iso.Fetch.Use(`https://catfact.ninja/fact`);
|
||||
|
||||
|
||||
console.log("component.tsx render!!")
|
||||
console.log("render!!")
|
||||
|
||||
return <div class="p-4 text-red-500">
|
||||
<Iso.Meta.Metas title="Component!"/>
|
||||
<Iso.Metas title="Component!"/>
|
||||
Component Route is: {routeGet.Path.toString()}
|
||||
<button className="p-4 bg-green-500 text-white" onClick={e=>{countSet(countGet+1); routeSet(["lol", "idk"], {count:countGet+1});}}>{countGet}</button>
|
||||
<a href="/page/about-us" className="p-2 text(lg blue-500) font-bold">a link</a>
|
||||
|
126
lib/iso.tsx
126
lib/iso.tsx
@ -1,116 +1,33 @@
|
||||
import React from "react";
|
||||
|
||||
type MetasInputs = { [Property in MetaKeys]?: string };
|
||||
type MetasModeArgs = {concatListed?:boolean; dropUnlisted?:boolean};
|
||||
type MetasStackItem = MetasModeArgs&MetasInputs&{id:string, depth:number}
|
||||
type Meta = {title:string, description:string, keywords:string, image:string, canonical:string }
|
||||
type MetaKeys = keyof Meta;
|
||||
|
||||
export const Meta =
|
||||
{
|
||||
Stack:[] as Array<MetasStackItem>,
|
||||
Meta: {
|
||||
export const Meta:Meta = {
|
||||
title:"",
|
||||
description:"",
|
||||
keywords:"",
|
||||
image:"",
|
||||
canonical:""
|
||||
} as Meta,
|
||||
Context: React.createContext([[], ()=>{}] as [Get:MetasStackItem[], Set:React.StateUpdater<MetasStackItem[]>]),
|
||||
Provider({children}:{children:Children})
|
||||
{
|
||||
const binding = React.useState([] as MetasStackItem[]);
|
||||
};
|
||||
|
||||
React.useEffect(()=>{
|
||||
const stack = binding[0];
|
||||
const last = stack[stack.length-1];
|
||||
console.log("updating page title", stack);
|
||||
document.title = last?.title||"";
|
||||
})
|
||||
return <Meta.Context.Provider value={binding}>{children}</Meta.Context.Provider>;
|
||||
},
|
||||
Metas({concatListed=false, dropUnlisted=false, ...props}:MetasModeArgs&MetasInputs):null
|
||||
{
|
||||
const id = React.useId();
|
||||
const [, metasSet] = React.useContext(Meta.Context);
|
||||
const {depth} = React.useContext(SwitchContext);
|
||||
type MetasInputs = { [Property in MetaKeys]?: string };
|
||||
export const Metas =(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasInputs):null=>
|
||||
{
|
||||
const additive = props.concatListed ? (key:MetaKeys, value:string)=> Meta[key] += value : (key:MetaKeys, value:string)=> Meta[key] = value;
|
||||
const subtractive = props.dropUnlisted ? (key:MetaKeys)=> Meta[key] = "" : (key:MetaKeys)=> {};
|
||||
|
||||
React.useEffect(()=>{
|
||||
metasSet((m)=>{
|
||||
console.log(`adding meta`, props, depth);
|
||||
const clone = [...m];
|
||||
let i;
|
||||
for(i=clone.length-1; i>-1; i--)
|
||||
{
|
||||
if(clone[i].depth <= depth)
|
||||
{
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
clone.splice(i+1, 0, {id, depth, concatListed, dropUnlisted, ...props});
|
||||
return clone;
|
||||
});
|
||||
return ()=>
|
||||
{
|
||||
metasSet((m)=>{
|
||||
const clone = [...m];
|
||||
const ind = clone.findIndex(i=>i.id === id);
|
||||
if(ind > -1)
|
||||
{
|
||||
console.log(`removing meta`, props, depth);
|
||||
clone.splice(ind, 1);
|
||||
}
|
||||
return clone;
|
||||
});
|
||||
|
||||
};
|
||||
}, []);
|
||||
|
||||
React.useEffect(()=>{
|
||||
metasSet((m)=>{
|
||||
const clone = [...m];
|
||||
const ind = clone.findIndex(i=>i.id === id);
|
||||
if(ind > -1)
|
||||
{
|
||||
console.log(`updating meta`, props, depth);
|
||||
clone[ind] = {...clone[ind], ...props};
|
||||
}
|
||||
return clone;
|
||||
});
|
||||
}, Object.keys(props).map( (key) => props[key as MetaKeys] ));
|
||||
|
||||
/*
|
||||
React.useEffect(()=>{
|
||||
|
||||
metasSet((m)=>
|
||||
{
|
||||
const metas = {...m};
|
||||
|
||||
const additive = concatListed ? (key:MetaKeys, value:string)=> metas[key] += value : (key:MetaKeys, value:string)=> metas[key] = value;
|
||||
const subtractive = dropUnlisted ? (key:MetaKeys)=> metas[key] = "" : (key:MetaKeys)=> {};
|
||||
|
||||
Object.keys(metas).forEach((key)=>{
|
||||
Object.keys(Meta).forEach((key)=>{
|
||||
const metaKey = key as MetaKeys;
|
||||
const propValue = props[metaKey]||"";
|
||||
propValue ? additive(metaKey, propValue) : subtractive(metaKey);
|
||||
});
|
||||
|
||||
Meta.Meta = metas;
|
||||
console.log(Meta.Meta);
|
||||
|
||||
})
|
||||
if(window.innerWidth)
|
||||
{
|
||||
document.title = metas.title;
|
||||
document.title = Meta.title;
|
||||
}
|
||||
|
||||
return metas;
|
||||
});
|
||||
}, [props]);
|
||||
*/
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export type Children = string | number | React.JSX.Element | React.JSX.Element[];
|
||||
@ -161,10 +78,7 @@ export const Router = {
|
||||
document.addEventListener("click", e=>
|
||||
{
|
||||
const t = e.target as HTMLAnchorElement;
|
||||
const path = e.composedPath() as HTMLAnchorElement[];
|
||||
for(let i=0; i<path.length; i++)
|
||||
{
|
||||
if(path[i].href)
|
||||
if(t.href)
|
||||
{
|
||||
const u = new URL(t.href);
|
||||
if(u.origin == document.location.origin)
|
||||
@ -173,8 +87,6 @@ export const Router = {
|
||||
const parts = Router.Parse(u);
|
||||
routeUpdate(parts.Path, parts.Params, parts.Anchor);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
})
|
||||
}, []);
|
||||
@ -237,7 +149,7 @@ export const Switch =({children}:{children:Children})=>
|
||||
}
|
||||
if(childCase?.props?.default && !fallback)
|
||||
{
|
||||
//console.log(routeSegment);
|
||||
console.log(routeSegment);
|
||||
fallback = childCaseChildren;
|
||||
}
|
||||
}
|
||||
@ -276,7 +188,7 @@ export const Fetch = {
|
||||
inCheck.Promise = fetch(URL, Init?Init:undefined).then(resp=>resp.json()).then((json)=>{
|
||||
inCheck.JSON = json;
|
||||
inCheck.CachedAt = new Date().getTime();
|
||||
//console.log(`...cached!`);
|
||||
console.log(`...cached!`);
|
||||
return inCheck;
|
||||
});
|
||||
return inCheck;
|
||||
@ -287,7 +199,7 @@ export const Fetch = {
|
||||
// not in the cache
|
||||
// - listen
|
||||
|
||||
//console.log(`making new cache record...`);
|
||||
console.log(`making new cache record...`);
|
||||
return [load({URL, CacheFor, CachedAt:0, CacheOnServer, DelaySSR, Seed}), false, true];
|
||||
}
|
||||
else if(check.CachedAt == 0)
|
||||
@ -296,26 +208,26 @@ export const Fetch = {
|
||||
// - listen
|
||||
// - possibly init if there is something in JSON
|
||||
|
||||
//console.log(`currently being cached...`);
|
||||
console.log(`currently being cached...`);
|
||||
return [check, check.JSON ? true : false, true];
|
||||
}
|
||||
else
|
||||
{
|
||||
//console.log(`found in cache...`);
|
||||
console.log(`found in cache...`);
|
||||
let secondsAge = (new Date().getTime() - check.CachedAt)/1000;
|
||||
if(secondsAge > check.CacheFor)
|
||||
{
|
||||
// cached but expired
|
||||
// - listen
|
||||
// - init
|
||||
//console.log(`...outdated...`);
|
||||
console.log(`...outdated...`);
|
||||
return [load(check), true, true];
|
||||
}
|
||||
else
|
||||
{
|
||||
// cached and ready
|
||||
// - init
|
||||
//console.log(`...retrieved!`);
|
||||
console.log(`...retrieved!`);
|
||||
return [check, true, false];
|
||||
}
|
||||
|
||||
|
15
server.tsx
15
server.tsx
@ -226,7 +226,7 @@ FileListen("${url.pathname}", reloadHandler);`;
|
||||
Iso.Fetch.ServerBlocking = [];
|
||||
Iso.Fetch.ServerTouched = new Set();
|
||||
Iso.Fetch.ServerRemove = new Set();
|
||||
let app = <Iso.Router.Provider url={url}><Iso.Meta.Provider><App/></Iso.Meta.Provider></Iso.Router.Provider>;
|
||||
let app = <Iso.Router.Provider url={url}><App/></Iso.Router.Provider>;
|
||||
await Prepass(app)
|
||||
let bake = SSR(app);
|
||||
while(Iso.Fetch.ServerBlocking.length)
|
||||
@ -235,7 +235,7 @@ FileListen("${url.pathname}", reloadHandler);`;
|
||||
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}><Iso.Meta.Provider><App/></Iso.Meta.Provider></Iso.Router.Provider>;
|
||||
app = <Iso.Router.Provider url={url}><App/></Iso.Router.Provider>;
|
||||
await Prepass(app)
|
||||
bake = SSR(app);
|
||||
}
|
||||
@ -255,7 +255,7 @@ FileListen("${url.pathname}", reloadHandler);`;
|
||||
`<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>${Iso.Meta.Meta.title}</title>
|
||||
<title>${Iso.Meta.title}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta charset="utf-8"/>
|
||||
<style data-twind>${results.css}</style>
|
||||
@ -267,16 +267,11 @@ FileListen("${url.pathname}", reloadHandler);`;
|
||||
import {hydrate, 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, Meta} from "@eno/iso";
|
||||
import {Router, Fetch} from "@eno/iso";
|
||||
Twind.install(CSS);
|
||||
Fetch.Seed(${JSON.stringify(seed)});
|
||||
const hmrWrap = H( ()=>H(App) );
|
||||
hydrate(
|
||||
H(Router.Provider, null,
|
||||
H(Meta.Provider, null, hmrWrap)
|
||||
),
|
||||
document.querySelector("#app")
|
||||
);
|
||||
hydrate( H(Router.Provider, null, hmrWrap), document.querySelector("#app"));
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
|
Loading…
Reference in New Issue
Block a user