Skip to content

Commit 73b0bcb

Browse files
committed
msw问题处理
1 parent 3d55e9d commit 73b0bcb

2 files changed

Lines changed: 7 additions & 19 deletions

File tree

src/App.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import PageLayout from '@/layout';
22
import { routes } from '@/routes';
33
// import loadable from '@loadable/component';
44
import { Toaster } from "@/components/ui/sonner";
5-
import initMSW from '@/mock';
65
import { useLocaleStore } from "@/store/index";
7-
import { lazy, useEffect } from 'react';
6+
import { lazy } from 'react';
87
import { IntlProvider } from 'react-intl';
98
import {
109
BrowserRouter,
@@ -24,25 +23,8 @@ const getLazyComponent = (path: string) => {
2423
};
2524
const isHashRouter = import.meta.env.VITE_ROUTE === 'hashRouter';
2625
const Router = isHashRouter ? HashRouter : BrowserRouter;
27-
const mockEnable = (import.meta.env.VITE_MOCK_ENABLE||'true')=='true';
2826
export default function Index() {
2927
const { locale, messages } = useLocaleStore();
30-
useEffect(() => {
31-
if(mockEnable){
32-
initMSW();
33-
// 页面从后台恢复时,确保 SW 激活
34-
const handleVisibilityChange = () => {
35-
if (document.visibilityState === 'visible') {
36-
initMSW();
37-
}
38-
};
39-
40-
document.addEventListener('visibilitychange', handleVisibilityChange);
41-
return () => {
42-
document.removeEventListener('visibilitychange', handleVisibilityChange);
43-
};
44-
}
45-
}, []);
4628
//路由组件懒加载
4729
return (
4830
<IntlProvider locale={locale} messages={messages}>

src/main.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import './index.css';
66
const mockEnable = (import.meta.env.VITE_MOCK_ENABLE||'true')=='true';
77
if(mockEnable){
88
initMSW();
9+
const handleVisibilityChange = () => {
10+
if (document.visibilityState === 'visible') {
11+
initMSW();
12+
}
13+
};
14+
document.addEventListener('visibilitychange', handleVisibilityChange);
915
}
1016
createRoot(document.getElementById('root')!).render(
1117
<StrictMode>

0 commit comments

Comments
 (0)