Skip to content

Commit a64d8ed

Browse files
alan-agius4dgp1130
authored andcommittedOct 27, 2023
fix(@angular-devkit/build-angular): add a maximum rendering timeout for SSR and SSG during development
There might be cases were currently, the render application promise does not resolve because the application never becomes stable in most cases this is due to errors, this causes the worker to never exit and the build to keep running until it's manually terminated. With this change, we add a maximum rendering timeout of 30seconds for each page.
1 parent aa9f052 commit a64d8ed

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/app-shell/render-worker.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ interface RenderRequest {
5151
/**
5252
* An optional URL path that represents the Angular route that should be rendered.
5353
*/
54-
url: string | undefined;
54+
url: string;
5555
}
5656

5757
/**
@@ -77,28 +77,45 @@ async function render({ serverBundlePath, document, url }: RenderRequest): Promi
7777
},
7878
];
7979

80+
let renderAppPromise: Promise<string>;
8081
// Render platform server module
8182
if (isBootstrapFn(bootstrapAppFn)) {
8283
assert(renderApplication, `renderApplication was not exported from: ${serverBundlePath}.`);
8384

84-
return renderApplication(bootstrapAppFn, {
85+
renderAppPromise = renderApplication(bootstrapAppFn, {
8586
document,
8687
url,
8788
platformProviders,
8889
});
90+
} else {
91+
assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`);
92+
const moduleClass = bootstrapAppFn || AppServerModule;
93+
assert(
94+
moduleClass,
95+
`Neither an AppServerModule nor a bootstrapping function was exported from: ${serverBundlePath}.`,
96+
);
97+
98+
renderAppPromise = renderModule(moduleClass, {
99+
document,
100+
url,
101+
extraProviders: platformProviders,
102+
});
89103
}
90-
assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`);
91-
const moduleClass = bootstrapAppFn || AppServerModule;
92-
assert(
93-
moduleClass,
94-
`Neither an AppServerModule nor a bootstrapping function was exported from: ${serverBundlePath}.`,
104+
105+
// The below should really handled by the framework!!!.
106+
let timer: NodeJS.Timeout;
107+
const renderingTimeout = new Promise<never>(
108+
(_, reject) =>
109+
(timer = setTimeout(
110+
() =>
111+
reject(
112+
new Error(`Page ${new URL(url, 'resolve://').pathname} did not render in 30 seconds.`),
113+
),
114+
30_000,
115+
)),
95116
);
96117

97-
return renderModule(moduleClass, {
98-
document,
99-
url,
100-
extraProviders: platformProviders,
101-
});
118+
return Promise.race([renderAppPromise, renderingTimeout]).finally(() => clearTimeout(timer));
102119
}
103120

104121
function isBootstrapFn(value: unknown): value is () => Promise<ApplicationRef> {

0 commit comments

Comments
 (0)
Please sign in to comment.