Skip to content

Commit d735d99

Browse files
committed
feat(): support emit render info
1 parent 6bb18cc commit d735d99

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/brick-kit/src/core/Router.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
PluginLocation,
77
PluginRuntimeContext,
88
RuntimeMisc,
9+
NavTip,
910
} from "@next-core/brick-types";
1011
import {
1112
restoreDynamicTemplates,
@@ -234,6 +235,7 @@ export class Router {
234235
const history = getHistory();
235236
history.unblock();
236237

238+
const renderStartTime = Date.now();
237239
// Create the page tracker before page load.
238240
// And the API Analyzer maybe disabled.
239241
const pageTracker = apiAnalyzer.getInstance()?.pageTracker();
@@ -559,6 +561,36 @@ export class Router {
559561
pageTitle: document.title,
560562
});
561563

564+
const renderTime = Date.now() - renderStartTime;
565+
const { loadTime = 2500, loadInfoPage } =
566+
this.kernel.bootstrapData.settings.misc ?? {};
567+
if (currentApp.isBuildPush && renderTime > loadTime) {
568+
const getSecond = (time: number): number =>
569+
Math.floor(time * 100) / 100;
570+
window.dispatchEvent(
571+
new CustomEvent<NavTip[]>("app.bar.tips", {
572+
detail: [
573+
{
574+
text: `您的页面存在性能问题, 当前页面渲染时间为: ${getSecond(
575+
renderTime / 1000
576+
)} 秒, 规定阈值为: ${getSecond(
577+
(loadTime as number) / 1000
578+
)} 秒; 您已超过, 请您针对该页面进行性能优化!`,
579+
closeable: false,
580+
...(loadInfoPage
581+
? {
582+
info: {
583+
label: "查看详情",
584+
url: loadInfoPage as string,
585+
},
586+
}
587+
: {}),
588+
},
589+
],
590+
})
591+
);
592+
}
593+
562594
// analytics page_view event
563595
userAnalytics.event("page_view", {
564596
micro_app_id: this.kernel.currentApp.id,

packages/brick-types/src/runtime.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ export interface UserInfo {
321321
user_memo: string;
322322
}
323323

324+
export interface NavTip {
325+
text: string;
326+
closeable?: boolean;
327+
info?: {
328+
url: string;
329+
label: string;
330+
};
331+
}
332+
324333
/** @internal */
325334
export interface MagicBrickConfig {
326335
selector: string;

0 commit comments

Comments
 (0)