Skip to content

Commit b434574

Browse files
authored
Refactor to use async component imports (#7941)
#### What type of PR is this? /area ui /kind improvement /milestone 2.22.x #### What this PR does / why we need it: Lazy load partial routes to optimize initial load speed #### Does this PR introduce a user-facing change? ```release-note None ```
1 parent 10e3f16 commit b434574

File tree

15 files changed

+97
-63
lines changed

15 files changed

+97
-63
lines changed

ui/console-src/layouts/BasicLayout.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script lang="ts" setup>
2-
import GlobalSearchModal from "@/components/global-search/GlobalSearchModal.vue";
32
import MenuLoading from "@/components/menu/MenuLoading.vue";
43
import { RoutesMenu } from "@/components/menu/RoutesMenu";
54
import { useRouteMenuGenerator } from "@/composables/use-route-menu-generator";
@@ -14,14 +13,19 @@ import {
1413
type UseOverlayScrollbarsParams,
1514
} from "overlayscrollbars-vue";
1615
import { defineStore } from "pinia";
17-
import { onMounted, reactive, ref } from "vue";
16+
import { defineAsyncComponent, onMounted, reactive, ref } from "vue";
1817
import { RouterView, useRoute } from "vue-router";
1918
import IconLogo from "~icons/core/logo?width=5rem&height=2rem";
2019
2120
const route = useRoute();
2221
2322
// Global Search
2423
const globalSearchVisible = ref(false);
24+
25+
const GlobalSearchModal = defineAsyncComponent(
26+
() => import("@/components/global-search/GlobalSearchModal.vue")
27+
);
28+
2529
useEventListener(document, "keydown", (e: KeyboardEvent) => {
2630
const { key, ctrlKey, metaKey } = e;
2731
if (key === "k" && ((ctrlKey && !isMac) || metaKey)) {

ui/console-src/modules/contents/pages/module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import BasicLayout from "@console/layouts/BasicLayout.vue";
22
import { IconPages } from "@halo-dev/components";
33
import { definePlugin } from "@halo-dev/ui-shared";
44
import { markRaw } from "vue";
5-
import SinglePageList from "./SinglePageList.vue";
65

76
export default definePlugin({
87
routes: [
@@ -25,7 +24,7 @@ export default definePlugin({
2524
{
2625
path: "",
2726
name: "SinglePages",
28-
component: SinglePageList,
27+
component: () => import("./SinglePageList.vue"),
2928
},
3029
{
3130
path: "deleted",

ui/console-src/modules/contents/posts/module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import BlankLayout from "@console/layouts/BlankLayout.vue";
33
import { IconBookRead } from "@halo-dev/components";
44
import { definePlugin } from "@halo-dev/ui-shared";
55
import { markRaw } from "vue";
6-
import PostEditor from "./PostEditor.vue";
7-
import PostList from "./PostList.vue";
86

97
export default definePlugin({
108
routes: [
@@ -28,7 +26,7 @@ export default definePlugin({
2826
{
2927
path: "",
3028
name: "Posts",
31-
component: PostList,
29+
component: () => import("./PostList.vue"),
3230
},
3331
{
3432
path: "deleted",
@@ -43,7 +41,7 @@ export default definePlugin({
4341
{
4442
path: "editor",
4543
name: "PostEditor",
46-
component: PostEditor,
44+
component: () => import("./PostEditor.vue"),
4745
meta: {
4846
title: "core.post_editor.title",
4947
searchable: true,

ui/console-src/modules/interface/themes/components/ThemeListModal.vue

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<script lang="ts" setup>
22
import { usePluginModuleStore } from "@/stores/plugin";
33
import type { Theme } from "@halo-dev/api-client";
4-
import { VButton, VModal, VTabbar } from "@halo-dev/components";
4+
import { VButton, VLoading, VModal, VTabbar } from "@halo-dev/components";
55
import { utils, type ThemeListTab } from "@halo-dev/ui-shared";
66
import { useRouteQuery } from "@vueuse/router";
77
import {
88
computed,
9+
defineAsyncComponent,
910
inject,
10-
markRaw,
1111
nextTick,
1212
onMounted,
1313
provide,
@@ -17,10 +17,6 @@ import {
1717
type Ref,
1818
} from "vue";
1919
import { useI18n } from "vue-i18n";
20-
import InstalledThemes from "./list-tabs/InstalledThemes.vue";
21-
import LocalUpload from "./list-tabs/LocalUpload.vue";
22-
import NotInstalledThemes from "./list-tabs/NotInstalledThemes.vue";
23-
import RemoteDownload from "./list-tabs/RemoteDownload.vue";
2420
2521
const { t } = useI18n();
2622
@@ -37,25 +33,37 @@ const tabs = shallowRef<ThemeListTab[]>([
3733
{
3834
id: "installed",
3935
label: t("core.theme.list_modal.tabs.installed"),
40-
component: markRaw(InstalledThemes),
36+
component: defineAsyncComponent({
37+
loader: () => import("./list-tabs/InstalledThemes.vue"),
38+
loadingComponent: VLoading,
39+
}),
4140
priority: 10,
4241
},
4342
{
4443
id: "local-upload",
4544
label: t("core.theme.list_modal.tabs.local_upload"),
46-
component: markRaw(LocalUpload),
45+
component: defineAsyncComponent({
46+
loader: () => import("./list-tabs/LocalUpload.vue"),
47+
loadingComponent: VLoading,
48+
}),
4749
priority: 20,
4850
},
4951
{
5052
id: "remote-download",
5153
label: t("core.theme.list_modal.tabs.remote_download.label"),
52-
component: markRaw(RemoteDownload),
54+
component: defineAsyncComponent({
55+
loader: () => import("./list-tabs/RemoteDownload.vue"),
56+
loadingComponent: VLoading,
57+
}),
5358
priority: 30,
5459
},
5560
{
5661
id: "not_installed",
5762
label: t("core.theme.list_modal.tabs.not_installed"),
58-
component: markRaw(NotInstalledThemes),
63+
component: defineAsyncComponent({
64+
loader: () => import("./list-tabs/NotInstalledThemes.vue"),
65+
loadingComponent: VLoading,
66+
}),
5967
priority: 40,
6068
},
6169
]);

ui/console-src/modules/interface/themes/module.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import { IconPalette } from "@halo-dev/components";
22
import { definePlugin } from "@halo-dev/ui-shared";
33
import { markRaw } from "vue";
4-
import ThemeDetail from "./ThemeDetail.vue";
5-
import ThemeSetting from "./ThemeSetting.vue";
6-
import ThemeLayout from "./layouts/ThemeLayout.vue";
74

85
export default definePlugin({
96
components: {},
107
routes: [
118
{
129
path: "/theme",
1310
name: "ThemeRoot",
14-
component: ThemeLayout,
11+
component: () => import("./layouts/ThemeLayout.vue"),
1512
meta: {
1613
title: "core.theme.title",
1714
searchable: true,
@@ -27,12 +24,12 @@ export default definePlugin({
2724
{
2825
path: "",
2926
name: "ThemeDetail",
30-
component: ThemeDetail,
27+
component: () => import("./ThemeDetail.vue"),
3128
},
3229
{
3330
path: "settings/:group",
3431
name: "ThemeSetting",
35-
component: ThemeSetting,
32+
component: () => import("./ThemeSetting.vue"),
3633
meta: {
3734
title: "core.theme.settings.title",
3835
permissions: ["system:themes:view"],

ui/console-src/modules/system/backup/Backups.vue

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
<script lang="ts" setup>
2+
import { usePluginModuleStore } from "@/stores/plugin";
23
import {
34
IconAddCircle,
45
IconServerLine,
56
VButton,
67
VCard,
8+
VLoading,
79
VPageHeader,
810
VTabbar,
911
} from "@halo-dev/components";
10-
11-
import { usePluginModuleStore } from "@/stores/plugin";
1212
import type { BackupTab } from "@halo-dev/ui-shared";
1313
import { useRouteQuery } from "@vueuse/router";
14-
import { markRaw, onMounted, shallowRef } from "vue";
14+
import { defineAsyncComponent, onMounted, shallowRef } from "vue";
1515
import { useI18n } from "vue-i18n";
1616
import { useBackup } from "./composables/use-backup";
17-
import ListTab from "./tabs/List.vue";
18-
import RestoreTab from "./tabs/Restore.vue";
1917
2018
const { t } = useI18n();
2119
2220
const tabs = shallowRef<BackupTab[]>([
2321
{
2422
id: "backups",
2523
label: t("core.backup.tabs.backup_list"),
26-
component: markRaw(ListTab),
24+
component: defineAsyncComponent({
25+
loader: () => import("./tabs/List.vue"),
26+
loadingComponent: VLoading,
27+
}),
2728
},
2829
{
2930
id: "restore",
3031
label: t("core.backup.tabs.restore"),
31-
component: markRaw(RestoreTab),
32+
component: defineAsyncComponent({
33+
loader: () => import("./tabs/Restore.vue"),
34+
loadingComponent: VLoading,
35+
}),
3236
},
3337
]);
3438

ui/console-src/modules/system/plugins/components/PluginInstallationModal.vue

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script lang="ts" setup>
22
import { usePluginModuleStore } from "@/stores/plugin";
33
import type { Plugin } from "@halo-dev/api-client";
4-
import { VButton, VModal, VTabbar } from "@halo-dev/components";
4+
import { VButton, VLoading, VModal, VTabbar } from "@halo-dev/components";
55
import { utils, type PluginInstallationTab } from "@halo-dev/ui-shared";
66
import { useRouteQuery } from "@vueuse/router";
77
import {
88
computed,
9-
markRaw,
9+
defineAsyncComponent,
1010
nextTick,
1111
onMounted,
1212
provide,
@@ -16,8 +16,6 @@ import {
1616
type Ref,
1717
} from "vue";
1818
import { useI18n } from "vue-i18n";
19-
import LocalUpload from "./installation-tabs/LocalUpload.vue";
20-
import RemoteDownload from "./installation-tabs/RemoteDownload.vue";
2119
2220
const { t } = useI18n();
2321
@@ -43,13 +41,19 @@ const tabs = shallowRef<PluginInstallationTab[]>([
4341
{
4442
id: "local",
4543
label: t("core.plugin.upload_modal.tabs.local"),
46-
component: markRaw(LocalUpload),
44+
component: defineAsyncComponent({
45+
loader: () => import("./installation-tabs/LocalUpload.vue"),
46+
loadingComponent: VLoading,
47+
}),
4748
priority: 10,
4849
},
4950
{
5051
id: "remote",
5152
label: t("core.plugin.upload_modal.tabs.remote.title"),
52-
component: markRaw(RemoteDownload),
53+
component: defineAsyncComponent({
54+
loader: () => import("./installation-tabs/RemoteDownload.vue"),
55+
loadingComponent: VLoading,
56+
}),
5357
priority: 20,
5458
},
5559
]);

ui/console-src/modules/system/plugins/composables/use-plugin.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import {
66
type Plugin,
77
type SettingForm,
88
} from "@halo-dev/api-client";
9-
import { Dialog, Toast } from "@halo-dev/components";
9+
import { Dialog, Toast, VLoading } from "@halo-dev/components";
1010
import { utils, type PluginTab } from "@halo-dev/ui-shared";
1111
import { useMutation, useQuery } from "@tanstack/vue-query";
1212
import { useRouteQuery } from "@vueuse/router";
1313
import type { ComputedRef, Ref } from "vue";
14-
import { computed, markRaw, ref, shallowRef } from "vue";
14+
import { computed, defineAsyncComponent, ref, shallowRef } from "vue";
1515
import { useI18n } from "vue-i18n";
16-
import DetailTab from "../components/tabs/Detail.vue";
17-
import SettingTab from "../components/tabs/Setting.vue";
1816

1917
interface usePluginLifeCycleReturn {
2018
isStarted: ComputedRef<boolean | undefined>;
@@ -286,7 +284,10 @@ export function usePluginDetailTabs(
286284
{
287285
id: "detail",
288286
label: t("core.plugin.tabs.detail"),
289-
component: markRaw(DetailTab),
287+
component: defineAsyncComponent({
288+
loader: () => import("../components/tabs/Detail.vue"),
289+
loadingComponent: VLoading,
290+
}),
290291
},
291292
];
292293

@@ -338,7 +339,10 @@ export function usePluginDetailTabs(
338339
return {
339340
id: item.group,
340341
label: item.label || "",
341-
component: markRaw(SettingTab),
342+
component: defineAsyncComponent({
343+
loader: () => import("../components/tabs/Setting.vue"),
344+
loadingComponent: VLoading,
345+
}),
342346
};
343347
}),
344348
] as PluginTab[];

ui/console-src/modules/system/settings/SystemSettings.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import { coreApiClient } from "@halo-dev/api-client";
44
import {
55
IconSettings,
66
VCard,
7+
VLoading,
78
VPageHeader,
89
VTabbar,
910
} from "@halo-dev/components";
1011
import { utils } from "@halo-dev/ui-shared";
1112
import { useQuery } from "@tanstack/vue-query";
1213
import { useRouteQuery } from "@vueuse/router";
1314
import type { Component, Raw } from "vue";
14-
import { markRaw, provide, shallowRef, type Ref } from "vue";
15+
import {
16+
defineAsyncComponent,
17+
markRaw,
18+
provide,
19+
shallowRef,
20+
type Ref,
21+
} from "vue";
1522
import { useI18n } from "vue-i18n";
16-
import NotificationsTab from "./tabs/Notifications.vue";
1723
import SettingTab from "./tabs/Setting.vue";
1824
1925
const { t } = useI18n();
@@ -65,7 +71,10 @@ const { data: setting } = useQuery({
6571
{
6672
id: "notification",
6773
label: "通知设置",
68-
component: markRaw(NotificationsTab),
74+
component: defineAsyncComponent({
75+
loader: () => import("./tabs/Notifications.vue"),
76+
loadingComponent: VLoading,
77+
}),
6978
},
7079
];
7180
}

ui/console-src/modules/system/settings/module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import BasicLayout from "@console/layouts/BasicLayout.vue";
22
import { IconSettings } from "@halo-dev/components";
33
import { definePlugin } from "@halo-dev/ui-shared";
44
import { markRaw } from "vue";
5-
import SystemSettings from "./SystemSettings.vue";
65

76
export default definePlugin({
87
components: {},
@@ -25,7 +24,7 @@ export default definePlugin({
2524
{
2625
path: "",
2726
name: "SystemSetting",
28-
component: SystemSettings,
27+
component: () => import("./SystemSettings.vue"),
2928
},
3029
],
3130
},

0 commit comments

Comments
 (0)