Skip to content

Commit

Permalink
fix(@angular/build): add a maximum rendering timeout for SSG
Browse files Browse the repository at this point in the history
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.

Closes #27565

(cherry picked from commit 59b69f5)
  • Loading branch information
alan-agius4 committed May 2, 2024
1 parent a8a4d1f commit b7a0792
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/angular/build/src/utils/server-rendering/render-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,46 @@ export async function renderPage({
},
];

let html: string | undefined;
assert(
bootstrapAppFnOrModule,
'The file "./main.server.mjs" does not have a default export for an AppServerModule or a bootstrapping function.',
);

let renderAppPromise: Promise<string>;
if (isBootstrapFn(bootstrapAppFnOrModule)) {
html = await renderApplication(bootstrapAppFnOrModule, {
renderAppPromise = renderApplication(bootstrapAppFnOrModule, {
document,
url: route,
platformProviders,
});
} else {
html = await renderModule(bootstrapAppFnOrModule, {
renderAppPromise = renderModule(bootstrapAppFnOrModule, {
document,
url: route,
extraProviders: platformProviders,
});
}

// The below should really handled by the framework!!!.
// See: https://github.com/angular/angular/issues/51549
let timer: NodeJS.Timeout;
const renderingTimeout = new Promise<never>(
(_, reject) =>
(timer = setTimeout(
() =>
reject(
new Error(
`Page ${new URL(route, 'resolve://').pathname} did not render in 30 seconds.`,
),
),
30_000,
)),
);

const html = await Promise.race([renderAppPromise, renderingTimeout]).finally(() =>
clearTimeout(timer),
);

if (inlineCriticalCss) {
const { InlineCriticalCssProcessor } = await import(
'../../utils/index-file/inline-critical-css'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ async function render({ serverBundlePath, document, url }: RenderRequest): Promi
}

// The below should really handled by the framework!!!.
// See: https://github.com/angular/angular/issues/51549
let timer: NodeJS.Timeout;
const renderingTimeout = new Promise<never>(
(_, reject) =>
Expand Down

0 comments on commit b7a0792

Please sign in to comment.