-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve again router navigation speedup
Signed-off-by: Innei <tukon479@gmail.com>
- Loading branch information
Showing
8 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { NextPage, NextPageContext } from 'next' | ||
import type { NextRouter } from 'next/router' | ||
import { useRouter } from 'next/router' | ||
import { memo, useEffect, useState } from 'react' | ||
|
||
import { Loading } from '~/components/universal/Loading' | ||
import { isClientSide } from '~/utils/env' | ||
|
||
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: NextPage<any> = memo(() => { | ||
const router = useRouter() | ||
const [loading, setLoading] = useState( | ||
NextPage.getInitialProps ? true : false, | ||
) | ||
|
||
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(() => { | ||
// handle error | ||
}) | ||
} else { | ||
setLoading(false) | ||
setProps(task) | ||
} | ||
} catch (err) { | ||
// handle error | ||
} | ||
// NOTE: if asPath change, re-fetch data but not set loading to `true`!! | ||
}, [router.asPath]) | ||
|
||
if (!dataProps && loading) { | ||
return <Loading /> | ||
} | ||
|
||
// @ts-ignore | ||
return <NextPage {...dataProps} /> | ||
}) | ||
|
||
return Page as T | ||
} | ||
|
||
return NextPage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters