File tree Expand file tree Collapse file tree 2 files changed +15
-9
lines changed
packages/schematics/angular/universal Expand file tree Collapse file tree 2 files changed +15
-9
lines changed Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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 ( / d o c u m e n t .a d d E v e n t L i s t e n e r \ (' D O M C o n t e n t L o a d e d ' , \( \) = > { / ) ;
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- / d o c u m e n t .a d d E v e n t L i s t e n e r \( ' D O M C o n t e n t L o a d e d ' , \( \) = > { [ \n \r \s ] + b o o t s t r a p \( \) / ,
226- ) ;
224+ expect ( contents ) . toContain ( `document.addEventListener('DOMContentLoaded', bootstrap);` ) ;
227225 } ) ;
228226
229227 it ( 'should install npm dependencies' , async ( ) => {
You can’t perform that action at this time.
0 commit comments