cache fixes
This commit is contained in:
parent
52086f4eb9
commit
b8d929f2b8
|
|
@ -16,6 +16,8 @@ export * from './utils'
|
||||||
export { frontend } from './frontend'
|
export { frontend } from './frontend'
|
||||||
export type { Context } from 'hono'
|
export type { Context } from 'hono'
|
||||||
|
|
||||||
|
const pageCache = new Map()
|
||||||
|
|
||||||
export type HypeProps = {
|
export type HypeProps = {
|
||||||
pico?: boolean
|
pico?: boolean
|
||||||
reset?: boolean
|
reset?: boolean
|
||||||
|
|
@ -146,12 +148,22 @@ export class Hype<
|
||||||
if (!(await Bun.file(path).exists()))
|
if (!(await Bun.file(path).exists()))
|
||||||
return render404(c)
|
return render404(c)
|
||||||
|
|
||||||
const layoutPath = join(process.env.PWD ?? '.', `./src/pages/_layout.tsx`)
|
|
||||||
let Layout = defaultLayout
|
let Layout = defaultLayout
|
||||||
if (await Bun.file(layoutPath).exists())
|
const layoutPath = join(process.env.PWD ?? '.', `./src/pages/_layout.tsx`)
|
||||||
Layout = (await import(layoutPath + `?t=${Date.now()}`)).default
|
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 c={c} req={c.req} /> : page.default
|
const innerHTML = typeof page.default === 'function' ? <page.default c={c} req={c.req} /> : page.default
|
||||||
const withLayout = this.props.layout !== false ? <Layout props={this.props}>{innerHTML}</Layout> : innerHTML
|
const withLayout = this.props.layout !== false ? <Layout props={this.props}>{innerHTML}</Layout> : innerHTML
|
||||||
return c.html(withLayout)
|
return c.html(withLayout)
|
||||||
|
|
@ -164,9 +176,7 @@ export class Hype<
|
||||||
const isDev = process.env.NODE_ENV !== 'production'
|
const isDev = process.env.NODE_ENV !== 'production'
|
||||||
let port = process.env.PORT ? Number(process.env.PORT) : 3000
|
let port = process.env.PORT ? Number(process.env.PORT) : 3000
|
||||||
|
|
||||||
if (isDev) {
|
if (isDev) port = findAvailablePort(port)
|
||||||
port = findAvailablePortSync(port)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
port,
|
port,
|
||||||
|
|
@ -176,10 +186,8 @@ export class Hype<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// find an available port starting from the given port
|
||||||
* Synchronously find an available port starting from the given port
|
function findAvailablePort(startPort: number, maxAttempts = 100): number {
|
||||||
*/
|
|
||||||
function findAvailablePortSync(startPort: number, maxAttempts = 100): number {
|
|
||||||
for (let port = startPort; port < startPort + maxAttempts; port++) {
|
for (let port = startPort; port < startPort + maxAttempts; port++) {
|
||||||
try {
|
try {
|
||||||
const server = Bun.serve({ port, fetch: () => new Response() })
|
const server = Bun.serve({ port, fetch: () => new Response() })
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import { type Context } from 'hono'
|
import { type Context } from 'hono'
|
||||||
import { stat } from 'fs/promises'
|
|
||||||
|
|
||||||
// template literal tag for inline CSS. returns a <style> tag
|
// template literal tag for inline CSS. returns a <style> tag
|
||||||
export function css(strings: TemplateStringsArray, ...values: any[]) {
|
export function css(strings: TemplateStringsArray, ...values: any[]) {
|
||||||
|
|
@ -143,11 +142,8 @@ const transpileCache: Record<string, string> = {}
|
||||||
|
|
||||||
// transpile frontend ts to js
|
// transpile frontend ts to js
|
||||||
export async function transpile(path: string): Promise<string> {
|
export async function transpile(path: string): Promise<string> {
|
||||||
const { mtime } = await stat(path)
|
// cache by path; --watch restarts server on file changes, clearing the cache
|
||||||
const key = `${path}?${mtime}`
|
let cached = transpileCache[path]
|
||||||
|
|
||||||
// no caching in dev mode
|
|
||||||
let cached = process.env.NODE_ENV === 'production' ? transpileCache[key] : undefined
|
|
||||||
|
|
||||||
if (!cached) {
|
if (!cached) {
|
||||||
const result = await Bun.build({
|
const result = await Bun.build({
|
||||||
|
|
@ -160,7 +156,7 @@ export async function transpile(path: string): Promise<string> {
|
||||||
if (!result.outputs[0]) throw new Error(`Failed to build ${path}`)
|
if (!result.outputs[0]) throw new Error(`Failed to build ${path}`)
|
||||||
|
|
||||||
cached = await result.outputs[0].text()
|
cached = await result.outputs[0].text()
|
||||||
transpileCache[key] = cached
|
transpileCache[path] = cached
|
||||||
}
|
}
|
||||||
|
|
||||||
return cached
|
return cached
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user