Skip to content

Server-side running code throws an error if the provideZoneChangeDetection is used #50801

Closed

Description

@arturovt

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

The code which is running in the Node.js throws an error Cannot read properties of undefined (reading 'call') when the provideZoneChangeDetection is called in ApplicationConfig.providers.

This happens because the NgZone delays change detection to coalesce event tasks (happened within the same animation frame) or multiple ngZone.run calls in a row. The change detection is delayed through the native requestAnimationFrame which isn't available in the Node.js.

Quick solution:

// server.ts
import 'zone.js/node';

globalThis.requestAnimationFrame =
  // @ts-ignore
  setTimeout[Zone.__symbol__('OriginalDelegate')];
globalThis.cancelAnimationFrame =
  // @ts-ignore
  setTimeout[Zone.__symbol__('OriginalDelegate')];

Please provide a link to a minimal reproduction of the bug

https://github.com/arturovt/provide-cd-ssr

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 16.1.0
Node: 16.18.0
Package Manager: yarn 1.22.19
OS: linux x64

Angular: 16.1.2
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1601.0
@angular-devkit/build-angular   16.1.0
@angular-devkit/core            16.1.0
@angular-devkit/schematics      16.1.0
@angular/cli                    16.1.0
@nguniversal/builders           16.1.0
@nguniversal/express-engine     16.1.0
@schematics/angular             16.1.0
rxjs                            7.8.1
typescript                      5.1.3

Anything else?

No response

Activity

  1. added
    area: server
    on Jun 21, 2023
  2. added this to the needsTriage milestone on Jun 21, 2023
  3. AndrewKushnir commented on Jun 21, 2023

    @AndrewKushnir
    Contributor

    @JiaLiPassion @alan-agius4 what do you think about using setTimeout as a replacement for requestAnimationFrame for Node.js environment in the mentioned scenario? I think we can also consider using queueMicrotask function.

  4. arturovt commented on Jun 21, 2023

    @arturovt
    ContributorAuthor

    I don't think there should be any task scheduling on the Node.js side since the current implementation (with no task scheduling) has existed until now. First of all, there're no event tasks on the Node.js side until we dispatch them manually through el.dispatchEvent, compared to how event tasks are invoked in the browser (they're "classically" invoked by browsers as a reaction to user actions or also dispatched manually). This means there might be only multiple ngZone.run calls in a row which may be coalesced. Bug again, there's no reason to coalesce those calls on the Node.js side since there will be no "frame drop". Having any task scheduling on the server side may also affect the time the response comes back.

    As I mentioned in one of my comments in the linked PR, we may keep provideZoneChangeDetection compatible with both environments but provide explicit zone options which default to false when the platform is server:

    inject(PLATFORM_ID) === 'browser' ? getNgZoneOptions(options) : getNgZoneOptions({ eventCoalescing: false, runCoalescing: false });
  5. AndrewKushnir commented on Jun 21, 2023

    @AndrewKushnir
    Contributor

    @arturovt thanks for the comment, I do agree that the mentioned change might be a possible solution. I think what'd be great to preserve though is the consistency of execution paths on the client and on the server, so that operations happen in the same order (otherwise it may lead to some subtle bugs in the code).

  6. added
    P3
    on Jun 22, 2023
  7. modified the milestones: needsTriage, Backlog on Jun 22, 2023
  8. AndrewKushnir commented on Jun 22, 2023

    @AndrewKushnir
    Contributor

    @arturovt thanks again for reporting the issue. We had a discussion with the team and the decision is to use setTimeout on the server when no requestAnimationFrame is available, as you proposed. Please let us know if you might be interested in creating a PR with the fix. Thank you.

  9. arturovt commented on Jun 22, 2023

    @arturovt
    ContributorAuthor

    Sure, I’ll make a PR if you don’t mind today.

  10. AndrewKushnir commented on Jun 22, 2023

    @AndrewKushnir
    Contributor

    Sure, I’ll make a PR if you don’t mind today.

    Thank you 👍

  11. angular-automatic-lock-bot commented on Jul 31, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: serverstate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions