Skip to content

Commit

Permalink
perf(router): reduce the number of guard files
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Jun 27, 2021
1 parent 941ad59 commit 327d71b
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 177 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"qrcode",
"sider",
"pinia",
"sider"
"sider",
"nprogress"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"mockjs": "^1.1.0",
"nprogress": "^0.2.0",
"path-to-regexp": "^6.2.0",
"pinia": "2.0.0-beta.3",
"pinia": "^2.0.0-beta.3",
"print-js": "^1.6.0",
"qrcode": "^1.4.4",
"sortablejs": "^1.13.0",
Expand Down
1 change: 0 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import { defineComponent } from 'vue';
import { ConfigProvider } from 'ant-design-vue';
import { AppProvider } from '/@/components/Application';
import { useTitle } from '/@/hooks/web/useTitle';
import { useLocale } from '/@/locales/useLocale';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Drawer/src/BasicDrawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
watch(
() => props.visible,
(newVal, oldVal) => {
if (newVal != oldVal) visibleRef.value = newVal;
if (newVal !== oldVal) visibleRef.value = newVal;
},
{ deep: true }
);
Expand Down
7 changes: 0 additions & 7 deletions src/components/Menu/src/BasicMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,18 @@
</template>
<script lang="ts">
import type { MenuState } from './types';
import { computed, defineComponent, unref, reactive, watch, toRefs, ref } from 'vue';
import { Menu } from 'ant-design-vue';
import BasicSubMenuItem from './components/BasicSubMenuItem.vue';
import { MenuModeEnum, MenuTypeEnum } from '/@/enums/menuEnum';
import { useOpenKeys } from './useOpenKeys';
import { RouteLocationNormalizedLoaded, useRouter } from 'vue-router';
import { isFunction } from '/@/utils/is';
import { basicProps } from './props';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
import { REDIRECT_NAME } from '/@/router/constant';
import { useDesign } from '/@/hooks/web/useDesign';
import { getCurrentParentPath } from '/@/router/menus';
import { listenerRouteChange } from '/@/logics/mitt/routeChange';
import { getAllParentPath } from '/@/router/helper/menuHelper';
Expand Down
3 changes: 2 additions & 1 deletion src/components/registerGlobComp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
// Need
Button as AntButton,
Input,
Layout,
} from 'ant-design-vue';

const compList = [AntButton.Group];
Expand All @@ -14,5 +15,5 @@ export function registerGlobComp(app: App) {
app.component(comp.name || comp.displayName, comp);
});

app.use(Input).use(Button);
app.use(Input).use(Button).use(Layout);
}
3 changes: 0 additions & 3 deletions src/layouts/default/menu/useLayoutMenu.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { Menu } from '/@/router/types';
import type { Ref } from 'vue';

import { watch, unref, ref, computed } from 'vue';
import { useRouter } from 'vue-router';

import { MenuSplitTyeEnum } from '/@/enums/menuEnum';
import { useThrottleFn } from '@vueuse/core';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';

import { getChildrenMenus, getCurrentParentPath, getMenus, getShallowMenus } from '/@/router/menus';
import { usePermissionStore } from '/@/store/modules/permission';
import { useAppInject } from '/@/hooks/web/useAppInject';
Expand Down
5 changes: 0 additions & 5 deletions src/layouts/default/sider/MixSider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,19 @@
import type { Menu } from '/@/router/types';
import type { CSSProperties } from 'vue';
import type { RouteLocationNormalized } from 'vue-router';
import { defineComponent, onMounted, ref, computed, unref } from 'vue';
import { ScrollContainer } from '/@/components/Container';
import { SimpleMenuTag } from '/@/components/SimpleMenu';
import { Icon } from '/@/components/Icon';
import { AppLogo } from '/@/components/Application';
import Trigger from '../trigger/HeaderTrigger.vue';
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
import { useDragLine } from './useLayoutSider';
import { useGlobSetting } from '/@/hooks/setting';
import { useDesign } from '/@/hooks/web/useDesign';
import { useI18n } from '/@/hooks/web/useI18n';
import { useGo } from '/@/hooks/web/usePage';
import { SIDE_BAR_SHOW_TIT_MINI_WIDTH, SIDE_BAR_MINI_WIDTH } from '/@/enums/appEnum';
import clickOutside from '/@/directives/clickOutside';
import { getShallowMenus, getChildrenMenus, getCurrentParentPath } from '/@/router/menus';
import { listenerRouteChange } from '/@/logics/mitt/routeChange';
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import '/@/design/index.less';
import '/@/design/tailwind.css';

// Register icon sprite
import 'virtual:svg-icons-register';

import App from './App.vue';
import { createApp } from 'vue';
import { initAppConfigStore } from '/@/logics/initAppConfig';
Expand Down Expand Up @@ -42,7 +40,7 @@ async function bootstrap() {
setupRouter(app);

// router-guard
setupRouterGuard();
setupRouterGuard(router);

// Register global directive
setupGlobDirectives(app);
Expand Down
20 changes: 0 additions & 20 deletions src/router/guard/httpGuard.ts

This file was deleted.

142 changes: 133 additions & 9 deletions src/router/guard/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { router } from '/@/router';

import { createProgressGuard } from './progressGuard';
import type { Router, RouteLocationNormalized } from 'vue-router';
import { useAppStoreWidthOut } from '/@/store/modules/app';
import { useUserStoreWidthOut } from '/@/store/modules/user';
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
import { Modal, notification } from 'ant-design-vue';
import { warn } from '/@/utils/log';
import { unref } from 'vue';
import { setRouteChange } from '/@/logics/mitt/routeChange';
import { createPermissionGuard } from './permissionGuard';
import { createPageLoadingGuard } from './pageLoadingGuard';
import { createMessageGuard } from './messageGuard';
import { createScrollGuard } from './scrollGuard';
import { createHttpGuard } from './httpGuard';
import { createPageGuard } from './pageGuard';
import { createStateGuard } from './stateGuard';
import nProgress from 'nprogress';
import projectSetting from '/@/settings/projectSetting';

export function setupRouterGuard() {
// Don't change the order of creation
export function setupRouterGuard(router: Router) {
createPageGuard(router);
createPageLoadingGuard(router);
createHttpGuard(router);
Expand All @@ -19,3 +23,123 @@ export function setupRouterGuard() {
createPermissionGuard(router);
createStateGuard(router);
}

/**
* Hooks for handling page state
*/
function createPageGuard(router: Router) {
const loadedPageMap = new Map<string, boolean>();

router.beforeEach(async (to) => {
// The page has already been loaded, it will be faster to open it again, you don’t need to do loading and other processing
to.meta.loaded = !!loadedPageMap.get(to.path);
// Notify routing changes
setRouteChange(to);

return true;
});

router.afterEach((to) => {
loadedPageMap.set(to.path, true);
});
}

// Used to handle page loading status
function createPageLoadingGuard(router: Router) {
const userStore = useUserStoreWidthOut();
const appStore = useAppStoreWidthOut();
const { getOpenPageLoading } = useTransitionSetting();
router.beforeEach(async (to) => {
if (!userStore.getToken) {
return true;
}
if (to.meta.loaded) {
return true;
}

if (unref(getOpenPageLoading)) {
appStore.setPageLoadingAction(true);
return true;
}

return true;
});
router.afterEach(async () => {
if (unref(getOpenPageLoading)) {
// TODO Looking for a better way
// The timer simulates the loading time to prevent flashing too fast,
setTimeout(() => {
appStore.setPageLoading(false);
}, 220);
}
return true;
});
}

/**
* The interface used to close the current page to complete the request when the route is switched
* @param router
*/
function createHttpGuard(router: Router) {
const { removeAllHttpPending } = projectSetting;
let axiosCanceler: Nullable<AxiosCanceler>;
if (removeAllHttpPending) {
axiosCanceler = new AxiosCanceler();
}
router.beforeEach(async () => {
// Switching the route will delete the previous request
axiosCanceler?.removeAllPending();
return true;
});
}

// Routing switch back to the top
function createScrollGuard(router: Router) {
const isHash = (href: string) => {
return /^#/.test(href);
};

const body = document.body;

router.afterEach(async (to) => {
// scroll top
isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
return true;
});
}

/**
* Used to close the message instance when the route is switched
* @param router
*/
export function createMessageGuard(router: Router) {
const { closeMessageOnSwitch } = projectSetting;

router.beforeEach(async () => {
try {
if (closeMessageOnSwitch) {
Modal.destroyAll();
notification.destroy();
}
} catch (error) {
warn('message guard error:' + error);
}
return true;
});
}

export function createProgressGuard(router: Router) {
const { getOpenNProgress } = useTransitionSetting();
router.beforeEach(async (to) => {
if (to.meta.loaded) {
return true;
}
unref(getOpenNProgress) && nProgress.start();
return true;
});

router.afterEach(async () => {
unref(getOpenNProgress) && nProgress.done();
return true;
});
}
24 changes: 0 additions & 24 deletions src/router/guard/messageGuard.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/router/guard/pageGuard.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/router/guard/pageLoadingGuard.ts

This file was deleted.

Loading

0 comments on commit 327d71b

Please sign in to comment.