|
| 1 | +/** |
| 2 | + * External Dependencies. |
| 3 | + */ |
| 4 | +import { isArray, isEmpty } from 'lodash'; |
| 5 | +import { useRouter } from 'next/router'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Internal Dependencies. |
| 9 | + */ |
| 10 | +import Layout from '../src/components/layout'; |
| 11 | +import { FALLBACK, handleRedirectsAndReturnData, isCustomPageUri } from '../src/utils/slug'; |
| 12 | +import { getFormattedDate, getPathNameFromUrl, sanitize } from '../src/utils/miscellaneous'; |
| 13 | +import { getPage, getPages, getPost, getPosts } from '../src/utils/blog'; |
| 14 | +import axios from 'axios'; |
| 15 | +import { HEADER_FOOTER_ENDPOINT } from '../src/utils/constants/endpoints'; |
| 16 | +import Image from '../src/components/image'; |
| 17 | +import PostMeta from '../src/components/post-meta'; |
| 18 | + |
| 19 | +const Page = ( { headerFooter, pageData } ) => { |
| 20 | + const router = useRouter(); |
| 21 | + |
| 22 | + // If the page is not yet generated, this will be displayed |
| 23 | + // initially until getStaticProps() finishes running |
| 24 | + if ( router.isFallback ) { |
| 25 | + return <div>Loading...</div>; |
| 26 | + } |
| 27 | + |
| 28 | + return ( |
| 29 | + <Layout headerFooter={ headerFooter || {} } seo={ pageData?.yoast_head_json ?? {} }> |
| 30 | + <div className="mb-8 w-4/5 m-auto"> |
| 31 | + <figure className="overflow-hidden mb-4"> |
| 32 | + <Image |
| 33 | + sourceUrl={ pageData?._embedded[ 'wp:featuredmedia' ]?.[ 0 ]?.source_url ?? '' } |
| 34 | + title={ pageData?.title?.rendered ?? '' } |
| 35 | + width="600" |
| 36 | + height="400" |
| 37 | + layout="fill" |
| 38 | + containerClassNames="w-full h-600px" |
| 39 | + /> |
| 40 | + </figure> |
| 41 | + <PostMeta date={ getFormattedDate( pageData?.date ?? '' ) } authorName={ pageData?._embedded?.author?.[0]?.name ?? '' }/> |
| 42 | + <h1 dangerouslySetInnerHTML={ { __html: sanitize( pageData?.title?.rendered ?? '' ) } }/> |
| 43 | + <div dangerouslySetInnerHTML={ { __html: sanitize( pageData?.content?.rendered ?? '' ) } }/> |
| 44 | + </div> |
| 45 | + </Layout> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +export default Page; |
| 50 | + |
| 51 | +export async function getStaticProps( { params } ) { |
| 52 | + |
| 53 | + const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT ); |
| 54 | + const pageData = await getPage( params?.slug.pop() ?? '' ); |
| 55 | + |
| 56 | + const defaultProps = { |
| 57 | + props: { |
| 58 | + headerFooter: headerFooterData?.data ?? {}, |
| 59 | + pageData: pageData?.[0] ?? {} |
| 60 | + }, |
| 61 | + /** |
| 62 | + * Revalidate means that if a new request comes to server, then every 1 sec it will check |
| 63 | + * if the data is changed, if it is changed then it will update the |
| 64 | + * static file inside .next folder with the new data, so that any 'SUBSEQUENT' requests should have updated data. |
| 65 | + */ |
| 66 | + revalidate: 1, |
| 67 | + }; |
| 68 | + |
| 69 | + return handleRedirectsAndReturnData( defaultProps, pageData ); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Since the page name uses catch-all routes, |
| 74 | + * for example [...slug], |
| 75 | + * that's why params would contain slug which is an array. |
| 76 | + * For example, If we need to have dynamic route '/foo/bar' |
| 77 | + * Then we would add paths: [ params: { slug: ['foo', 'bar'] } } ] |
| 78 | + * Here slug will be an array is ['foo', 'bar'], then Next.js will statically generate the page at /foo/bar |
| 79 | + * |
| 80 | + * At build time next js will make an api call get the data and |
| 81 | + * generate a page bar.js inside .next/foo directory, so when the page is served on browser |
| 82 | + * data is already present, unlike getInitialProps which gets the page at build time but makes an api |
| 83 | + * call after page is served on the browser. |
| 84 | + * |
| 85 | + * @see https://nextjs.org/docs/basic-features/data-fetching#the-paths-key-required |
| 86 | + * |
| 87 | + * @returns {Promise<{paths: [], fallback: boolean}>} |
| 88 | + */ |
| 89 | +export async function getStaticPaths() { |
| 90 | + const pagesData = await getPages(); |
| 91 | + |
| 92 | + const pathsData = []; |
| 93 | + |
| 94 | + isArray( pagesData ) && pagesData.map( page => { |
| 95 | + |
| 96 | + /** |
| 97 | + * Extract pathname from url. |
| 98 | + * e.g. |
| 99 | + * getPathNameFromUrl( 'https://example.com/hello/hi/' ) will return '/hello/hi' |
| 100 | + * getPathNameFromUrl( 'https://example.com' ) will return '/' |
| 101 | + * @type {String} |
| 102 | + */ |
| 103 | + const pathName = getPathNameFromUrl( page?.link ?? '' ); |
| 104 | + |
| 105 | + // Build paths data. |
| 106 | + if ( ! isEmpty( pathName ) && ! isCustomPageUri( pathName ) ) { |
| 107 | + const slugs = pathName?.split( '/' ).filter( pageSlug => pageSlug ); |
| 108 | + pathsData.push( { params: { slug: slugs } } ); |
| 109 | + } |
| 110 | + } ); |
| 111 | + |
| 112 | + return { |
| 113 | + paths: pathsData, |
| 114 | + fallback: FALLBACK, |
| 115 | + }; |
| 116 | +} |
0 commit comments