-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes DG-62, DG-58 ## What changed? Why? 1. Scroll restoration works on cross-page navigation 2. Layouts for the pages, with support for reuse of components on page nav!
- Loading branch information
Showing
9 changed files
with
164 additions
and
110 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
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,56 @@ | ||
import { Typography } from '@mui/material'; | ||
import { Link } from 'components/Link'; | ||
import { useLinkWithName } from 'hooks/useLinkWithName'; | ||
import { useRouter } from 'next/router'; | ||
|
||
export type HasStatusCode = { | ||
/** | ||
* What kind of error we encountered | ||
*/ | ||
statusCode?: number; | ||
}; | ||
|
||
// Error codes to title of page | ||
const TITLE_TEXT: Record<number | 'fallback', string> = { | ||
404: "😢 Oops, couldn't find that!", | ||
fallback: '😬 This is awkward...', | ||
}; | ||
|
||
/** | ||
* Contents of the page in a different element so fallback can work its server-rendered magic | ||
*/ | ||
export function ErrorPageContents({ statusCode }: HasStatusCode) { | ||
const router = useRouter(); | ||
const emailLink = useLinkWithName('Email'); | ||
const descriptions: Record<number | 'fallback', JSX.Element> = { | ||
404: ( | ||
<> | ||
I didn't see a page matching the url{' '} | ||
<Typography variant="code" component="code"> | ||
{router.asPath} | ||
</Typography>{' '} | ||
on the site. Check out the homepage and see if you can find what you were looking for. If | ||
not, | ||
</> | ||
), | ||
fallback: ( | ||
<> | ||
Looks like I encountered a serverside error, otherwise known as a dreaded{' '} | ||
{statusCode ?? 500}. Sorry! Try refreshing the page or attempting your action again. If | ||
it's still broken, | ||
</> | ||
), | ||
}; | ||
return ( | ||
<> | ||
<Typography variant="h1"> | ||
{(statusCode && TITLE_TEXT[statusCode]) || TITLE_TEXT.fallback} | ||
</Typography> | ||
<Typography variant="body1" sx={{ maxWidth: '35em' }}> | ||
{(statusCode && descriptions[statusCode]) || descriptions.fallback}{' '} | ||
{emailLink ? <Link layout="iconText" {...emailLink} href={emailLink.url} /> : 'Email Me'}{' '} | ||
and I can help you out! | ||
</Typography> | ||
</> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import { fetchFallbackData } from 'api/fetchFallbackData'; | ||
import { FetchedFallbackData, fetchFallbackData } from 'api/fetchFallbackData'; | ||
import { ErrorPageContents } from 'components/errors/ErrorPageContents'; | ||
import { ErrorLayout } from 'components/layouts/ErrorLayout'; | ||
import { PageLayout } from 'components/layouts/PageLayout'; | ||
import type { GetStaticProps } from 'next/types'; | ||
import { Contents, ErrorPageProps } from './_error'; | ||
import type { GetLayout } from 'types/Page'; | ||
|
||
export const getStaticProps: GetStaticProps = async () => fetchFallbackData(['version', 'footer']); | ||
type PageProps = { | ||
fallback: FetchedFallbackData<'footer' | 'version'>; | ||
}; | ||
|
||
export const getStaticProps: GetStaticProps<PageProps> = async () => | ||
fetchFallbackData(['version', 'footer']); | ||
|
||
/** | ||
* Error page, for 404s specifically | ||
*/ | ||
function Error404Page({ fallback }: ErrorPageProps) { | ||
return ( | ||
<ErrorLayout fallback={fallback} statusCode={404}> | ||
<Contents statusCode={404} /> | ||
</ErrorLayout> | ||
); | ||
function Page() { | ||
return <ErrorPageContents statusCode={404} />; | ||
} | ||
|
||
export default Error404Page; | ||
const getLayout: GetLayout<PageProps> = (page, pageProps) => ( | ||
<PageLayout fallback={pageProps.fallback}> | ||
<ErrorLayout statusCode={404}>{page}</ErrorLayout> | ||
</PageLayout> | ||
); | ||
|
||
Page.getLayout = getLayout; | ||
|
||
export default Page; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import { FetchedFallbackData, fetchFallbackData } from 'api/fetchFallbackData'; | ||
import { PageLayout } from 'components/layouts/PageLayout'; | ||
import { Privacy } from 'components/privacy/Privacy'; | ||
import { GetStaticProps } from 'next/types'; | ||
import type { GetStaticProps } from 'next/types'; | ||
import type { GetLayout } from 'types/Page'; | ||
|
||
type PrivacyProps = { | ||
type PageProps = { | ||
fallback: FetchedFallbackData<'footer' | 'version' | 'privacy'>; | ||
}; | ||
|
||
export const getStaticProps: GetStaticProps<PrivacyProps> = async () => | ||
export const getStaticProps: GetStaticProps<PageProps> = async () => | ||
fetchFallbackData(['footer', 'version', 'privacy']); | ||
|
||
function PrivacyPage({ fallback }: PrivacyProps) { | ||
return ( | ||
<PageLayout fallback={fallback}> | ||
<Privacy /> | ||
</PageLayout> | ||
); | ||
function Page() { | ||
return <Privacy />; | ||
} | ||
|
||
export default PrivacyPage; | ||
const getLayout: GetLayout<PageProps> = (page, pageProps) => ( | ||
<PageLayout fallback={pageProps.fallback}>{page}</PageLayout> | ||
); | ||
|
||
Page.getLayout = getLayout; | ||
|
||
export default Page; |
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,16 @@ | ||
import type { AppProps } from 'next/app'; | ||
import type { ReactElement, ReactNode } from 'react'; | ||
import type { NextPage } from 'next'; | ||
|
||
export type GetLayout<PropsType> = (page: ReactElement, pageProps: PropsType) => ReactNode; | ||
|
||
export type NextPageWithLayout<PropsType> = NextPage & { | ||
/** | ||
* All layouts must take a props from the page | ||
*/ | ||
getLayout?: GetLayout<PropsType>; | ||
}; | ||
|
||
export type AppPropsWithLayout<PropsType> = AppProps<PropsType> & { | ||
Component: NextPageWithLayout<PropsType>; | ||
}; |
d2be4a1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
dg – ./
dg-dgattey.vercel.app
dylangattey.com
dg.vercel.app
dg-git-main-dgattey.vercel.app