Skip to content

Commit

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

(cherry picked from commit a64d8ed)
  • Loading branch information
alan-agius4 authored and dgp1130 committed Oct 27, 2023
1 parent 5d063f1 commit 9994b2d
Showing 1 changed file with 29 additions and 12 deletions.
Expand Up @@ -51,7 +51,7 @@ interface RenderRequest {
/**
* An optional URL path that represents the Angular route that should be rendered.
*/
url: string | undefined;
url: string;
}

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

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

return renderApplication(bootstrapAppFn, {
renderAppPromise = renderApplication(bootstrapAppFn, {
document,
url,
platformProviders,
});
} else {
assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`);
const moduleClass = bootstrapAppFn || AppServerModule;
assert(
moduleClass,
`Neither an AppServerModule nor a bootstrapping function was exported from: ${serverBundlePath}.`,
);

renderAppPromise = renderModule(moduleClass, {
document,
url,
extraProviders: platformProviders,
});
}
assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`);
const moduleClass = bootstrapAppFn || AppServerModule;
assert(
moduleClass,
`Neither an AppServerModule nor a bootstrapping function was exported from: ${serverBundlePath}.`,

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

return renderModule(moduleClass, {
document,
url,
extraProviders: platformProviders,
});
return Promise.race([renderAppPromise, renderingTimeout]).finally(() => clearTimeout(timer));
}

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

0 comments on commit 9994b2d

Please sign in to comment.