Sub topic 6
Enabling zoneless mode and removing zone.js
This chapter assumes you can build standalone Angular components, use signal and computed, and understand default and OnPush change detection. Here you will switch an application to zoneless change detection and remove Zone.js from the build.
What changes when you go zoneless
In a zone-based app, Zone.js patches browser APIs such as setTimeout, event listeners and Promise. After each patched task finishes, NgZone tells Angular to run change detection. In zoneless mode Angular no longer relies on that signal. Instead, it schedules change detection when it gets an explicit notification, such as a signal change read by a template. The next chapter covers these notifications in detail.
Enabling zoneless mode takes two steps. First, you add a provider. Second, you remove Zone.js from the bundle.
Step 1: add the provider
Add provideZonelessChangeDetection() to the application providers.
import { bootstrapApplication } from '@angular/platform-browser';
import { provideZonelessChangeDetection } from '@angular/core';
import { App } from './app/app';
bootstrapApplication(App, {
providers: [provideZonelessChangeDetection()],
});Angular now uses its own scheduler instead of NgZone. If Zone.js is still loaded at this point, the app keeps working, because Zone.js simply has no effect on change detection. This lets you test the provider before you touch the build.
Step 2: remove zone.js from the build
Open angular.json. Under both the build and test targets, find the polyfills option and delete the "zone.js" entry. If nothing else is listed, the option becomes an empty list:
"polyfills": []Also delete any import 'zone.js' or import 'zone.js/testing' line in polyfills.ts or other files. After this, Zone.js is no longer in the bundle, and your initial download gets smaller.
Note that the test target has its own list. If you forget it, your tests still load Zone.js while the app does not, and the two environments behave differently.
A component that works without Zone.js
OnPush plus a signal is the pattern that works best. The template reads count(), so Angular knows to refresh the view when the signal changes.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<button (click)="increment()">Clicked {{ count() }} times</button>`,
})
export class Counter {
protected readonly count = signal(0);
increment() {
this.count.update((c) => c + 1);
}
}Using OnPush is not required in zoneless mode, but it makes components compatible with it and is a good habit.
Common errors
- Removing zone.js but not adding the provider. Angular then has no way to know when to run change detection, and Angular reports an error at startup in recent versions. Add the provider first.
- Forgetting the
testpolyfills. Tests then run with a different setup from the app. - Plain fields that change in timers or callbacks. A field like
seconds++insidesetIntervalno longer refreshes the view. Convert it to a signal, or callmarkForCheck(), which a later chapter covers. - Libraries that import
zone.jsthemselves. The bundle can still contain Zone.js even though your config no longer lists it. Search your build output if the size does not drop.
Exercise
You added provideZonelessChangeDetection() and deleted "zone.js" from the build polyfills in angular.json. The app works, but unit tests behave differently from the running app. What did you most likely miss, and what is the fix?
Show answer
You most likely left "zone.js" in the polyfills list of the test target (or an import of zone.js/testing in a test setup file). Remove it there too, and add provideZonelessChangeDetection() to the TestBed providers, so the tests run in the same mode as the app.