dev-server: with SSR, stale component HMR update re-applied on every page load
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 75/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- angular, sass, typescript
- 领域
- build-system, devtools
调研方向
Start with packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts and plugins/setup-middlewares-plugin.ts, then compare their SSR request flow with the existing reset path in the Angular index HTML middleware. Reproduce the Sass partial case with SSR and verify both internal and external SSR paths clear stale component updates after a rendered document while component HMR still works.
由索引模型根据 Issue 内容生成。
描述
Command
serve
Is this a regression?
- Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
No response
Description
With SSR enabled, once a component has received an HMR update (Component update sent to client(s).), later changes that rebuild as a page reload never show in the browser for that component. Typical trigger: editing a Sass partial @used by the component stylesheet. Server output (/, main.js) is fresh; the browser keeps the old styles through any number of reloads, until the component's own file is saved again or ng serve restarts.
Cause, read from @angular/build 22.2.0 source:
src/builders/dev-server/vite/index.js: aComponentUpdateresult stores its content intemplateUpdates. The map is cleared only forResultKind.Full. The dev-server setsincrementalResults = true, so every rebuild after the first isIncrementaland never clears it.- The other clear path,
resetComponentUpdates, is passed only tocreateAngularIndexHtmlMiddleware(src/tools/vite/plugins/setup-middlewares-plugin.js). With SSR the SSR middleware answers the page request first, so the reset never runs. - On load, each component's HMR init fetches
/@ng/component?c=<id>&t=<now>.component-middleware.jsreturns the stored, now stale, update and the runtime applies it over the fresh bundle.
Without SSR the same steps work: the index HTML middleware resets the map on reload.
Expected: a page load after a later rebuild uses current styles. A fix is proposed at the end of this issue.
Minimal Reproduction
ng new hmr-repro-ssr --defaults --style=scss --ssrsrc/app/_tokens.scss:$gap: 10px;src/app/app.scss:@use "tokens"; h1 { margin-top: tokens.$gap; color: red; }src/app/app.html:<h1>Hello</h1>ng serve, open http://localhost:4200/. h1:margin-top: 10px, red.- In
app.scsschangeredtoblue. Log:Component update sent to client(s).h1 turns blue. Correct. - In
_tokens.scsschange10pxto40px. Log:Page reload sent to client(s). - Actual: h1 keeps
margin-top: 10px, also after a manual reload or a new navigation.curl http://localhost:4200/andcurl http://localhost:4200/main.jsboth carrymargin-top: 40px.curl 'http://localhost:4200/@ng/component?c=src%2Fapp%2Fapp.ts%40App&t=1'still returnsmargin-top: 10px.
- Save
app.scssagain, or restartng serve:40pxappears.
Control: the same steps in an app created with --ssr=false show 40px at step 8.
Workaround: NG_HMR_TEMPLATES=0 ng serve, or ng serve --no-hmr.
Exception or Error
Your Environment
Angular CLI : 22.2.0
Angular : 22.2.0
Node.js : 26.7.0
Package Manager : pnpm 10.30.2
Operating System : darwin arm64
@angular/build 22.2.0
@angular/cli 22.2.0
@angular/common 22.2.0
@angular/compiler 22.2.0
@angular/compiler-cli 22.2.0
@angular/core 22.2.0
@angular/platform-browser 22.2.0
@angular/platform-server 22.2.0
@angular/router 22.2.0
@angular/ssr 22.2.0
rxjs 7.8.2
typescript 6.0.3
Anything else relevant?
Not browser specific: the stale content is served by the dev server (tested in Chrome). First seen on 22.2.0-rc.0 in a larger SSR app, then confirmed on a clean 22.2.0 app with the steps above.
Proposed fix (verified locally)
CSR already resets the map on a page load: createAngularIndexHtmlMiddleware calls resetComponentUpdates() ("A request for the index indicates a full page reload request."). The SSR middlewares never receive that callback. Pass it to both SSR middleware factories and call it in the html:transform:pre hook, which runs once per rendered document:
--- a/packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts
+++ b/packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts
@@ -22,6 +22,7 @@
export function createAngularSsrInternalMiddleware(
server: ViteDevServer,
indexHtmlTransformer?: (content: string) => Promise<string>,
+ resetComponentUpdates?: () => void,
): Connect.NextHandleFunction {
let cachedAngularServerApp: ReturnType<typeof getOrCreateAngularServerApp> | undefined;
@@ -53,6 +54,9 @@
// Only Add the transform hook only if it's a different instance.
if (cachedAngularServerApp !== angularServerApp) {
angularServerApp.hooks.on('html:transform:pre', async ({ html, url }) => {
+ // A rendered document indicates a full page reload request.
+ resetComponentUpdates?.();
+
const processedHtml = await server.transformIndexHtml(url.pathname, html);
return indexHtmlTransformer?.(processedHtml) ?? processedHtml;
@@ -77,6 +81,7 @@
export async function createAngularSsrExternalMiddleware(
server: ViteDevServer,
indexHtmlTransformer?: (content: string) => Promise<string>,
+ resetComponentUpdates?: () => void,
): Promise<Connect.NextHandleFunction> {
let fallbackWarningShown = false;
let cachedAngularAppEngine: typeof SSRAngularAppEngine | undefined;
@@ -121,6 +126,7 @@
angularSsrInternalMiddleware ??= createAngularSsrInternalMiddleware(
server,
indexHtmlTransformer,
+ resetComponentUpdates,
);
angularSsrInternalMiddleware(req, res, next);
@@ -132,6 +138,9 @@
AngularAppEngine.ɵdisableAllowedHostsCheck = disableAllowedHostsCheck;
AngularAppEngine.ɵallowStaticRouteRender = true;
AngularAppEngine.ɵhooks.on('html:transform:pre', async ({ html, url }) => {
+ // A rendered document indicates a full page reload request.
+ resetComponentUpdates?.();
+
const processedHtml = await server.transformIndexHtml(url.pathname, html);
return indexHtmlTransformer?.(processedHtml) ?? processedHtml;
--- a/packages/angular/build/src/tools/vite/plugins/setup-middlewares-plugin.ts
+++ b/packages/angular/build/src/tools/vite/plugins/setup-middlewares-plugin.ts
@@ -117,13 +117,13 @@
if (ssrMode === ServerSsrMode.ExternalSsrMiddleware) {
patchBaseMiddleware(server.middlewares, server.config.base);
- middlewares.use(await createAngularSsrExternalMiddleware(server, indexHtmlTransformer));
+ middlewares.use(await createAngularSsrExternalMiddleware(server, indexHtmlTransformer, resetComponentUpdates));
return;
}
if (ssrMode === ServerSsrMode.InternalSsrMiddleware) {
- middlewares.use(createAngularSsrInternalMiddleware(server, indexHtmlTransformer));
+ middlewares.use(createAngularSsrInternalMiddleware(server, indexHtmlTransformer, resetComponentUpdates));
}
middlewares.use(angularHtmlFallbackMiddleware);
Verified by applying the equivalent change to the compiled @angular/build 22.2.0 in the reproduction above (external SSR middleware, default ng new --ssr server.ts):
- Before: after steps 6 and 7 plus one page request,
/@ng/componentstill returns the stored update (4839 bytes,margin-top: 10px). - After: the same request returns an empty body. The browser shows the new partial value after the reload, and component HMR still hot-swaps in place (step 6 applies without a page reload).
Not exercised: the internal SSR middleware path (the same change is applied there). The diff has not been run through prettier.
- 主要语言
- TypeScript
- 星标
- 27k
- 派生
- 11.8k
- 平均合并
- 17 小时 47 分钟
- 30 天内合并 PR
- 181
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
angular/angular-cli 的其他 Issue
-
area: @angular/build gemini-triaged
难度 2/5 1-3 小时 新手友好度 74/100
angular/angular-cli#33955 ·
-
area: @angular/cli gemini-triaged
难度 2/5 1-3 小时 新手友好度 72/100
angular/angular-cli#33055 · 1 条评论 · 3 个 reaction ·
-
area: @angular/build gemini-triaged
难度 3/5 1-2 天 新手友好度 68/100
angular/angular-cli#34166 · 1 条评论 ·
-
area: @angular/build gemini-triaged
难度 4/5 3-5 天 新手友好度 52/100
angular/angular-cli#34164 ·
-
area: @angular/build gemini-triaged severity5: regression
难度 4/5 3-5 天 新手友好度 58/100
angular/angular-cli#34162 · 1 条评论 · 1 个 reaction ·
查看 angular/angular-cli 的全部 Issue
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 88/100
motiondivision/motion#3849 ·
-
check:passed streams:add
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
bug
难度 2/5 1-3 小时 新手友好度 84/100
i-am-bee/beeai-framework#1697 · 1 个 reaction ·
-
enhancement
难度 2/5 1-3 小时 新手友好度 75/100
antfu/node-modules-inspector#214 ·