From dabc52d37a4ea3c9cfdca545cc8c6d8d5939ddda Mon Sep 17 00:00:00 2001 From: Weitian Li Date: Thu, 5 Nov 2020 15:35:42 +0800 Subject: [PATCH 1/3] clean all router cache when switching project --- Composer/packages/client/src/utils/hooks.ts | 6 ++++++ Composer/packages/client/src/utils/routerCache.ts | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/Composer/packages/client/src/utils/hooks.ts b/Composer/packages/client/src/utils/hooks.ts index a67abc4f9f..2b92ca55e3 100644 --- a/Composer/packages/client/src/utils/hooks.ts +++ b/Composer/packages/client/src/utils/hooks.ts @@ -50,10 +50,16 @@ export const useLinks = () => { }; export const useRouterCache = (to: string) => { + const projectId = useRecoilValue(currentProjectIdState); const [state, setState] = useState(routerCache.getAll()); const { topLinks, bottomLinks } = useLinks(); const linksRef = useRef(topLinks.concat(bottomLinks)); linksRef.current = topLinks.concat(bottomLinks); + + useEffect(() => { + routerCache.cleanAll(); + }, [projectId]); + useEffect(() => { globalHistory.listen(({ location }) => { const links = linksRef.current; diff --git a/Composer/packages/client/src/utils/routerCache.ts b/Composer/packages/client/src/utils/routerCache.ts index 36a78f9db7..08290d9d20 100644 --- a/Composer/packages/client/src/utils/routerCache.ts +++ b/Composer/packages/client/src/utils/routerCache.ts @@ -25,6 +25,11 @@ class RouterCache { this._all[linkTo] = uri; this.storage.set(KEY, this._all); } + + cleanAll() { + this._all = {}; + this.storage.set(KEY, this._all); + } } export default new RouterCache(); From 0d0fe17a261c8f3ade08782571c32a171e169511 Mon Sep 17 00:00:00 2001 From: Weitian Li Date: Thu, 5 Nov 2020 15:59:20 +0800 Subject: [PATCH 2/3] reset routerCache --- Composer/packages/client/src/utils/hooks.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/Composer/packages/client/src/utils/hooks.ts b/Composer/packages/client/src/utils/hooks.ts index 2b92ca55e3..3ab7a49a92 100644 --- a/Composer/packages/client/src/utils/hooks.ts +++ b/Composer/packages/client/src/utils/hooks.ts @@ -58,6 +58,7 @@ export const useRouterCache = (to: string) => { useEffect(() => { routerCache.cleanAll(); + setState({}); }, [projectId]); useEffect(() => { From 1e04797c369525bea2b0fe5ec30c17b3a5b7d620 Mon Sep 17 00:00:00 2001 From: Weitian Li Date: Thu, 5 Nov 2020 17:01:20 +0800 Subject: [PATCH 3/3] should check rootProjectId --- Composer/packages/client/src/utils/hooks.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Composer/packages/client/src/utils/hooks.ts b/Composer/packages/client/src/utils/hooks.ts index 3ab7a49a92..d206829d53 100644 --- a/Composer/packages/client/src/utils/hooks.ts +++ b/Composer/packages/client/src/utils/hooks.ts @@ -8,7 +8,13 @@ import find from 'lodash/find'; import { useRecoilValue } from 'recoil'; import { FeatureFlagKey } from '@bfc/shared'; -import { designPageLocationState, currentProjectIdState, pluginPagesSelector, featureFlagsState } from '../recoilModel'; +import { + designPageLocationState, + currentProjectIdState, + pluginPagesSelector, + featureFlagsState, + rootBotProjectIdSelector, +} from '../recoilModel'; import { bottomLinks, topLinks } from './pageLinks'; import routerCache from './routerCache'; @@ -50,7 +56,7 @@ export const useLinks = () => { }; export const useRouterCache = (to: string) => { - const projectId = useRecoilValue(currentProjectIdState); + const rootProjectId = useRecoilValue(rootBotProjectIdSelector); const [state, setState] = useState(routerCache.getAll()); const { topLinks, bottomLinks } = useLinks(); const linksRef = useRef(topLinks.concat(bottomLinks)); @@ -59,7 +65,7 @@ export const useRouterCache = (to: string) => { useEffect(() => { routerCache.cleanAll(); setState({}); - }, [projectId]); + }, [rootProjectId]); useEffect(() => { globalHistory.listen(({ location }) => {