Skip to content

Commit bf6df6f

Browse files
committed
fix(core): do not activate event replay when no events are registered (#56509)
This commit adds extra checks to handle a situation when an application has no events configured, but the Event Replay feature was enabled. This situation can happen when some routes in an application are mostly static, when other routes are more interactive. Resolves #56423. PR Close #56509
1 parent 86bcfd3 commit bf6df6f

2 files changed

Lines changed: 81 additions & 15 deletions

File tree

‎packages/core/src/hydration/event_replay.ts‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import {CLEANUP, LView, TView} from '../render3/interfaces/view';
2626
import {isPlatformBrowser} from '../render3/util/misc_utils';
2727
import {unwrapRNode} from '../render3/util/view_utils';
2828

29-
import {IS_EVENT_REPLAY_ENABLED, IS_GLOBAL_EVENT_DELEGATION_ENABLED} from './tokens';
29+
import {
30+
EVENT_REPLAY_ENABLED_DEFAULT,
31+
IS_EVENT_REPLAY_ENABLED,
32+
IS_GLOBAL_EVENT_DELEGATION_ENABLED,
33+
} from './tokens';
3034
import {
3135
GlobalEventDelegation,
3236
sharedStashFunction,
@@ -50,6 +54,16 @@ function isGlobalEventDelegationEnabled(injector: Injector) {
5054
return injector.get(IS_GLOBAL_EVENT_DELEGATION_ENABLED, false);
5155
}
5256

57+
/**
58+
* Determines whether Event Replay feature should be activated on the client.
59+
*/
60+
function shouldEnableEventReplay(injector: Injector) {
61+
return (
62+
injector.get(IS_EVENT_REPLAY_ENABLED, EVENT_REPLAY_ENABLED_DEFAULT) &&
63+
!isGlobalEventDelegationEnabled(injector)
64+
);
65+
}
66+
5367
/**
5468
* Returns a set of providers required to setup support for event replay.
5569
* Requires hydration to be enabled separately.
@@ -58,19 +72,28 @@ export function withEventReplay(): Provider[] {
5872
return [
5973
{
6074
provide: IS_EVENT_REPLAY_ENABLED,
61-
useValue: true,
75+
useFactory: () => {
76+
let isEnabled = true;
77+
if (isPlatformBrowser()) {
78+
// Note: globalThis[CONTRACT_PROPERTY] may be undefined in case Event Replay feature
79+
// is enabled, but there are no events configured in this application, in which case
80+
// we don't activate this feature, since there are no events to replay.
81+
const appId = inject(APP_ID);
82+
isEnabled = !!globalThis[CONTRACT_PROPERTY]?.[appId];
83+
}
84+
return isEnabled;
85+
},
6286
},
6387
{
6488
provide: ENVIRONMENT_INITIALIZER,
6589
useValue: () => {
6690
const injector = inject(Injector);
67-
if (isGlobalEventDelegationEnabled(injector)) {
68-
return;
91+
if (isPlatformBrowser(injector) && shouldEnableEventReplay(injector)) {
92+
setStashFn((rEl: RElement, eventName: string, listenerFn: VoidFunction) => {
93+
sharedStashFunction(rEl, eventName, listenerFn);
94+
jsactionSet.add(rEl as unknown as Element);
95+
});
6996
}
70-
setStashFn((rEl: RElement, eventName: string, listenerFn: VoidFunction) => {
71-
sharedStashFunction(rEl, eventName, listenerFn);
72-
jsactionSet.add(rEl as unknown as Element);
73-
});
7497
},
7598
multi: true,
7699
},
@@ -81,13 +104,14 @@ export function withEventReplay(): Provider[] {
81104
const injector = inject(Injector);
82105
const appRef = inject(ApplicationRef);
83106
return () => {
107+
if (!shouldEnableEventReplay(injector)) {
108+
return;
109+
}
110+
84111
// Kick off event replay logic once hydration for the initial part
85112
// of the application is completed. This timing is similar to the unclaimed
86113
// dehydrated views cleanup timing.
87114
whenStable(appRef).then(() => {
88-
if (isGlobalEventDelegationEnabled(injector)) {
89-
return;
90-
}
91115
const globalEventDelegation = injector.get(GlobalEventDelegation);
92116
initEventReplay(globalEventDelegation, injector);
93117
jsactionSet.forEach(removeListeners);
@@ -112,8 +136,6 @@ function getJsactionData(container: EarlyJsactionDataContainer) {
112136
const initEventReplay = (eventDelegation: GlobalEventDelegation, injector: Injector) => {
113137
const appId = injector.get(APP_ID);
114138
// This is set in packages/platform-server/src/utils.ts
115-
// Note: globalThis[CONTRACT_PROPERTY] may be undefined in case Event Replay feature
116-
// is enabled, but there are no events configured in an application.
117139
const container = globalThis[CONTRACT_PROPERTY]?.[appId];
118140
const earlyJsactionData = getJsactionData(container)!;
119141
const eventContract = (eventDelegation.eventContract = new EventContract(

‎packages/platform-server/test/event_replay_spec.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
*/
88

99
import {DOCUMENT} from '@angular/common';
10-
import {Component, destroyPlatform, getPlatform, Type} from '@angular/core';
10+
import {
11+
Component,
12+
destroyPlatform,
13+
ErrorHandler,
14+
getPlatform,
15+
PLATFORM_ID,
16+
Type,
17+
} from '@angular/core';
1118
import {TestBed} from '@angular/core/testing';
1219
import {
1320
withEventReplay,
@@ -19,7 +26,14 @@ import {provideServerRendering} from '../public_api';
1926
import {EVENT_DISPATCH_SCRIPT_ID, renderApplication} from '../src/utils';
2027
import {EventPhase} from '@angular/core/primitives/event-dispatch';
2128

22-
import {getAppContents, hydrate, render as renderHtml, resetTViewsFor} from './dom_utils';
29+
import {
30+
getAppContents,
31+
hydrate,
32+
renderAndHydrate,
33+
render as renderHtml,
34+
resetTViewsFor,
35+
} from './dom_utils';
36+
import {CONTRACT_PROPERTY} from '@angular/core/src/hydration/event_replay';
2337

2438
/**
2539
* Represents the <script> tag added by the build process to inject
@@ -38,6 +52,24 @@ function hasJSActionAttrs(content: string) {
3852
return content.includes('jsaction="');
3953
}
4054

55+
/**
56+
* Enables strict error handler that fails a test
57+
* if there was an error reported to the ErrorHandler.
58+
*/
59+
function withStrictErrorHandler() {
60+
class StrictErrorHandler extends ErrorHandler {
61+
override handleError(error: any): void {
62+
fail(error);
63+
}
64+
}
65+
return [
66+
{
67+
provide: ErrorHandler,
68+
useClass: StrictErrorHandler,
69+
},
70+
];
71+
}
72+
4173
describe('event replay', () => {
4274
let doc: Document;
4375
const originalDocument = globalThis.document;
@@ -61,6 +93,7 @@ describe('event replay', () => {
6193

6294
afterEach(() => {
6395
doc.body.outerHTML = '<body></body>';
96+
globalThis[CONTRACT_PROPERTY] = {};
6497
});
6598

6699
/**
@@ -402,6 +435,17 @@ describe('event replay', () => {
402435

403436
expect(hasJSActionAttrs(ssrContents)).toBeFalse();
404437
expect(hasEventDispatchScript(ssrContents)).toBeFalse();
438+
439+
resetTViewsFor(SimpleComponent);
440+
await renderAndHydrate(doc, ssrContents, SimpleComponent, {
441+
envProviders: [
442+
{provide: PLATFORM_ID, useValue: 'browser'},
443+
// This ensures that there are no errors while bootstrapping an application
444+
// that has no events, but enables Event Replay feature.
445+
withStrictErrorHandler(),
446+
],
447+
hydrationFeatures: [withEventReplay()],
448+
});
405449
});
406450

407451
it('should not replay mouse events', async () => {

0 commit comments

Comments
 (0)