Skip to content

Commit 174bd3c

Browse files
committed
fix(@angular/build): ensure ɵgetOrCreateAngularServerApp is always defined after errors
Addresses an issue where could become `ɵgetOrCreateAngularServerApp` undefined after an error, leading to subsequent rendering failures. This change modifies the HMR process to include a timestamp when loading. This ensures the server application is always re-evaluated, preventing stale application states.
1 parent 21a486b commit 174bd3c

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

packages/angular/build/src/builders/dev-server/vite/hmr.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { ComponentStyleRecord } from '../../../tools/vite/middlewares';
1414
import { BuildOutputFileType } from '../internal';
1515
import type { NormalizedDevServerOptions } from '../options';
1616
import type { OutputAssetRecord, OutputFileRecord } from './utils';
17+
import { now } from 'lodash';
1718

1819
/**
1920
* Invalidates any updated asset or generated files and resets their `updated` state.
@@ -57,9 +58,12 @@ export async function invalidateUpdatedFiles(
5758
}
5859

5960
if (serverApplicationChanged) {
60-
// Clear the server app cache and
61-
// trigger module evaluation before reload to initiate dependency optimization.
62-
const { ɵdestroyAngularServerApp } = (await server.ssrLoadModule('/main.server.mjs')) as {
61+
// Clear the server app cache and trigger module evaluation before reload to initiate dependency optimization.
62+
// The querystring is needed as a workaround for:
63+
// `ɵgetOrCreateAngularServerApp` can be undefined right after an error.
64+
const { ɵdestroyAngularServerApp } = (await server.ssrLoadModule(
65+
`/main.server.mjs?timestamp=${now()}`,
66+
)) as {
6367
ɵdestroyAngularServerApp: typeof destroyAngularServerApp;
6468
};
6569

packages/angular/build/src/tools/vite/middlewares/ssr-middleware.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ export function createAngularSsrInternalMiddleware(
4545
ɵgetOrCreateAngularServerApp: typeof getOrCreateAngularServerApp;
4646
};
4747

48-
// `ɵgetOrCreateAngularServerApp` can be undefined right after an error.
49-
// See: https://github.com/angular/angular-cli/issues/29907
50-
if (!ɵgetOrCreateAngularServerApp) {
51-
return next();
52-
}
53-
5448
const angularServerApp = ɵgetOrCreateAngularServerApp({
5549
allowStaticRouteRender: true,
5650
});

0 commit comments

Comments
 (0)