Universal: Universal with ngZone noop

Created on 3 Oct 2020  路  5Comments  路  Source: angular/universal

Hello. on my browser app i use ngZone noop in boostraping appModule
How can i achieve the same for universal ssr? i didnt see that option at CommonEngine

common feature

Most helpful comment

well, i made some custom changes in CommonEngine and have now universal without zone :dancers:
ill share it later as plugin, here

All 5 comments

Hi @moczix, this is not possible. I'll add this a feature request.

well, i made some custom changes in CommonEngine and have now universal without zone :dancers:
ill share it later as plugin, here

It's possible. This is our render function:

async function renderModuleWithoutZone(app: Type<unknown>, platformRef: PlatformRef): Promise<string> {
    const moduleRef = await platformRef.bootstrapModule(app, {ngZone: 'noop'});

    // leapfrog the Promises scheduled by @angular/router
    await new Promise((resolve) => setImmediate(resolve));

    const platformState = platformRef.injector.get(PlatformState);

    const callbacks = moduleRef.injector.get(BEFORE_APP_SERIALIZED, undefined);
    if (callbacks) {
        await Promise.all(
            callbacks
                .map((callback) => callback())
                .filter((result?: any): result is Promise<unknown> => typeof result?.then === 'function'),
        );
    }

    const result = platformState.renderToString();
    moduleRef.destroy();

    return result;
}

And zone.js catches uncaught errors by default instead of crashing the server. So if you are used to that, you should do that too

process.on('uncaughtException', function (error) {
    yourLoggerCallbackOfChoice(error);
});

We don't allow timeouts or really any async in ssr. Thats why we can just do one setImmediate, then render => destroy. We have a set of unit tests that run all routes and fails if e.g. a setTimeout was done. If you do async operations, you have to add some kind of reference count and wait for all async to settle until you render & destroy - as that is something that zone.js did for you.

@sod this is bad. I mean it could work, but it is not the angular way its look more like hack

i forgot to post my way to solve this, so this is the way:
https://github.com/moczix/angular-universal-zoneless

Angular team: if you feel that this is good, you can include this in future universal release, just let me know

Was this page helpful?
0 / 5 - 0 ratings