Sub topic 14
Checking third-party library compatibility
This chapter assumes you can build standalone components, use signals, and know how OnPush change detection works. It also assumes you have read the chapters on triggers and markForCheck. Your own code is only part of a zoneless migration. Libraries you depend on can also rely on Zone.js, and you need to find those before you remove it.
Why libraries break without zones
Zone.js patches browser APIs such as setTimeout, event listeners and Promise. A library written for a zone-based app can change state inside a timer or callback and never tell Angular. In a zone-based app this works because the zone reports that the task finished, and Angular then runs change detection.
Without zones, nothing reports that. The library's component updates its own fields and the view stays stale. Libraries fail in a few recurring ways:
- Components use the default change detection strategy and mutate plain fields from async callbacks.
- Code calls
NgZone.onStable,NgZone.onMicrotaskEmptyorNgZone.run()and expects them to signal when Angular has finished work. - Code depends on
NgZone.isInAngularZone()to decide where to run. - Code imports
zone.jsdirectly or usesZone.current, which fails when Zone.js is absent.
Checking a library
Work through these checks in order, from cheapest to most expensive.
- Read the library's documentation and changelog for the words "zoneless" or "zone-free".
- Search the issue tracker for the same terms. An open issue with no reply is a warning sign.
- Check which Angular versions the library supports. Support usually arrives together with Angular's own zoneless support.
- Search the library source for the zone APIs listed in the table below.
- Run the library's components in a zoneless test and in a small spike app.
grep -rnE "NgZone|Zone\.current|onMicrotaskEmpty|onStable|isInAngularZone" node_modules/some-library/fesm2022Compiled output is a rough guide. A match does not prove a problem, because some libraries use runOutsideAngular only for performance. A missing match does not prove safety either. The reliable check is to run the component.
| Finding in the library | Zoneless risk | What to do |
|---|---|---|
| `runOutsideAngular` only | Low | It still runs your callback, so it is usually harmless. |
| `NgZone.run()` to re-enter Angular | Low to medium | Updates may not be scheduled unless state is in signals or `markForCheck` is called. |
| `onMicrotaskEmpty` or `onStable` | High | These events do not fire in a zoneless app. Look for an alternative or a newer version. |
| Direct `Zone` references | High | Fails when `zone.js` is not loaded. |
| Default change detection with plain fields | Medium | The component may not refresh after async work. |
Testing a library in a zoneless spike
Enable zoneless mode in a test and exercise the library component the way a user would. Use fixture.whenStable() so the scheduled change detection has run before you assert.
import { TestBed } from '@angular/core/testing';
import { provideZonelessChangeDetection } from '@angular/core';
import { SomeLibraryWidget } from 'some-library';
describe('SomeLibraryWidget without zones', () => {
it('renders after async data arrives', async () => {
TestBed.configureTestingModule({
providers: [provideZonelessChangeDetection()],
});
const fixture = TestBed.createComponent(SomeLibraryWidget);
await fixture.whenStable();
expect(fixture.nativeElement.textContent).not.toBe('');
});
});If the widget fails here, it will fail in your app as well.
Options when a library is not compatible
flowchart TDUse this decision order instead of a diagram you cannot read. First, upgrade to a version that supports zoneless mode. Second, wrap the library in your own component that holds its output in a signal or calls markForCheck() after the library's callbacks. Third, replace the library. Fourth, delay the migration for the affected part of the app.
A wrapper is often enough. The library keeps its own internal logic, and your component turns its events into signals:
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-wrapped-widget',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>Selected: {{ selected() }}</p>`,
})
export class WrappedWidget {
protected readonly selected = signal('none');
onLibrarySelect(value: string) {
this.selected.set(value);
}
}The wrapper only fixes your own template. If the library renders its own DOM internally and never notifies Angular, a wrapper cannot repair that. Test the library's visible output directly.
Exercise
A library's search component uses default change detection and updates a plain results field inside a setTimeout callback. It works in your zone-based app. Why does it stop updating in a zoneless app, and what are two ways to deal with it?
Show answer
Zone.js used to notice the finished timer and trigger change detection. Without it, nothing tells Angular that results changed. You can upgrade to a library version that uses signals or calls markForCheck(). You can also wrap the component and hold its output in a signal, or replace the library.