Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/web-assets-cache-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

web: Serve hashed static assets with long-lived immutable cache headers so repeat visits load faster.
12 changes: 11 additions & 1 deletion packages/kap-server/src/routes/webAssets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createReadStream } from 'node:fs';
import { stat } from 'node:fs/promises';
import { extname, join, normalize, resolve, sep } from 'node:path';
import { extname, join, normalize, relative, resolve, sep } from 'node:path';

import type { FastifyReply, FastifyRequest } from 'fastify';

Expand Down Expand Up @@ -57,9 +57,19 @@ async function serveWebAsset(
return reply
.type(mimeType(filePath))
.header('Content-Length', String(fileInfo.size))
.header('Cache-Control', cacheControl(assetsDir, filePath))
.send(createReadStream(filePath));
}

// Vite emits content-hashed files under `assets/` — cache them forever.
// Everything else (index.html, SPA fallback, favicon, ...) must revalidate.
function cacheControl(assetsDir: string, filePath: string): string {
const rel = relative(resolve(assetsDir), filePath);
return rel.startsWith(`assets${sep}`)
? 'public, max-age=31536000, immutable'
: 'no-cache';
}

async function resolveStaticFile(
assetsDir: string,
pathname: string,
Expand Down
Loading