Skip to content

Commit f6ed31f

Browse files
committed
fix(@schematics/angular): wrap bootstrapping code in an HMR compatible manner
With this change we update the universal schematic bootstrap code to handle HMR properly. Previously, the bootstrapping code was called only on `DOMContentLoaded` which is not triggered when running in HMR. Closes #21932 (cherry picked from commit 08687d1)
1 parent d2e24d3 commit f6ed31f

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/schematics/angular/universal/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,17 @@ function wrapBootstrapCall(mainFile: string): Rule {
141141
// indent contents
142142
const triviaWidth = bootstrapCall.getLeadingTriviaWidth();
143143
const beforeText =
144-
`document.addEventListener('DOMContentLoaded', () => {\n` +
145-
' '.repeat(triviaWidth > 2 ? triviaWidth + 1 : triviaWidth);
146-
const afterText = `\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}});`;
144+
`function bootstrap() {\n` + ' '.repeat(triviaWidth > 2 ? triviaWidth + 1 : triviaWidth);
145+
const afterText =
146+
`\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}};\n` +
147+
`
148+
149+
if (document.readyState === 'complete') {
150+
bootstrap();
151+
} else {
152+
document.addEventListener('DOMContentLoaded', bootstrap);
153+
}
154+
`;
147155

148156
// in some cases we need to cater for a trailing semicolon such as;
149157
// bootstrap().catch(err => console.log(err));
@@ -236,8 +244,8 @@ export default function (options: UniversalOptions): Rule {
236244
throw targetBuildNotFoundError();
237245
}
238246

239-
const clientBuildOptions = ((clientBuildTarget.options ||
240-
{}) as unknown) as BrowserBuilderOptions;
247+
const clientBuildOptions = (clientBuildTarget.options ||
248+
{}) as unknown as BrowserBuilderOptions;
241249

242250
const clientTsConfig = normalize(clientBuildOptions.tsConfig);
243251
const tsConfigExtends = basename(clientTsConfig);

packages/schematics/angular/universal/index_spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe('Universal Schematic', () => {
191191
.toPromise();
192192
const filePath = '/projects/bar/src/main.ts';
193193
const contents = tree.readContent(filePath);
194-
expect(contents).toMatch(/document.addEventListener\('DOMContentLoaded', \(\) => {/);
194+
expect(contents).toContain(`document.addEventListener('DOMContentLoaded', bootstrap);`);
195195
});
196196

197197
it('should wrap the bootstrap declaration in a DOMContentLoaded event handler', async () => {
@@ -221,9 +221,7 @@ describe('Universal Schematic', () => {
221221
.runSchematicAsync('universal', defaultOptions, appTree)
222222
.toPromise();
223223
const contents = tree.readContent(filePath);
224-
expect(contents).toMatch(
225-
/document.addEventListener\('DOMContentLoaded', \(\) => {[\n\r\s]+bootstrap\(\)/,
226-
);
224+
expect(contents).toContain(`document.addEventListener('DOMContentLoaded', bootstrap);`);
227225
});
228226

229227
it('should install npm dependencies', async () => {

0 commit comments

Comments
 (0)