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: speed-up navigation to server-fetched route #1323

Merged
merged 2 commits into from
Jul 11, 2022
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
96 changes: 96 additions & 0 deletions src/components/biz/WrapperNextPage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import type { AxiosError } from 'axios'
import { isNumber } from 'lodash-es'
import type { NextPage, NextPageContext } from 'next'
import type { NextRouter } from 'next/router'
import { useRouter } from 'next/router'
import type { FC } from 'react'
import { useEffect, useState } from 'react'

import { RequestError } from '@mx-space/api-client'

import { Loading } from '~/components/universal/Loading'
import { isClientSide } from '~/utils/env'

import { ErrorView } from '../Error'

const createMockContext = (router: NextRouter): NextPageContext => {
return {
AppTree: () => null,
pathname: router.pathname,
query: router.query,
asPath: router.asPath,
}
}

export function wrapperNextPage<T extends NextPage<any>>(NextPage: T) {
if (isClientSide()) {
const Page: FC<any> = () => {
const router = useRouter()
const [loading, setLoading] = useState(
NextPage.getInitialProps ? true : false,
)

const [error, setError] = useState<any>(null)

const [dataProps, setProps] = useState(null)

useEffect(() => {
if (!NextPage.getInitialProps) {
return
}

try {
const task = NextPage.getInitialProps(createMockContext(router))
const isPromise = task.then
if (isPromise) {
task
.then((data) => {
setLoading(false)
setProps(data)
})
.catch((err) => {
setLoading(false)
setError(err)
})
} else {
setLoading(false)
setProps(task)
}
} catch (err: any) {
setLoading(false)
setError(err)
}
}, [])

if (error) {
let code: any
if (error instanceof RequestError) {
// @see: https://github.com/axios/axios/pull/3645
const axiosError = error.raw as AxiosError

code = isNumber(axiosError.response?.status)
? axiosError.response!.status
: 408
}

return (
<ErrorView
statusCode={code ?? 'Error'}
description={error?.message || '请求出错了'}
showRefreshButton
></ErrorView>
)
}

if (!dataProps && loading) {
return <Loading />
}

// @ts-ignore
return <NextPage {...dataProps} />
}
return Page as T
}

return NextPage
}
3 changes: 2 additions & 1 deletion src/pages/categories/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
CategoryWithChildrenModel,
} from '@mx-space/api-client'

import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import { TimelineListWrapper } from '~/components/universal/TimelineListWrapper'
import { BottomUpTransitionView } from '~/components/universal/Transition/bottom-up'
import { apiClient } from '~/utils/client'
Expand Down Expand Up @@ -77,4 +78,4 @@ CategoryListView.getInitialProps = async (ctx) => {
}
}

export default CategoryListView
export default wrapperNextPage(CategoryListView)
3 changes: 2 additions & 1 deletion src/pages/friends/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createElement, useEffect, useState } from 'react'
import type { LinkModel } from '@mx-space/api-client'
import { LinkState, LinkType } from '@mx-space/api-client'

import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import {
BannedSection,
FavoriteSection,
Expand Down Expand Up @@ -158,4 +159,4 @@ FriendsView.getInitialProps = async () => {

return { friends: shuffle(friends), collections, outdated, banned }
}
export default FriendsView
export default wrapperNextPage(FriendsView)
3 changes: 2 additions & 1 deletion src/pages/notes/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useUpdate } from 'react-use'
import type { NoteModel } from '@mx-space/api-client'
import { RequestError } from '@mx-space/api-client'

import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import { NoteFooterActionBar } from '~/components/in-page/Note/NoteActionBar'
import { NoteFooterActionBarForMobile } from '~/components/in-page/Note/NoteFooterNavigation'
import { NoteMarkdownRender } from '~/components/in-page/Note/NoteMarkdownRender'
Expand Down Expand Up @@ -321,4 +322,4 @@ PP.getInitialProps = async (ctx) => {
}
}

export default PP
export default wrapperNextPage(PP)
3 changes: 2 additions & 1 deletion src/pages/notes/topics/[topicSlug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { NoteModel, Pager } from '@mx-space/api-client'
import type { TopicModel } from '@mx-space/api-client/types/models/topic'

import { SEO } from '~/components/biz/Seo'
import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import { ArticleLayout } from '~/components/layouts/ArticleLayout'
import { Divider } from '~/components/universal/Divider'
import { Pagination } from '~/components/universal/Pagination'
Expand Down Expand Up @@ -93,4 +94,4 @@ TopicDetailPage.getInitialProps = async (ctx) => {
const { topicSlug } = ctx.query
return await apiClient.topic.getTopicBySlug(topicSlug as string)
}
export default TopicDetailPage
export default wrapperNextPage(TopicDetailPage)
3 changes: 2 additions & 1 deletion src/pages/posts/[category]/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { PostModel } from '@mx-space/api-client'

import { buildStoreDataLoadableView } from '~/components/biz/LoadableView'
import { Seo } from '~/components/biz/Seo'
import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import { PostRelated } from '~/components/in-page/Post/post-related'
import { ArticleLayout } from '~/components/layouts/ArticleLayout'
import {
Expand Down Expand Up @@ -295,4 +296,4 @@ PP.getInitialProps = async (ctx) => {
return data
}

export default PP
export default wrapperNextPage(PP)
3 changes: 2 additions & 1 deletion src/pages/projects/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextPage } from 'next'

import type { ProjectModel } from '@mx-space/api-client'

import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import { ProjectDetail } from '~/components/in-page/Project/detail'
import { useStore } from '~/store'
import { apiClient } from '~/utils/client'
Expand Down Expand Up @@ -35,4 +36,4 @@ ProjectView.getInitialProps = async (ctx) => {
return data
}

export default ProjectView
export default wrapperNextPage(ProjectView)
3 changes: 2 additions & 1 deletion src/pages/timeline/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { usePrevious } from 'react-use'

import type { TimelineData } from '@mx-space/api-client'

import { wrapperNextPage } from '~/components/biz/WrapperNextPage'
import { SolidBookmark } from '~/components/universal/Icons'
import { NumberTransition } from '~/components/universal/NumberRecorder'
import { TimelineListWrapper } from '~/components/universal/TimelineListWrapper'
Expand Down Expand Up @@ -301,4 +302,4 @@ TimeLineView.getInitialProps = async (ctx) => {
memory: !!memory,
} as TimeLineViewProps
}
export default TimeLineView
export default wrapperNextPage(TimeLineView)