Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(next/web): auto reload when dynamic import failed #947

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions next/web/src/App/helpers/lazy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { lazy as _lazy } from 'react';

function shouldReload() {
const reloadedAt = sessionStorage.getItem('TapDesk/reloadedAt');
if (reloadedAt && Date.now() - parseInt(reloadedAt) < 10000) {
return false;
}
sessionStorage.setItem('TapDesk/reloadedAt', Date.now().toString());
return true;
}

function Reloading() {
return <div>A newer version was found, reloading...</div>;
}

export const lazy: typeof _lazy = (factory) => {
return _lazy(() =>
factory().catch((err) => {
if (shouldReload()) {
location.reload();
return { default: Reloading as any };
}
throw err;
})
);
};
3 changes: 2 additions & 1 deletion next/web/src/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Suspense, lazy } from 'react';
import { Suspense } from 'react';
import { QueryClientProvider } from 'react-query';
import { RecoilRoot } from 'recoil';
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom';
Expand All @@ -11,6 +11,7 @@ import zhCN from 'antd/lib/locale/zh_CN';
import { useCurrentUser } from '@/leancloud';
import { queryClient } from '@/api/query-client';
import { Spin } from '@/components/antd';
import { lazy } from './helpers/lazy';

const Tickets = lazy(() => import('./Tickets'));
const Admin = lazy(() => import('./Admin'));
Expand Down