
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
angular-zoneless
Advanced tools
Angular 16 introduces a new and better server side rendering with hydration support.
To render server-side without the hassle of using Zone.js, we need a custom Zone.js implementation that works different from hooking into the whole environment (like setTimeout, async to generator, etc).
Why is Zone.js problematic?
I prefer to have my Node.js instance clean and not polluted with Zone.js, so I can use async/await without any problems. This package provides such a Zone.js implementation, making SSR and hydration possible without Zone.js. It comes with some limitations though.
It hooks automatically into all lifecycle hooks and makes Zone.js aware of them.
This means, you can use async ngOnInit
and the SSR as well as hydration on the client wait
correctly until your init procedure is done (like loading data from the server).
import { withZoneLessModule } from 'angular-zoneless';
import { ApplicationConfig } from '@angular/core';
import { provideClientHydration } from "@angular/platform-browser";
export const appConfig: ApplicationConfig = {
providers: [
provideClientHydration(),
withZoneLessModule(),
// ...
]
};
Or use ZoneLessModule.forRoot()
for non-standalone applications.
Then make sure to use async
in your lifecycle hooks:
@Component({
selector: 'app-root',
template: `
<h1>{{ title }}</h1>
`
})
export class AppComponent implements OnInit {
title: string;
async ngOnInit() {
this.title = await this.getTitle();
}
async getTitle() {
return 'Hello World';
}
}
Once the Promise of ngOnInit
is resolved in all components, the ZoneJS implementation calls onStable,
which allows hydration and SSR to finish.
This works since this module hooks into all async methods of your components. By doing that it knows about all the called initial async methods and can wait for them to finish. Once finished, it calls onStable and allows hydration and SSR to finish.
This works with activatedRoute as well, but make sure to use it in your ngOnInit method, not in the constructor:
@Component({
selector: 'app-root',
template: `
<h1>{{ title }}</h1>
`
})
export class AppComponent implements OnInit {
title: string;
constructor(private activatedRoute: ActivatedRoute) {
}
ngOnInit() {
this.activatedRoute.params.subscribe(params => {
this.load(params.id);
});
}
async load(id: string) {
this.title = await this.getTitle(id);
}
}
If you render anything dynamic like (click)="load()" and load
is async, you need either use RxJS with async pipe to render dynamically,
or make sure load
calls ChangeDetectorRef.detectChanges()
once the data is loaded.
Soon, you also will be able to use signals to make this easier.
@Component({
selector: 'app-root',
template: `
<h1>{{ title }}</h1>
<button (click)="load()">Load</button>
`
})
export class AppComponent implements OnInit {
title: string;
constructor(private cd: ChangeDetectorRef) {
}
async ngOnInit() {
this.title = await this.getTitle();
}
async getTitle() {
return 'Hello World';
}
async load() {
this.title = await this.getTitle();
this.cd.detectChanges();
}
}
FAQs
Library to support Zoneless Angular with Hydration and SSR
The npm package angular-zoneless receives a total of 0 weekly downloads. As such, angular-zoneless popularity was classified as not popular.
We found that angular-zoneless demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.