Skip to content

Commit

Permalink
♻️ refactor: fix dynamic import in rsc layout (#4445)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Oct 22, 2024
1 parent f104f65 commit 011d62a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { SpeedInsights } from '@vercel/speed-insights/next';
import { ResolvingViewport } from 'next';
import dynamic from 'next/dynamic';
import { cookies } from 'next/headers';
import { ReactNode } from 'react';
import { isRtlLang } from 'rtl-detect';

import Analytics from '@/components/Analytics';
import { DEFAULT_LANG, LOBE_LOCALE_COOKIE } from '@/const/locale';
import PWAInstall from '@/features/PWAInstall';
import AuthProvider from '@/layout/AuthProvider';
import GlobalProvider from '@/layout/GlobalProvider';
import { isMobileDevice } from '@/utils/responsive';

const PWAInstall = dynamic(() => import('@/features/PWAInstall'), { ssr: false });
const inVercel = process.env.VERCEL === '1';

type RootLayoutProps = {
Expand Down
35 changes: 35 additions & 0 deletions src/features/DebugUI/Content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import { Icon } from '@lobehub/ui';
import { App, FloatButton, Spin } from 'antd';
import { DatabaseIcon, Loader2 } from 'lucide-react';
import { memo, useState } from 'react';

import { debugService } from '@/services/debug';

const DebugUI = memo(() => {
const [loading, setLoading] = useState(false);
const { message } = App.useApp();
return (
<>
{loading && <Spin fullscreen />}
<FloatButton
icon={<Icon icon={loading ? Loader2 : DatabaseIcon} spin={loading} />}
onClick={async () => {
setLoading(true);

const startTime = Date.now();

await debugService.insertLargeDataToDB();

const duration = Date.now() - startTime;

setLoading(false);
message.success(`插入成功,耗时:${(duration / 1000).toFixed(1)} s`);
}}
tooltip={'性能压测,插入100w数据'}
/>
</>
);
});

export default DebugUI;
48 changes: 16 additions & 32 deletions src/features/DebugUI/index.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
'use client';

import { Icon } from '@lobehub/ui';
import { App, FloatButton, Spin } from 'antd';
import { DatabaseIcon, Loader2 } from 'lucide-react';
import { memo, useState } from 'react';

import { debugService } from '@/services/debug';

const DebugUI = memo(() => {
const [loading, setLoading] = useState(false);
const { message } = App.useApp();
return (
<>
{loading && <Spin fullscreen />}
<FloatButton
icon={<Icon icon={loading ? Loader2 : DatabaseIcon} spin={loading} />}
onClick={async () => {
setLoading(true);

const startTime = Date.now();

await debugService.insertLargeDataToDB();

const duration = Date.now() - startTime;

setLoading(false);
message.success(`插入成功,耗时:${(duration / 1000).toFixed(1)} s`);
}}
tooltip={'性能压测,插入100w数据'}
/>
</>
);
});
import dynamic from 'next/dynamic';
import { FC } from 'react';

import { getDebugConfig } from '@/config/debug';

let DebugUI: FC = () => null;

// we need use Constant Folding to remove code below in production
// refs: https://webpack.js.org/plugins/internal-plugins/#constplugin
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line unicorn/no-lonely-if
if (getDebugConfig().DEBUG_MODE) {
// @ts-ignore
DebugUI = dynamic(() => import('./Content'), { ssr: false });
}
}

export default DebugUI;
16 changes: 2 additions & 14 deletions src/layout/GlobalProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import dynamic from 'next/dynamic';
import { cookies, headers } from 'next/headers';
import { FC, PropsWithChildren } from 'react';
import { PropsWithChildren } from 'react';
import { resolveAcceptLanguage } from 'resolve-accept-language';

import { appEnv } from '@/config/app';
import { getDebugConfig } from '@/config/debug';
import { getServerFeatureFlagsValue } from '@/config/featureFlags';
import { LOBE_LOCALE_COOKIE } from '@/const/locale';
import {
LOBE_THEME_APPEARANCE,
LOBE_THEME_NEUTRAL_COLOR,
LOBE_THEME_PRIMARY_COLOR,
} from '@/const/theme';
import DebugUI from '@/features/DebugUI';
import { locales } from '@/locales/resources';
import { getServerGlobalConfig } from '@/server/globalConfig';
import { ServerConfigStoreProvider } from '@/store/serverConfig';
Expand All @@ -24,17 +23,6 @@ import QueryProvider from './Query';
import StoreInitialization from './StoreInitialization';
import StyleRegistry from './StyleRegistry';

let DebugUI: FC = () => null;

// we need use Constant Folding to remove code below in production
// refs: https://webpack.js.org/plugins/internal-plugins/#constplugin
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line unicorn/no-lonely-if
if (getDebugConfig().DEBUG_MODE) {
DebugUI = dynamic(() => import('@/features/DebugUI'), { ssr: false }) as FC;
}
}

const parserFallbackLang = () => {
/**
* The arguments are as follows:
Expand Down

0 comments on commit 011d62a

Please sign in to comment.