perf(serve): give app resources validators and immutable caching - #6
Merged
Merged
Conversation
App version files were sent with only a CSP header: no Cache-Control, no ETag, no Last-Modified, while the Platform Shell's own /_next/static assets already had them. A hashed bundle under /serve/<owner>/<app>/assets/ therefore could not be cached or revalidated, so every visit re-downloaded it in full — a 570 KB bundle and a 308 KB stylesheet measured on one real app. Serve files from an app version directory through one helper that sets an ETag and Last-Modified, answers 304 on a matching If-None-Match, and marks a Vite-style content-hashed asset under assets/ immutable. Anything else (index.html, or a file the app itself shipped) keeps its name across versions, so it stays 'no-cache' and revalidates. Measured against a locally installed app on the shipped build: app JS 570,538 B, no validators -> same bytes + immutable + ETag app CSS 307,949 B, no validators -> same bytes + immutable + ETag app HTML -> no-cache + ETag, revalidates as 304 repeat request with If-None-Match -> 304, 0 bytes (was a full re-download)
Patodo
added a commit
that referenced
this pull request
Sep 18, 2026
Ships the served-app caching fix from #6: app version files now carry an ETag and Last-Modified, answer 304 on revalidation, and mark a content-hashed asset under assets/ immutable, instead of re-downloading the whole bundle on every visit. Version-bound test assertions move to 0.2.8 and the reviewed public source baseline digests are re-recorded for the two files this touches. The flagged content in both files is unchanged: the same three canonical package markers and the same apiKey fixture as before.
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.
针对页面加载耗时的排查结论里,平台侧那两条中的一条。
先纠正一处事实
那份报告说「静态服务代码在平台里,不在这个仓库」——不成立。应用资源的服务代码就在本仓库的
packages/server/src/routes/serve.ts(/serve/:userId/:name/*),我本地跑同一份代码复现了它的观察:assets/index-*.jsContent-Encoding、无Cache-Control、无ETag、无Last-Modifiedassets/index-*.css/_next/static/*(对照)etag+cache-control: public, max-age=0所以「每次访问重下 690 KB、连 304 都协商不了」这条是代码里的真实缺失,不是部署配置问题。
改了什么
/serve/**下所有来自应用版本目录的文件改由同一个 helper 发送:ETag(size + mtime)与Last-ModifiedIf-None-Match命中 → 304assets/下形如<name>-<内容哈希>.<ext>的文件 →Cache-Control: public, max-age=31536000, immutableindex.html,或应用自己上传的同名文件)→Cache-Control: no-cache,每次协商只对
assets/下的哈希名用immutable是有意的:/serve/<owner>/<app>/的 URL 不含版本号,若把应用自己上传的、名字恰好以短横线加长串结尾的文件也标成不可变,它在应用升级后就再也刷不新。实测(本机安装的真实应用,同一份数据目录)
新增用例
marks content-hashed app assets immutable and revalidates the rest;tests/integration/serve-edge.test.ts12 项全过。没做的那条:压缩(附证据)
压缩是首屏最大的收益(报告估算 1.83 MB → 552 KB),我实现了并加了
@fastify/compress,但验证后发现它在本 Server 自己的路由上是坏的,所以回退了:同一个客户端取
/_next/static/*(@fastify/static的文件路径)能正常解出 173,217 字节,说明不是客户端问题,而是插件与本 Server 自建路由的交互问题(packages/server与server-core里都没有其他onSend/serializer 钩子)。带上它会让服务端测试从 14 条失败涨到 23 条(含全部 shell/serve 渲染用例)。所以我没提交这个依赖。压缩的可行路径:你们部署前面就有 nginx,在那层开 gzip/brotli 是纯配置改动、零代码风险,也是这份报告最初的建议;如果希望由 Server 自带(容器镜像里没有代理层),需要在插件交互上单独排查一次,我可以另开 issue 跟进。
未涉及
报告里的第三条(应用是「fetch 完再注入」的串行关键路径)属于架构选型,我没有动。