perf(web): add cache headers for web UI static assets saving 90% data traffic in loading pages - #2452
Open
bowenliang123 wants to merge 1 commit into
Open
Conversation
🦋 Changeset detectedLatest commit: 6c2e94a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
Contributor
Author
|
Hi @wbxl2000 , could you consider reviewing this PR? It brings a performance improvement in page loading efficiency with minimal changes. It adds and sets the cache control to immutable, as the hashes in all generated files are always different. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue
No linked issue — the problem is explained below.
Problem
The web UI's static assets (served by kap-server's web-assets routes) are sent with only
Content-TypeandContent-Length— noCache-Control, noETag, noLast-Modified. Browsers therefore receive no caching guidance at all and fall back to per-browser heuristic caching, so repeat visits tokimi webre-download JS/CSS bundles unnecessarily (or behave inconsistently across browsers).What changed
One focused change in
packages/kap-server/src/routes/webAssets.ts:serveWebAssetnow stamps aCache-Controlheader based on the file's location relative to the assets root:assets/files (Vite's content-hashed build output, e.g.index-abc123.js) →Cache-Control: public, max-age=31536000, immutable. Content changes produce a new filename/URL, so caching them for a year is safe. This is the actual win: repeat visits load JS/CSS/fonts straight from disk cache with zero network requests, including on manual refresh (immutable).index.html, SPA fallback responses, favicon, ...) →Cache-Control: no-cache. The entry document must be revalidated every load so a new release is picked up immediately and starts referencing the new hashed assets.This is the standard "entry no-cache + hashed assets immutable" deployment pattern; the distinction is trivially reliable here because Vite always emits hashed files under
dist/assets/.Measured benefit
Built the real web UI (
apps/kimi-web→ dist) and served it through this route twice — once with the old code, once with the new — in a real Chrome instance, measuring transferred bytes via the Resource Timing API:So each repeat visit / refresh saves ~2 MB of transfer (~99% reduction; larger over the wire than these uncompressed localhost numbers since the same applies to gzipped bodies). Load time on localhost is dominated by other factors, but on any real network the difference is proportionally larger.
The remaining ~20 KB on warm loads is the
index.htmlrevalidation plus the root-levelfavicon.ico(17 KB, intentionallyno-cache); adding ETag/304 support for root files could shrink that further but is out of scope here.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.