diff --git a/package.json b/package.json index 7c76b99..fb0d05a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "module", "scripts": { "dev": "concurrently \"bun run --hot src/main.ts\" \"bun run uno:dev\" \"bun run liveReload\"", - "liveReload": "bun run src/lib/liveReload.ts", + "liveReload": "bun run src/beth/liveReload.ts", "start": "bun run uno && bun run src/main.ts", "db:push": "bunx drizzle-kit push:sqlite", "db:studio": "bunx drizzle-kit studio", diff --git a/src/components/base.tsx b/src/components/base.tsx index 97f83af..51b8882 100644 --- a/src/components/base.tsx +++ b/src/components/base.tsx @@ -8,6 +8,29 @@ export const BaseHtml = ({ children }: PropsWithChildren) => ( THE BETH STACK + ( `} - {children} + {children} ); diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 577265d..f619e7b 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,16 +1,52 @@ import Elysia from "elysia"; import { BaseHtml } from "../components/base"; import { ctx } from "../context"; +import { renderToString } from "beth-jsx"; +import { persistedCache, revalidateTag } from "../beth/cache"; -export const index = new Elysia().use(ctx).get("/", ({ html }) => - html( - -
- - ) -); +const start = Date.now(); + +const getTime = async () => (Date.now() - start) / 1000; + +const cachedGetTime = persistedCache(getTime, "getTime", { + tags: ["time"], + revalidate: 2, +}); + +export const index = new Elysia() + .use(ctx) + .onRequest(({ request }) => { + const revalidate = request.headers.get("HX-Revalidate"); + if (revalidate) { + const tags = JSON.parse(revalidate); + tags.forEach((tag: string) => { + revalidateTag(tag); + }); + } + }) + .get("/test", async () => { + const time = await cachedGetTime(); + return renderToString(() =>

{time}

); + }) + .get("/", async ({ set }) => { + set.headers["content-type"] = "text/html"; + return renderToString(() => ( + +

cache revalidates on two second interval

+ +
+
+ +
+
+ )); + }); diff --git a/src/types/htmx.d.ts b/src/types/htmx.d.ts index 19cc4c3..9da79be 100644 --- a/src/types/htmx.d.ts +++ b/src/types/htmx.d.ts @@ -44,5 +44,6 @@ declare namespace JSX { ["hx-delete"]?: DeleteRoutes; ["hx-patch"]?: PatchRoutes; _?: string; + ["hx-revalidate"]?: string; } }