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

fix: nprogress always pending in custom react pages #1793

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 2 additions & 10 deletions src/client/theme-api/DumiPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useRouteMeta, useSiteData } from 'dumi';
import { useRouteMeta } from 'dumi';
import ContentTabs from 'dumi/theme/slots/ContentTabs';
import nprogress from 'nprogress';
import React, { useEffect, useState, type FC, type ReactNode } from 'react';
import React, { useState, type FC, type ReactNode } from 'react';
import { useTabQueryState } from './useTabMeta';

export const DumiPage: FC<{ children: ReactNode }> = (props) => {
Expand All @@ -10,13 +9,6 @@ export const DumiPage: FC<{ children: ReactNode }> = (props) => {
const [tab, setTab] = useState<NonNullable<typeof tabs>[0] | undefined>(() =>
tabs?.find(({ key }) => key === tabKey),
);
const { setLoading } = useSiteData();

// update loading status when page loaded
useEffect(() => {
setLoading(false);
nprogress.done();
}, []);

return (
<>
Expand Down
78 changes: 61 additions & 17 deletions src/features/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,67 @@ export default (api: IApi) => {
return memo;
});

api.onGenerateFiles({
// execute before umi tmpFiles plugin
stage: -Infinity,
fn() {
const { globalLoading } = api.appData;
const enableNProgress = !!api.config.themeConfig.nprogress;

// replace original loading component data
api.appData.globalLoading = '@@/dumi/theme/loading';

// generate dumi internal loading component for control loading status
// also wrap user loading component
api.writeTmpFile({
noPluginDir: true,
path: 'dumi/theme/loading.tsx',
content: `${
enableNProgress
? `import nprogress from '${winPath(
path.dirname(require.resolve('nprogress/package')),
)}';
import './nprogress.css';`
: ''
}${
globalLoading
? `
import UserLoading from '${globalLoading}';`
: ''
}
import React, { useLayoutEffect, type FC } from 'react';
import { useSiteData } from 'dumi';

const DumiLoading: FC = () => {
const { setLoading } = useSiteData();

useLayoutEffect(() => {
setLoading(true);${
enableNProgress
? `
nprogress.start();`
: ''
}

return () => {
setLoading(false);${
enableNProgress
? `
nprogress.done();`
: ''
}
}
}, []);

return ${globalLoading ? '<UserLoading />' : 'null'};
}

export default DumiLoading;
`,
});
},
});

api.onGenerateFiles(() => {
// write shadow theme files to tmp dir
themeMapKeys.forEach((key) => {
Expand Down Expand Up @@ -272,24 +333,13 @@ export default (api: IApi) => {
const entryExports = entryFile ? getModuleExports(entryFile) : [];
const hasDefaultExport = entryExports.includes('default');
const hasNamedExport = entryExports.some((exp) => exp !== 'default');
const enableNProgress = !!api.config.themeConfig.nprogress;

// generate context layout
api.writeTmpFile({
noPluginDir: true,
path: 'dumi/theme/ContextWrapper.tsx',
content: `import React, { useState, useEffect, useRef } from 'react';
import { useOutlet, history } from 'dumi';
${
enableNProgress
? `
import nprogress from '${winPath(
path.dirname(require.resolve('nprogress/package')),
)}';
import './nprogress.css';
`
: ''
}
import { SiteContext } from '${winPath(
require.resolve('../../client/theme-api/context'),
)}';
Expand Down Expand Up @@ -319,12 +369,6 @@ export default function DumiContextWrapper() {
if (next.location.pathname !== prev.current) {
prev.current = next.location.pathname;

// mark loading when route change, page component will set false when loaded
setLoading(true);

// start nprogress
${enableNProgress ? `nprogress.start();` : ''}

// scroll to top when route changed
document.documentElement.scrollTo(0, 0);
}
Expand Down
Loading