Skip to content

Commit 638715e

Browse files
committed
refactor(@angular/ssr): Convert crypto unavailable error to a soft error
This update changes the handling of the crypto unavailable error in the Angular SSR environment. Instead of throwing a hard error, which could interrupt the application flow, the error is now treated as a soft error
1 parent 523c2c9 commit 638715e

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

packages/angular/ssr/src/app.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,17 @@ export class AngularServerApp {
254254
return this.assets.getServerAsset(fileName);
255255
});
256256

257-
if (isSsrMode) {
257+
// TODO(alanagius): remove once Node.js version 18 is no longer supported.
258+
if (isSsrMode && typeof crypto === 'undefined') {
259+
// eslint-disable-next-line no-console
260+
console.error(
261+
`The global 'crypto' module is unavailable. ` +
262+
`If you are running on Node.js, please ensure you are using version 20 or later, ` +
263+
`which includes built-in support for the Web Crypto module.`,
264+
);
265+
}
266+
267+
if (isSsrMode && typeof crypto !== 'undefined') {
258268
// Only cache if we are running in SSR Mode.
259269
const cacheKey = await sha256(html);
260270
let htmlWithCriticalCss = this.criticalCssLRUCache.get(cacheKey);

packages/angular/ssr/src/utils/crypto.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
* represented as a hexadecimal string.
1515
*/
1616
export async function sha256(data: string): Promise<string> {
17-
if (typeof crypto === 'undefined') {
18-
// TODO(alanagius): remove once Node.js version 18 is no longer supported.
19-
throw new Error(
20-
`The global 'crypto' module is unavailable. ` +
21-
`If you are running on Node.js, please ensure you are using version 20 or later, ` +
22-
`which includes built-in support for the Web Crypto module.`,
23-
);
24-
}
25-
2617
const encodedData = new TextEncoder().encode(data);
2718
const hashBuffer = await crypto.subtle.digest('SHA-256', encodedData);
2819
const hashParts: string[] = [];

0 commit comments

Comments
 (0)