Skip to content

Commit 2d2e799

Browse files
committed
fix(@angular-devkit/build-angular): clean up internal Angular state during rendering SSR
This commit clean ups the compiled components state when the build is being executed in watch mode. This is required as otherwise during development `Component ID generation collision detected` are displayed on the server. Closes: #25924
1 parent 0368b23 commit 2d2e799

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ export function createServerCodeBundleOptions(
134134
target: string[],
135135
sourceFileCache: SourceFileCache,
136136
): BuildOptions {
137-
const { jit, serverEntryPoint, workspaceRoot, ssrOptions } = options;
137+
const {
138+
jit,
139+
serverEntryPoint,
140+
workspaceRoot,
141+
ssrOptions,
142+
watch,
143+
externalPackages,
144+
prerenderOptions,
145+
} = options;
138146

139147
assert(
140148
serverEntryPoint,
@@ -194,7 +202,7 @@ export function createServerCodeBundleOptions(
194202
};
195203

196204
buildOptions.plugins ??= [];
197-
if (options.externalPackages) {
205+
if (externalPackages) {
198206
buildOptions.packages = 'external';
199207
} else {
200208
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
@@ -225,7 +233,11 @@ export function createServerCodeBundleOptions(
225233
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
226234
];
227235

228-
if (options.prerenderOptions?.discoverRoutes) {
236+
if (watch) {
237+
contents.push(`export { ɵresetCompiledComponents } from '@angular/core';`);
238+
}
239+
240+
if (prerenderOptions?.discoverRoutes) {
229241
// We do not import it directly so that node.js modules are resolved using the correct context.
230242
const routesExtractorCode = await readFile(
231243
join(__dirname, '../../utils/routes-extractor/extractor.js'),

packages/angular_devkit/build_angular/src/utils/server-rendering/main-bundle-exports.ts

+2
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ export interface MainServerBundleExports {
2525

2626
/** Method to extract routes from the router config. */
2727
extractRoutes: typeof extractRoutes;
28+
29+
ɵresetCompiledComponents?: () => void;
2830
}

packages/angular_devkit/build_angular/src/utils/server-rendering/render-page.ts

+6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ export async function renderPage({
4545
ɵSERVER_CONTEXT,
4646
renderModule,
4747
renderApplication,
48+
ɵresetCompiledComponents,
4849
} = await loadBundle('./main.server.mjs');
4950

51+
// Need to clean up GENERATED_COMP_IDS map in `@angular/core`.
52+
// Otherwise an incorrect component ID generation collision detected warning will be displayed in development.
53+
// See: https://github.com/angular/angular-cli/issues/25924
54+
ɵresetCompiledComponents?.();
55+
5056
const platformProviders: StaticProvider[] = [
5157
{
5258
provide: ɵSERVER_CONTEXT,

0 commit comments

Comments
 (0)