Skip to content

setTimeout in interceptor causes error with/in EventReplay #56423

Closed

Description

@phihochzwei

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

platform-browser

Is this a regression?

No

Description

When using EventReplay via withEventReplay(), we receive an error inside one of our interceptors, in the marked line in the interceptor we use 'setTimeout'

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

`TypeError: Cannot read properties of undefined (reading '_ejsa')
    at getJsactionData (core.mjs:36732:22)
    at initEventReplay (core.mjs:36740:31)
    at core.mjs:36716:29
    at _ZoneDelegate.invoke (zone.js:368:26)
    at Object.onInvoke (core.mjs:15898:33)
    at _ZoneDelegate.invoke (zone.js:367:52)
    at Zone.run (zone.js:129:43)
    at zone.js:1257:36
    at _ZoneDelegate.invokeTask (zone.js:402:31)
    at core.mjs:15583:55
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:15583:36)
    at _ZoneDelegate.invokeTask (zone.js:401:60)
    at Object.onInvokeTask (core.mjs:15885:33)
    at _ZoneDelegate.invokeTask (zone.js:401:60)
    at Zone.runTask (zone.js:173:47)`

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

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 18.0.3
Node: 22.2.0
Package Manager: npm 10.7.0
OS: darwin arm64

Angular: 18.0.3
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, google-maps, language-service, material
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.3
@angular-devkit/build-angular   18.0.3
@angular-devkit/core            18.0.3
@angular-devkit/schematics      18.0.3
@schematics/angular             18.0.3
ng-packagr                      18.0.0
rxjs                            7.5.7
typescript                      5.4.3
webpack                         5.91.0
zone.js                         0.14.3

Anything else?

No response

Activity

  1. added
    needs reproduction
    on Jun 13, 2024
  2. Fluccioni commented on Jun 14, 2024

    @Fluccioni

    I have currently the same TypeError, but I'm not sure it has something to do with the use of setTimeout.

    I came across this comment in the initEventReplay function saying we may encounter undefined reading 'ngContracts'.

    If it is the case, therefore the const container will be undefined too and the error will be thrown on the invocation of getJsactionData.

    // TODO: Upstream this back into event-dispatch.
    function getJsactionData(container) {
        return container._ejsa;
    }
    
    const initEventReplay = (eventDelegation, injector) => {
        const appId = injector.get(APP_ID);
        // This is set in packages/platform-server/src/utils.ts
        // Note: globalThis[CONTRACT_PROPERTY] may be undefined in case Event Replay feature
        // is enabled, but there are no events configured in an application.
        const container = globalThis[CONTRACT_PROPERTY]?.[appId];
        const earlyJsactionData = getJsactionData(container);
        ...

    The problem seems to be the bundle jsaction contract script not correctly inlined during the html generation, which might be the case while using the "old" browser builder instead of esbuild from what I saw

  3. added
    area: core
    on Jun 18, 2024
  4. modified the milestone: Backlog on Jun 18, 2024
  5. added
    P3
    and removed
    needs reproduction
    on Jun 18, 2024
  6. AndrewKushnir commented on Jun 19, 2024

    @AndrewKushnir
    Contributor

    Thanks for reporting this issue. I was able to reproduce it for cases when there are no events registered in an application, but Event Replay feature is enabled. I've created PR #56509 with the fix.

    The problem seems to be the bundle jsaction contract script not correctly inlined during the html generation, which might be the case while using the "old" browser builder instead of esbuild from what I saw

    @Fluccioni the mentioned PR would likely resolve that problem as well (there will be no JS exceptions), but we may consider adding some logic to detect those cases and add a warning. Could you please create a minimal repro (in a form of GitHub repository), so that we can perform further investigation to see if we can detect those cases?

  7. added a commit that references this issue on Jun 20, 2024
    83ffa94
  8. added a commit that references this issue on Jun 20, 2024
    bf6df6f
  9. AndrewKushnir commented on Jun 20, 2024

    @AndrewKushnir
    Contributor

    @Fluccioni @phihochzwei just wanted to share a quick update: the fix was merged and released to NPM. Please try updating to Angular v18.0.4 and let us know if the problem still exists. Thank you.

  10. Fluccioni commented on Jun 21, 2024

    @Fluccioni

    @AndrewKushnir Sorry I didn't had time to setup a quick repro yet with custom builders, thanks for the fix though indeed there is no more the exception thrown w/ 18.0.4

  11. angular-automatic-lock-bot commented on Jul 22, 2024

    @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 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: corecore: event dispatchstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions