Skip to content

Commit e5b7d46

Browse files
committed
fix(marketplace): skip redundant route updates
1 parent b9b2fa1 commit e5b7d46

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

projects/app/src/pages/config/tool/marketplace.tsx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ import { PluginStatusEnum } from '@fastgpt/global/core/plugin/type';
4444
import { useConfirm } from '@fastgpt/web/hooks/useConfirm';
4545
import { useToast } from '@fastgpt/web/hooks/useToast';
4646

47+
type QueryValue = string | string[] | undefined;
48+
type QueryRecord = Record<string, QueryValue>;
49+
50+
const getComparableQueryValue = (value: QueryValue) =>
51+
Array.isArray(value) ? value.join('\0') : (value ?? '');
52+
53+
const isSameQuery = (currentQuery: QueryRecord, nextQuery: QueryRecord) => {
54+
const queryKeys = new Set([...Object.keys(currentQuery), ...Object.keys(nextQuery)]);
55+
56+
return Array.from(queryKeys).every(
57+
(key) => getComparableQueryValue(currentQuery[key]) === getComparableQueryValue(nextQuery[key])
58+
);
59+
};
60+
4761
// Custom hook for managing URL search params
4862
const useSearchParams = () => {
4963
const router = useRouter();
@@ -88,6 +102,9 @@ const useSearchParams = () => {
88102
delete nextQuery.source;
89103
}
90104

105+
// router.replace 会触发全局 NProgress。query 没变时直接跳过,避免浅路由空转循环。
106+
if (isSameQuery(router.query, nextQuery)) return;
107+
91108
router.replace(
92109
{
93110
pathname: router.pathname,
@@ -178,14 +195,14 @@ const ToolkitMarketplace = ({ marketplaceUrl }: { marketplaceUrl: string }) => {
178195

179196
// Control search box expansion based on focus and input value
180197
const [isSearchExpanded, setIsSearchExpanded] = useState(false);
181-
const [isFocused, setIsFocused] = useState(false);
182-
useEffect(() => {
183-
if (isFocused) {
184-
setIsSearchExpanded(true);
185-
} else if (!inputValue) {
198+
const handleSearchFocus = useCallback(() => {
199+
setIsSearchExpanded(true);
200+
}, []);
201+
const handleSearchBlur = useCallback(() => {
202+
if (!inputValue) {
186203
setIsSearchExpanded(false);
187204
}
188-
}, [isFocused, inputValue]);
205+
}, [inputValue]);
189206

190207
const {
191208
data: tools,
@@ -681,8 +698,8 @@ const ToolkitMarketplace = ({ marketplaceUrl }: { marketplaceUrl: string }) => {
681698
placeholder={t('app:toolkit_marketplace_search_placeholder')}
682699
value={inputValue}
683700
onChange={(e) => setInputValue(e.target.value)}
684-
onFocus={() => setIsFocused(true)}
685-
onBlur={() => setIsFocused(false)}
701+
onFocus={handleSearchFocus}
702+
onBlur={handleSearchBlur}
686703
/>
687704
{inputValue && (
688705
<MyIcon
@@ -793,8 +810,8 @@ const ToolkitMarketplace = ({ marketplaceUrl }: { marketplaceUrl: string }) => {
793810
placeholder={t('app:toolkit_marketplace_search_placeholder')}
794811
value={inputValue}
795812
onChange={(e) => setInputValue(e.target.value)}
796-
onFocus={() => setIsFocused(true)}
797-
onBlur={() => setIsFocused(false)}
813+
onFocus={handleSearchFocus}
814+
onBlur={handleSearchBlur}
798815
/>
799816
</InputGroup>
800817
</Box>

projects/marketplace/src/pages/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ const getSourceFilterValue = (value: string | string[] | undefined) => {
5757
: undefined;
5858
};
5959

60+
const isSameBrowserUrl = (nextUrl: string) => {
61+
if (typeof window === 'undefined') return false;
62+
63+
const targetUrl = new URL(nextUrl, window.location.origin);
64+
return (
65+
targetUrl.pathname === window.location.pathname && targetUrl.search === window.location.search
66+
);
67+
};
68+
6069
const ToolkitMarketplace = () => {
6170
const { t, i18n } = useTranslation();
6271
const router = useRouter();
@@ -150,6 +159,7 @@ const ToolkitMarketplace = () => {
150159
window.isSecureContext ||
151160
(window.location.protocol === 'http:' && window.location.hostname === 'localhost')
152161
) {
162+
if (isSameBrowserUrl(newUrl)) return;
153163
try {
154164
window.history.replaceState({}, '', newUrl);
155165
} catch (historyError) {

0 commit comments

Comments
 (0)