Skip to content

Commit

Permalink
chore: SSG keep server file when failed (#5023)
Browse files Browse the repository at this point in the history
* chore: keep serverFile for ssg

* chore: keep

* chore: failed

* chore: format
  • Loading branch information
ycjcl868 committed Jul 14, 2020
1 parent 0e2b19c commit eb84596
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions packages/preset-built-in/src/plugins/features/exportStatic.ts
Expand Up @@ -84,6 +84,8 @@ export default (api: IApi) => {
return routeMap;
});

// for debug prerender error
let serverRenderFailed = false;
// 不使用 api.modifyHTML 原因是不需要转 cheerio,提高预渲染效率
api.modifyProdHTMLContent(async (memo, args) => {
const { route } = args;
Expand Down Expand Up @@ -111,9 +113,11 @@ export default (api: IApi) => {
if (!error) {
return html;
} else {
serverRenderFailed = true;
api.logger.error('[SSR]', error);
}
} catch (e) {
serverRenderFailed = true;
api.logger.error(`${route.path} render failed`, e);
throw e;
}
Expand All @@ -122,14 +126,25 @@ export default (api: IApi) => {
});

api.onBuildComplete(({ err }) => {
if (!err && api.config?.ssr && process.env.RM_SERVER_FILE !== 'none') {
// remove umi.server.js
const serverFilePath = join(
api.paths.absOutputPath!,
OUTPUT_SERVER_FILENAME,
);
if (existsSync(serverFilePath)) {
rimraf.sync(serverFilePath);
if (!err && api.config?.ssr) {
if (serverRenderFailed) {
// tips: COMPRESS=none to debug
api.logger.info('You can use COMPRESS=none to debug.');
}
// RM_SERVER_FILE prior to serverFailed
if (
process.env.RM_SERVER_FILE
? process.env.RM_SERVER_FILE !== 'none'
: !serverRenderFailed
) {
// remove umi.server.js
const serverFilePath = join(
api.paths.absOutputPath!,
OUTPUT_SERVER_FILENAME,
);
if (existsSync(serverFilePath)) {
rimraf.sync(serverFilePath);
}
}
}
});
Expand Down

0 comments on commit eb84596

Please sign in to comment.