8000 fix(platform-server): less aggressive ngServerMode cleanup (#61106) · angular/angular@bc31ad9 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc31ad9

Browse files
jkremsAndrewKushnir
authored andcommitted
fix(platform-server): less aggressive ngServerMode cleanup (#61106)
Other code may depend on `ngServerMode` and it might have been set globally / via a bundler. Forcing it to `undefined` in those situations can lead to hard debug issues where the only symptom is that "suddenly" browser-specific code paths run on the server and (obviously) break. PR Close #61106
1 parent 116a97a commit bc31ad9

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

packages/platform-server/src/server.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ function _document(injector: Injector) {
116116
* @publicApi
117117
*/
118118
export function platformServer(extraProviders?: StaticProvider[] | undefined): PlatformRef {
119-
if (typeof ngServerMode === 'undefined') {
119+
const noServerModeSet = typeof ngServerMode === 'undefined';
120+
if (noServerModeSet) {
120121
globalThis['ngServerMode'] = true;
121122
}
122123

@@ -126,9 +127,11 @@ export function platformServer(extraProviders?: StaticProvider[] | undefined): P
126127
INTERNAL_SERVER_PLATFORM_PROVIDERS,
127128
)(extraProviders);
128129

129-
platform.onDestroy(() => {
130-
globalThis['ngServerMode'] = undefined;
131-
});
130+
if (noServerModeSet) {
131+
platform.onDestroy(() => {
132+
globalThis['ngServerMode'] = undefined;
133+
});
134+
}
132135

133136
return platform;
134137
}

packages/platform-server/test/hydration_utils.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,27 @@ export async function ssr(
253253
enableHydration?: boolean;
254254
} = {},
255255
): Promise<string> {
256-
const defaultHtml = DEFAULT_DOCUMENT;
257-
const {enableHydration = true, envProviders = [], hydrationFeatures = () => []} = options;
258-
const providers = [
259-
...envProviders,
260-
provideServerRendering(),
261-
enableHydration ? provideClientHydration(...hydrationFeatures()) : [],
262-
];
263-
264-
const bootstrap = () => bootstrapApplication(component, {providers});
265-
266-
return renderApplication(bootstrap, {
267-
document: options?.doc ?? defaultHtml,
268-
});
256+
try {
257+
// Enter server mode for the duration of this function.
258+
globalThis['ngServerMode'] = true;
259+
260+
const defaultHtml = DEFAULT_DOCUMENT;
261+
const {enableHydration = true, envProviders = [], hydrationFeatures = () => []} = options;
262+
const providers = [
263+
...envProviders,
264+
provideServerRendering(),
265+
enableHydration ? provideClientHydration(...hydrationFeatures()) : [],
266+
];
267+
268+
const bootstrap = () => bootstrapApplication(component, {providers});
269+
270+
return await renderApplication(bootstrap, {
271+
document: options?.doc ?? defaultHtml,
272+
});
273+
} finally {
274+
// Leave server mode so the remaining test is back in "client mode".
275+
globalThis['ngServerMode'] = undefined;
276+
}
269277
}
270278

271279
/**

0 commit comments

Comments
 (0)
0