Skip to content

Instantly share code, notes, and snippets.

@ayyash
Last active October 2, 2022 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayyash/49423e0b919069cd9ac7e4819c6ad191 to your computer and use it in GitHub Desktop.
Save ayyash/49423e0b919069cd9ac7e4819c6ad191 to your computer and use it in GitHub Desktop.
Rewriting _renderUniversal
// in index.ts, simplify
async function _renderUniversal(
routes: string[],
context: BuilderContext,
clientPath: string,
serverPath: string,
indexFile: string
): Promise<BuilderOutput> {
// the server bundle is server/main.js
const serverBundlePath = path.join(serverPath, 'main.js');
// no more loop into outputPaths
if (!fs.existsSync(serverBundlePath)) {
throw new Error(`Could not find the main bundle: ${serverBundlePath}`);
}
context.logger.info(`Prerendering ${routes.length} route(s) to ${clientPath}...`);
try {
// reduced the two tries into one
// this map is alright, I'll keep it
const results = (await Promise.all(
routes.map((route) => {
// removed deployUrl, and assume css related props
const options: RenderOptions = {
indexFile,
clientPath, // this is outputPath
route,
serverBundlePath
};
// direct import from worker, no more Piscina worker
return PreRender(options);
})
)) as RenderResult[];
let numErrors = 0;
for (const { errors, warnings } of results) {
errors?.forEach((e) => context.logger.error(e));
warnings?.forEach((e) => context.logger.warn(e));
numErrors += errors?.length ?? 0;
}
if (numErrors > 0) {
throw Error(`Rendering failed with ${numErrors} worker errors.`);
}
context.logger.info(`Prerendering routes to ${clientPath} complete.`);
} catch (err) {
context.logger.error(`Prerendering routes to ${clientPath} failed.`);
return { success: false, error: err.message };
}
return { success: true};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment