From a61bdc9caaf860d83ae79c0578f644b31c0d989a Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 14 Jun 2023 17:56:46 +0800 Subject: [PATCH] fix: page always loading Signed-off-by: Innei --- next.d.ts | 8 -- src/components/app/Suspense/index.tsx | 6 ++ .../common/KamiMarkdown/MarkdownToc.tsx | 19 +++-- src/components/layouts/NoteLayout.tsx | 18 +++-- src/components/layouts/SiteLayout/index.tsx | 14 ++-- src/pages/[page]/index.tsx | 28 ++++--- src/pages/notes/[id].tsx | 76 ++++++++++++------- src/pages/posts/[category]/[slug].tsx | 44 +++++------ 8 files changed, 123 insertions(+), 90 deletions(-) delete mode 100644 next.d.ts create mode 100644 src/components/app/Suspense/index.tsx diff --git a/next.d.ts b/next.d.ts deleted file mode 100644 index 6ada36ca0..000000000 --- a/next.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import type { DynamicOptions } from 'next/dynamic' - -declare module 'next/dynamic' { - export default function dynamic

( - fc: () => Promise

, - options?: DynamicOptions

, - ): P -} diff --git a/src/components/app/Suspense/index.tsx b/src/components/app/Suspense/index.tsx new file mode 100644 index 000000000..b53d5dc40 --- /dev/null +++ b/src/components/app/Suspense/index.tsx @@ -0,0 +1,6 @@ +import type { FC } from 'react' +import { Suspense as ReactSuspense } from 'react' + +export const Suspense: FC = (props) => { + return {props.children} +} diff --git a/src/components/common/KamiMarkdown/MarkdownToc.tsx b/src/components/common/KamiMarkdown/MarkdownToc.tsx index 69443c7ca..85a370dad 100644 --- a/src/components/common/KamiMarkdown/MarkdownToc.tsx +++ b/src/components/common/KamiMarkdown/MarkdownToc.tsx @@ -1,20 +1,19 @@ -import dynamic from 'next/dynamic' import type { FC } from 'react' -import { memo, useEffect } from 'react' +import { lazy, memo, useEffect } from 'react' import { useActionStore } from '~/atoms/action' import { useAppStore } from '~/atoms/app' +import { Suspense } from '~/components/app/Suspense' import { FloatPopover } from '~/components/ui/FloatPopover' import { FluentList16Filled } from '~/components/ui/Icons/shared' import { useModalStack } from '~/components/ui/Modal' import type { TocProps } from '~/components/widgets/Toc' import { useDetectIsNarrowThanLaptop } from '~/hooks/ui/use-viewport' -const Toc = dynamic( - () => import('~/components/widgets/Toc').then((m) => m.Toc), - { - ssr: false, - }, +const Toc = lazy(() => + import('~/components/widgets/Toc').then((m) => ({ + default: m.Toc, + })), ) export const MarkdownToc: FC = memo((props) => { @@ -62,5 +61,9 @@ export const MarkdownToc: FC = memo((props) => { actionStore.removeActionById(id) } }, [isNarrowThanLaptop, isMobile, present, props]) - return !isNarrowThanLaptop ? : null + return !isNarrowThanLaptop ? ( + + + + ) : null }) diff --git a/src/components/layouts/NoteLayout.tsx b/src/components/layouts/NoteLayout.tsx index d4a981d36..f6eaa4cb4 100644 --- a/src/components/layouts/NoteLayout.tsx +++ b/src/components/layouts/NoteLayout.tsx @@ -1,9 +1,8 @@ import clsx from 'clsx' import dayjs from 'dayjs' import { motion, useAnimationControls } from 'framer-motion' -import dynamic from 'next/dynamic' import type { ReactNode } from 'react' -import { forwardRef, useCallback, useEffect } from 'react' +import { forwardRef, lazy, useCallback, useEffect } from 'react' import { shallow } from 'zustand/shallow' import { useAppStore } from '~/atoms/app' @@ -19,14 +18,15 @@ import { microReboundPreset } from '~/constants/spring' import { springScrollToElement } from '~/utils/spring' import { resolveUrl } from '~/utils/utils' +import { Suspense } from '../app/Suspense' import { IconTransition } from '../common/IconTransition' import { AnimateChangeInHeight } from '../ui/AnimateChangeInHeight' import { Banner } from '../ui/Banner' -const NoteTimelineList = dynamic(() => - import('~/components/in-page/Note/NoteTimelineList').then( - (mo) => mo.NoteTimelineList, - ), +const NoteTimelineList = lazy(() => + import('~/components/in-page/Note/NoteTimelineList').then((mo) => ({ + default: mo.NoteTimelineList, + })), ) const bannerClassNames = { @@ -221,7 +221,11 @@ export const NoteLayout = forwardRef( - {!isPreview && } + {!isPreview && ( + + + + )} ) }, diff --git a/src/components/layouts/SiteLayout/index.tsx b/src/components/layouts/SiteLayout/index.tsx index 723aa36cf..256a670e5 100644 --- a/src/components/layouts/SiteLayout/index.tsx +++ b/src/components/layouts/SiteLayout/index.tsx @@ -1,7 +1,6 @@ import type { FC } from 'react' import React, { memo, - Suspense, useCallback, useEffect, useId, @@ -13,6 +12,7 @@ import { ShortcutProvider } from 'react-shortcut-guide' import { useActionStore } from '~/atoms/action' import { useAppStore } from '~/atoms/app' +import { Suspense } from '~/components/app/Suspense' import { BiMoonStarsFill, PhSunBold } from '~/components/ui/Icons/layout' import { ModalStackProvider } from '~/components/ui/Modal' import { TrackerAction } from '~/constants/tracker' @@ -134,7 +134,7 @@ export const SiteLayout: FC = memo(({ children }) => {

- +
@@ -155,19 +155,19 @@ export const SiteLayout: FC = memo(({ children }) => { } /> - +