Skip to content

CustomEvent breaking SSR #64175

Closed as not planned
Closed as not planned

Description

@MarkTechson

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

Don't known / other

Is this a regression?

Yes

Description

Per a report from a developer online:

Just found out that CustomEvent doesn't work on SSR because it doesn't use standard API's to dispatch the events but rather Angular shims which don't support it.

Can we please have a look at this?

Context: https://x.com/AlemTuzlak/status/1970798138270347410

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


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


Anything else?

No response

Activity

  1. JoostK commented on Oct 1, 2025

    @JoostK
    Member

    I'd request a bit more information here, ideally a proper reproduction that showcases the issue they're experiencing.

  2. added this to the needsTriage milestone on Oct 1, 2025
  3. JeanMeche commented on Oct 1, 2025

    @JeanMeche
    Member
  4. AlemTuzlak commented on Oct 1, 2025

    @AlemTuzlak

    Just dispatch a CustomEvent on the window object with window.dispatchEvent, and it will crash. The server dies with an error that CustomEvent can't be dispatched.

    But to be completely honest, I wouldn't expect window to be defined at all on the server, so I'm not sure what the right approach is. I don't like shims, and I'd prefer the environment to reflect what it is, in this case, a server without a window.

  5. JeanMeche commented on Oct 1, 2025

    @JeanMeche
    Member

    Angular does DOM emulation with a fork of Domino: https://github.com/angular/domino

  6. alan-agius4 commented on Oct 13, 2025

    @alan-agius4
    Contributor

    @AlemTuzlak, to give you a precise solution, could you provide a bit more context about what you're trying to achieve with this event? For an SSR compliant application you typically need to guard browser-only APIs. The best way to handle this is either by checking with isPlatformBrowser or the recommended approach is to place browser-specific code in afterNextRender and afterEveryRender hooks.

    One other alternative would be to use the Document injection token to get the "Window" which will result in the event to also work in SSR.

    const doc = inject(DOCUMENT);
    const event = new CustomEvent('myCustomEvent', { detail: { data: 'hello' } });
    // doc.defaultView can be null but it will be defined in SSR.
    doc.defaultView?.addEventListener('myCustomEvent', (e) => console.log(e));
    doc.defaultView?.dispatchEvent(event);
  7. added
    needs: clarificationThis issue needs additional clarification from the reporter before the team can investigate.
    on Oct 13, 2025
  8. alan-agius4 commented on Oct 21, 2025

    @alan-agius4
    Contributor

    Closing as per above.

  9. AlemTuzlak commented on Oct 21, 2025

    @AlemTuzlak

    @alan-agius4 I've fixed it already on our end by specifically checking if the code is running in angular, the issue I had was that Angular was "lying" about the environment, it has the window object defined on the server but uses an emulated version of that as Jean mentioned above. I can't use anything from Angular as framework agnostic software can't be framework agnostic if it imports methods from Angular, I just check if the actual normal dispatchEvent behavior works and then use a shim designed for angular by creating a normal event.

    Like mentioned in my tweet, emulating stuff that doesn't exist in places it shouldn't exist is a very frustrating experience that usually leads to a lot of external things breaking due to detecting things that shouldn't be there.

  10. angular-automatic-lock-bot commented on Nov 21, 2025

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

  11. locked and limited conversation to collaborators on Nov 21, 2025
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

    area: serverIssues related to server-side renderingneeds: clarificationThis issue needs additional clarification from the reporter before the team can investigate.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions