From b8d929f2b85196fcaef237ac9f5feb37d08cc2d9 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath <2+defunkt@users.noreply.github.com> Date: Tue, 27 Jan 2026 16:38:39 -0800 Subject: [PATCH] cache fixes --- src/index.tsx | 30 +++++++++++++++++++----------- src/utils.tsx | 10 +++------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 13e26d5..da5f62c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -16,6 +16,8 @@ export * from './utils' export { frontend } from './frontend' export type { Context } from 'hono' +const pageCache = new Map() + export type HypeProps = { pico?: boolean reset?: boolean @@ -146,12 +148,22 @@ export class Hype< if (!(await Bun.file(path).exists())) return render404(c) - const layoutPath = join(process.env.PWD ?? '.', `./src/pages/_layout.tsx`) let Layout = defaultLayout - if (await Bun.file(layoutPath).exists()) - Layout = (await import(layoutPath + `?t=${Date.now()}`)).default + const layoutPath = join(process.env.PWD ?? '.', `./src/pages/_layout.tsx`) + if (await Bun.file(layoutPath).exists()) { + let Layout = pageCache.get(layoutPath) + if (!Layout) { + Layout = (await import(layoutPath + `?t=${Date.now()}`)).default + pageCache.set(layoutPath, Layout) + } + } + + let page = pageCache.get(path) + if (!page) { + page = await import(path + `?t=${Date.now()}`) + pageCache.set(path, page) + } - const page = await import(path + `?t=${Date.now()}`) const innerHTML = typeof page.default === 'function' ? : page.default const withLayout = this.props.layout !== false ? {innerHTML} : innerHTML return c.html(withLayout) @@ -164,9 +176,7 @@ export class Hype< const isDev = process.env.NODE_ENV !== 'production' let port = process.env.PORT ? Number(process.env.PORT) : 3000 - if (isDev) { - port = findAvailablePortSync(port) - } + if (isDev) port = findAvailablePort(port) return { port, @@ -176,10 +186,8 @@ export class Hype< } } -/** - * Synchronously find an available port starting from the given port - */ -function findAvailablePortSync(startPort: number, maxAttempts = 100): number { +// find an available port starting from the given port +function findAvailablePort(startPort: number, maxAttempts = 100): number { for (let port = startPort; port < startPort + maxAttempts; port++) { try { const server = Bun.serve({ port, fetch: () => new Response() }) diff --git a/src/utils.tsx b/src/utils.tsx index 03bdcbe..7a08ca3 100644 --- a/src/utils.tsx +++ b/src/utils.tsx @@ -1,5 +1,4 @@ import { type Context } from 'hono' -import { stat } from 'fs/promises' // template literal tag for inline CSS. returns a