You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
@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.
constdoc=inject(DOCUMENT);constevent=newCustomEvent('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);
@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.
angular-automatic-lock-bot commented on Nov 21, 2025
Activity
JoostK commentedon Oct 1, 2025
I'd request a bit more information here, ideally a proper reproduction that showcases the issue they're experiencing.
JeanMeche commentedon Oct 1, 2025
cc @AlemTuzlak
AlemTuzlak commentedon Oct 1, 2025
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.
JeanMeche commentedon Oct 1, 2025
Angular does DOM emulation with a fork of Domino: https://github.com/angular/domino
alan-agius4 commentedon Oct 13, 2025
@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
Documentinjection token to get the "Window" which will result in the event to also work in SSR.alan-agius4 commentedon Oct 21, 2025
Closing as per above.
AlemTuzlak commentedon Oct 21, 2025
@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.
angular-automatic-lock-bot commentedon Nov 21, 2025
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.