Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preset-umi): throw by default when pre-render error #11521

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/docs/docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export default {

## exportStatic

- 类型:`{ extraRoutePaths: IUserExtraRoute[] | (() => IUserExtraRoute[] | Promise<IUserExtraRoute[]>) }`
- 类型:`{ extraRoutePaths: IUserExtraRoute[] | (() => IUserExtraRoute[] | Promise<IUserExtraRoute[]>), ignorePreRenderError: boolean }`
- 默认值:`undefined`

开启该配置后会针对每个路由单独输出 HTML 文件,通常用于静态站点托管。例如项目有如下路由:
Expand Down Expand Up @@ -654,6 +654,17 @@ export default {
}
```

`exportStatic` 在搭配 `ssr` 使用时会进行预渲染,在预渲染失败时会抛出异常并终止构建,可以通过配置 `ignorePreRenderError` 来忽略预渲染失败的错误,例如:
Copy link
Contributor

@fz6m fz6m Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- `exportStatic` 在搭配 `ssr` 使用时会进行预渲染,在预渲染失败时会抛出异常并终止构建,可以通过配置 `ignorePreRenderError` 来忽略预渲染失败的错误,例如:
+ `exportStatic` 在搭配 `ssr` 使用时会进行预渲染,默认在预渲染失败时会抛出异常并终止构建,通过配置 `ignorePreRenderError` 来忽略预渲染异常报错,例如:


```ts
// .umirc.ts
export default {
exportStatic: {
// 忽略预渲染失败的错误
ignorePreRenderError: true,
},
}
```

## favicons

Expand Down
7 changes: 7 additions & 0 deletions packages/preset-umi/src/features/exportStatic/exportStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ function getExportHtmlData(routes: Record<string, IRoute>): IExportHtmlItem[] {
* get pre-rendered html by route path
*/
async function getPreRenderedHTML(api: IApi, htmlTpl: string, path: string) {
const {
exportStatic: { ignorePreRenderError = false },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下面还有一个 api.config.mountElementId ,所有的属性解构应该放在一起。

} = api.config;
markupRender ??= require(absServerBuildPath(api))._markupGenerator;

try {
Expand All @@ -82,6 +85,9 @@ async function getPreRenderedHTML(api: IApi, htmlTpl: string, path: string) {
logger.info(`Pre-render for ${path}`);
} catch (err) {
logger.error(`Pre-render ${path} error: ${err}`);
if (!ignorePreRenderError) {
throw err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  throw new Error('Pre render error', { cause: err })

}
}

return htmlTpl;
Expand Down Expand Up @@ -126,6 +132,7 @@ export default (api: IApi) => {
zod.function(),
zod.array(zod.string()),
]),
ignorePreRenderError: zod.boolean().default(false),
})
.deepPartial(),
},
Expand Down
Loading