Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-zoneless

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-zoneless

Library to support Zoneless Angular with Hydration and SSR

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Angular Zoneless

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?

  • It hooks into the whole environment, which means it hooks into setTimeout, setInterval, etc.
  • It hooks into async/await and converts it to generators, because Zone.js does not support native async/await.
  • It does not support async lifecycle hooks like ngOnInit, which means you can't use async/await in ngOnInit.
  • It makes everything slower and pollutes the stack trace with Zone.js internals.
  • It forces you do compile the whole application with all dependencies via Angulars compiler (to replace async with generators), making it unnecessary complex and hard to replace providers in the server side rendering process.

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).

How to set up

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.

How to use

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

Package last updated on 17 May 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc