Skip to content

Commit

Permalink
fix: 修复获取html入口文件时,因为接口异常不能正常触发loadError生命周期 (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
dengBox authored Mar 29, 2023
1 parent de3c490 commit 1b45d30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/wujie-core/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ export const WUJIE_TIPS_NO_FETCH = "window上不存在fetch属性,需要自行
export const WUJIE_TIPS_NOT_SUPPORTED = "当前浏览器不支持无界,子应用将采用iframe方式渲染";
export const WUJIE_TIPS_SCRIPT_ERROR_REQUESTED = "脚本请求出现错误";
export const WUJIE_TIPS_CSS_ERROR_REQUESTED = "样式请求出现错误";
export const WUJIE_TIPS_HTML_ERROR_REQUESTED = "html请求出现错误";
export const WUJIE_TIPS_REPEAT_RENDER = "无界组件短时间重复渲染了两次,可能存在性能问题请检查代码";
export const WUJIE_TIPS_NO_SCRIPT = "目标Script尚未准备好或已经被移除";
23 changes: 17 additions & 6 deletions packages/wujie-core/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import processTpl, {
StyleObject,
} from "./template";
import { defaultGetPublicPath, getInlineCode, requestIdleCallback, error, compose, getCurUrl } from "./utils";
import { WUJIE_TIPS_NO_FETCH, WUJIE_TIPS_SCRIPT_ERROR_REQUESTED, WUJIE_TIPS_CSS_ERROR_REQUESTED } from "./constant";
import {
WUJIE_TIPS_NO_FETCH,
WUJIE_TIPS_SCRIPT_ERROR_REQUESTED,
WUJIE_TIPS_CSS_ERROR_REQUESTED,
WUJIE_TIPS_HTML_ERROR_REQUESTED,
} from "./constant";
import { getEffectLoaders, isMatchUrl } from "./plugin";
import Wujie from "./sandbox";
import { plugin, loadErrorHandler } from "./index";
Expand Down Expand Up @@ -211,14 +216,20 @@ export default function importHTML(params: {
const getHtmlParseResult = (url, html, htmlLoader) =>
(html
? Promise.resolve(html)
: fetch(url).then(
(response) => response.text(),
(e) => {
: fetch(url)
.then((response) => {
if (response.status >= 400) {
error(WUJIE_TIPS_HTML_ERROR_REQUESTED, { url, response });
loadError?.(url, new Error(WUJIE_TIPS_HTML_ERROR_REQUESTED));
return "";
}
return response.text();
})
.catch((e) => {
embedHTMLCache[url] = null;
loadError?.(url, e);
return Promise.reject(e);
}
)
})
).then((html) => {
const assetPublicPath = getPublicPath(url);
const { template, scripts, styles } = processTpl(htmlLoader(html), assetPublicPath);
Expand Down

0 comments on commit 1b45d30

Please sign in to comment.