Skip to content

Commit ff0ccd0

Browse files
committed
feat(@schematics/angular): Add global error listeners to new app generation
This commit adds the provider for global error listener in the browser. This is particularly useful for zoneless apps but also useful for Zone-based applications when errors happen outside the Angular zone.
1 parent 4121c8f commit ff0ccd0

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

Diff for: package.json

+13-13
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@
4646
},
4747
"homepage": "https://github.com/angular/angular-cli",
4848
"devDependencies": {
49-
"@angular/animations": "20.0.0-next.6",
50-
"@angular/cdk": "20.0.0-next.6",
51-
"@angular/common": "20.0.0-next.6",
52-
"@angular/compiler": "20.0.0-next.6",
53-
"@angular/compiler-cli": "20.0.0-next.6",
54-
"@angular/core": "20.0.0-next.6",
55-
"@angular/forms": "20.0.0-next.6",
56-
"@angular/localize": "20.0.0-next.6",
57-
"@angular/material": "20.0.0-next.6",
49+
"@angular/animations": "20.0.0-next.7",
50+
"@angular/cdk": "20.0.0-next.7",
51+
"@angular/common": "20.0.0-next.7",
52+
"@angular/compiler": "20.0.0-next.7",
53+
"@angular/compiler-cli": "20.0.0-next.7",
54+
"@angular/core": "20.0.0-next.7",
55+
"@angular/forms": "20.0.0-next.7",
56+
"@angular/localize": "20.0.0-next.7",
57+
"@angular/material": "20.0.0-next.7",
5858
"@angular/ng-dev": "https://github.com/angular/dev-infra-private-ng-dev-builds.git#a9e4627eb45742d81068087a5b57a676104fe053",
59-
"@angular/platform-browser": "20.0.0-next.6",
60-
"@angular/platform-server": "20.0.0-next.6",
61-
"@angular/router": "20.0.0-next.6",
62-
"@angular/service-worker": "20.0.0-next.6",
59+
"@angular/platform-browser": "20.0.0-next.7",
60+
"@angular/platform-server": "20.0.0-next.7",
61+
"@angular/router": "20.0.0-next.7",
62+
"@angular/service-worker": "20.0.0-next.7",
6363
"@bazel/bazelisk": "1.26.0",
6464
"@bazel/buildifier": "8.0.3",
6565
"@eslint/compat": "1.2.8",

Diff for: packages/schematics/angular/application/files/module-files/src/app/app-module.ts.template

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule<% if(experimentalZoneless) { %>, provideExperimentalZonelessChangeDetection<% } %> } from '@angular/core';
1+
import { NgModule, provideBrowserGlobalErrorListeners<% if(experimentalZoneless) { %>, provideExperimentalZonelessChangeDetection<% } %> } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
33
<% if (routing) { %>
44
import { AppRoutingModule } from './app-routing-module';<% } %>
@@ -12,7 +12,10 @@ import { App } from './app';
1212
BrowserModule<% if (routing) { %>,
1313
AppRoutingModule<% } %>
1414
],
15-
providers: [<% if (experimentalZoneless) { %>provideExperimentalZonelessChangeDetection()<% } %>],
15+
providers: [
16+
provideBrowserGlobalErrorListeners(),
17+
<% if (experimentalZoneless) { %>provideExperimentalZonelessChangeDetection()<% } %>
18+
],
1619
bootstrap: [App]
1720
})
1821
export class AppModule { }
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { ApplicationConfig, <% if(!experimentalZoneless) { %>provideZoneChangeDetection<% } else { %>provideExperimentalZonelessChangeDetection<% } %> } from '@angular/core';<% if (routing) { %>
1+
import { ApplicationConfig, provideBrowserGlobalErrorListeners, <% if(!experimentalZoneless) { %>provideZoneChangeDetection<% } else { %>provideExperimentalZonelessChangeDetection<% } %> } from '@angular/core';<% if (routing) { %>
22
import { provideRouter } from '@angular/router';
33

44
import { routes } from './app.routes';<% } %>
55

66
export const appConfig: ApplicationConfig = {
7-
providers: [<% if(experimentalZoneless) { %>provideExperimentalZonelessChangeDetection()<% } else { %>provideZoneChangeDetection({ eventCoalescing: true })<% } %><% if (routing) {%>, provideRouter(routes)<% } %>]
7+
providers: [
8+
provideBrowserGlobalErrorListeners(),
9+
<% if(experimentalZoneless) { %>provideExperimentalZonelessChangeDetection()<% } else { %>provideZoneChangeDetection({ eventCoalescing: true })<% } %>,
10+
<% if (routing) {%>provideRouter(routes),<% } %>]
811
};

Diff for: packages/schematics/angular/service-worker/index_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('Service Worker Schematic', () => {
139139
const tree = await schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
140140
const content = tree.readContent('/projects/bar/src/app/app.config.ts');
141141
expect(content).toContain(
142-
`import { ApplicationConfig, provideZoneChangeDetection, isDevMode } from '@angular/core';`,
142+
`import { ApplicationConfig, provideBrowserGlobalErrorListeners, provideZoneChangeDetection, isDevMode } from '@angular/core';`,
143143
);
144144
});
145145

Diff for: packages/schematics/angular/utility/standalone/rules_spec.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@ describe('standalone utilities', () => {
423423
const content = readFile('app/app-module.ts');
424424

425425
assertContains(content, `import { SOME_TOKEN } from '@my/module';`);
426-
assertContains(content, `providers: [{ provide: SOME_TOKEN, useValue: 123 }]`);
426+
assertContains(
427+
content,
428+
`providers: [provideBrowserGlobalErrorListeners(),{ provide: SOME_TOKEN, useValue: 123 },]`,
429+
);
427430
});
428431

429432
it('should add a root provider to a standalone app', async () => {
@@ -442,7 +445,11 @@ describe('standalone utilities', () => {
442445
assertContains(content, `import { provideModule } from '@my/module';`);
443446
assertContains(
444447
content,
445-
`providers: [provideZoneChangeDetection({ eventCoalescing:true }),provideModule([])]`,
448+
`providers: [
449+
provideBrowserGlobalErrorListeners(),
450+
provideZoneChangeDetection({ eventCoalescing:true }),
451+
provideModule([]),
452+
]`,
446453
);
447454
});
448455

@@ -453,11 +460,12 @@ describe('standalone utilities', () => {
453460
host.overwrite(
454461
getPathWithinProject(configPath),
455462
`
456-
import { ApplicationConfig } from '@angular/core';
463+
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
457464
import { provideRouter } from '@angular/router';
458465
459466
export const appConfig: ApplicationConfig = {
460467
providers: [
468+
provideBrowserGlobalErrorListeners(),
461469
provideRouter([]),
462470
]
463471
};
@@ -474,7 +482,10 @@ describe('standalone utilities', () => {
474482

475483
const content = readFile('app/app.config.ts');
476484
assertContains(content, `import { provideModule } from '@my/module';`);
477-
assertContains(content, `providers: [provideRouter([]),provideModule([]),]`);
485+
assertContains(
486+
content,
487+
`providers: [provideBrowserGlobalErrorListeners(), provideRouter([]),provideModule([]),]`,
488+
);
478489
});
479490
});
480491
});

0 commit comments

Comments
 (0)