Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ SESSION_SECRET=

# Session idle timeout in milliseconds (default: 30 minutes)
# ADMIN_SESSION_IDLE_TIMEOUT_MS=1800000

# ── Cache Control ────────────────────────────────────────────
# These mirror LibreChat's cache env vars. ADMIN_PANEL_* variants
# take precedence, falling back to the shared LibreChat equivalents.

# Static asset caching (hashed files in /assets/)
# STATIC_CACHE_MAX_AGE=172800 # browser max-age in seconds (default: 2 days)
# STATIC_CACHE_S_MAX_AGE=86400 # CDN s-maxage in seconds (default: 1 day)
# ADMIN_PANEL_STATIC_CACHE_MAX_AGE= # overrides STATIC_CACHE_MAX_AGE for admin panel
# ADMIN_PANEL_STATIC_CACHE_S_MAX_AGE= # overrides STATIC_CACHE_S_MAX_AGE for admin panel

# HTML / index response caching
# INDEX_CACHE_CONTROL=no-cache, no-store, must-revalidate
# INDEX_PRAGMA=no-cache
# INDEX_EXPIRES=0
# ADMIN_PANEL_INDEX_CACHE_CONTROL= # overrides INDEX_CACHE_CONTROL for admin panel
# ADMIN_PANEL_INDEX_PRAGMA= # overrides INDEX_PRAGMA for admin panel
# ADMIN_PANEL_INDEX_EXPIRES= # overrides INDEX_EXPIRES for admin panel
5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@tanstack/react-router-ssr-query": "^1.166.10",
"@tanstack/react-start": "^1.167.16",
"@tanstack/router-plugin": "^1.167.12",
"@types/bun": "^1.3.11",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"date-fns": "^4.1.0",
Expand Down
38 changes: 36 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import { join } from 'node:path';
const CLIENT_DIR = join(import.meta.dir, 'dist', 'client');
const SERVER_ENTRY = new URL('./dist/server/server.js', import.meta.url);

const env = process.env;

const ONE_DAY = 86400;
const maxAge = Number(env.ADMIN_PANEL_STATIC_CACHE_MAX_AGE ?? env.STATIC_CACHE_MAX_AGE) || ONE_DAY * 2;
const sMaxAge = Number(env.ADMIN_PANEL_STATIC_CACHE_S_MAX_AGE ?? env.STATIC_CACHE_S_MAX_AGE) || ONE_DAY;
Comment thread
danny-avila marked this conversation as resolved.
Outdated

const NO_CACHE: Record<string, string> = {
'Cache-Control': env.ADMIN_PANEL_INDEX_CACHE_CONTROL ?? env.INDEX_CACHE_CONTROL ?? 'no-cache, no-store, must-revalidate',
Pragma: env.ADMIN_PANEL_INDEX_PRAGMA ?? env.INDEX_PRAGMA ?? 'no-cache',
Expires: env.ADMIN_PANEL_INDEX_EXPIRES ?? env.INDEX_EXPIRES ?? '0',
};

const LONG_CACHE: Record<string, string> = {
'Cache-Control': `public, max-age=${maxAge}, s-maxage=${sMaxAge}`,
};

const NEVER_CACHE = new Set(['manifest.json', 'sw.js', 'robots.txt']);

function getCacheHeaders(filePath: string): Record<string, string> {
const fileName = filePath.split('/').pop() ?? '';
if (NEVER_CACHE.has(fileName)) return NO_CACHE;
if (filePath.startsWith('assets/')) return LONG_CACHE;
return {};
}

type Handler = { default: { fetch: (req: Request) => Promise<Response> } };

const { default: handler } = (await import(SERVER_ENTRY.href)) as Handler;
Expand All @@ -12,7 +37,9 @@ async function buildStaticRoutes(): Promise<Record<string, () => Response>> {
const routes: Record<string, () => Response> = {};
for await (const path of new Glob('**/*').scan(CLIENT_DIR)) {
const file = Bun.file(`${CLIENT_DIR}/${path}`);
routes[`/${path}`] = () => new Response(file, { headers: { 'Content-Type': file.type } });
const cache = getCacheHeaders(path);
routes[`/${path}`] = () =>
new Response(file, { headers: { 'Content-Type': file.type, ...cache } });
}
return routes;
}
Expand All @@ -21,7 +48,14 @@ Bun.serve({
port: Number(process.env.PORT ?? 3000),
routes: {
...(await buildStaticRoutes()),
'/*': (req) => handler.fetch(req),
'/*': async (req) => {
const res = await handler.fetch(req);
const patched = new Response(res.body, res);
for (const [k, v] of Object.entries(NO_CACHE)) {
patched.headers.set(k, v);
}
return patched;
},
},
});

Expand Down