Skip to content

Commit ae2777c

Browse files
fix(core): guard window access in event replay and update misconfiguration tests
Guards window access with typeof check in event_replay.ts to prevent ReferenceError in non-browser environments during SSR and tests. Updates platform-server tests to expect missing setup warnings only when incremental hydration is explicitly disabled via withNoIncrementalHydration().
1 parent 8cf7778 commit ae2777c

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

packages/core/src/hydration/event_replay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function withEventReplay(): Provider[] {
8484
// is enabled, but there are no events configured in this application, in which case
8585
// we don't activate this feature, since there are no events to replay.
8686
const appId = inject(APP_ID);
87-
isEnabled = !!window._ejsas?.[appId];
87+
isEnabled = typeof window !== 'undefined' && !!window._ejsas?.[appId];
8888
}
8989
if (isEnabled) {
9090
performanceMarkFeature('NgEventReplay');

packages/platform-server/test/incremental_hydration_spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
provideClientHydration,
4242
withEventReplay,
4343
withIncrementalHydration,
44+
withNoIncrementalHydration,
4445
} from '@angular/platform-browser';
4546
import {provideRouter, RouterLink, RouterOutlet, Routes} from '@angular/router';
4647
import {getAppContents, prepareEnvironmentAndHydrate, resetTViewsFor} from './dom_utils';
@@ -2998,7 +2999,7 @@ describe('platform-server partial hydration integration', () => {
29982999
});
29993000

30003001
describe('misconfiguration', () => {
3001-
it('should log a warning when `withIncrementalHydration()` is missing in SSR setup', async () => {
3002+
it('should log a warning when incremental hydration is disabled in SSR setup', async () => {
30023003
@Component({
30033004
selector: 'app',
30043005
template: `
@@ -3012,8 +3013,8 @@ describe('platform-server partial hydration integration', () => {
30123013
const appId = 'custom-app-id';
30133014
const providers = [{provide: APP_ID, useValue: appId}];
30143015

3015-
// Empty list, `withIncrementalHydration()` is not included intentionally.
3016-
const hydrationFeatures = () => [];
3016+
// Explicitly disabled using withNoIncrementalHydration()
3017+
const hydrationFeatures = () => [withNoIncrementalHydration()];
30173018
const consoleSpy = spyOn(console, 'warn');
30183019
resetIncrementalHydrationEnabledWarnedForTests();
30193020

@@ -3022,7 +3023,7 @@ describe('platform-server partial hydration integration', () => {
30223023
expect(consoleSpy).toHaveBeenCalledWith(jasmine.stringMatching('NG0508'));
30233024
});
30243025

3025-
it('should log a warning when `withIncrementalHydration()` is missing in hydration setup', async () => {
3026+
it('should log a warning when incremental hydration is disabled in hydration setup', async () => {
30263027
@Component({
30273028
selector: 'app',
30283029
template: `
@@ -3051,8 +3052,8 @@ describe('platform-server partial hydration integration', () => {
30513052
const doc = getDocument();
30523053
await prepareEnvironmentAndHydrate(doc, html, SimpleComponent, {
30533054
envProviders: [...providers, {provide: PLATFORM_ID, useValue: 'browser'}],
3054-
// Empty list, `withIncrementalHydration()` is not included intentionally.
3055-
hydrationFeatures: () => [],
3055+
// Explicitly disabled using withNoIncrementalHydration()
3056+
hydrationFeatures: () => [withNoIncrementalHydration()],
30563057
});
30573058

30583059
expect(consoleSpy).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)