|
| 1 | +import PropTypes from 'prop-types' |
1 | 2 | import Layout from '@/components/common/Layout'
|
2 | 3 | import Hero from '@/components/molecules/Hero'
|
3 | 4 | import config from '@/functions/config'
|
| 5 | +import getPostTypeStaticProps from '@/api/wordpress/_global/getPostTypeStaticProps' |
| 6 | +import Page from './[...slug]' |
4 | 7 |
|
5 |
| -export default function HomePage() { |
| 8 | +// Define route post type. |
| 9 | +const postType = 'page' |
| 10 | + |
| 11 | +/** |
| 12 | + * The HomePage component displays the home page via dynamic routing. |
| 13 | + * |
| 14 | + * @author WebDevStudios |
| 15 | + * @param {Object} [props] Properties passed to the component. |
| 16 | + * @return {Element} Element to render. |
| 17 | + */ |
| 18 | +export default function HomePage({post}) { |
| 19 | + // Display dynamic page data if homepage retrieved from WP. |
| 20 | + if (post) { |
| 21 | + return <Page post={post} /> |
| 22 | + } |
| 23 | + |
| 24 | + // Display static page content as fallback. |
6 | 25 | return (
|
7 | 26 | <Layout title={config.siteTitle} description={config.siteDescription}>
|
8 | 27 | <Hero
|
9 | 28 | background="https://images.unsplash.com/photo-1513106021000-168e5f56609d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2560&q=70"
|
10 | 29 | title="Next.js Starter"
|
11 | 30 | description="A slightly opinionated, yet bare-bones Next.js starter."
|
12 | 31 | />
|
| 32 | + <p> |
| 33 | + To display your WordPress homepage dynamically, set your homepage to a |
| 34 | + static page via the WP dashboard (Settings: Reading Settings). |
| 35 | + </p> |
13 | 36 | </Layout>
|
14 | 37 | )
|
15 | 38 | }
|
| 39 | + |
| 40 | +/** |
| 41 | + * Get post static props. |
| 42 | + * |
| 43 | + * @param {Object} context Context for current post. |
| 44 | + * @param {boolean} context.preview Whether requesting preview of post. |
| 45 | + * @param {Object} context.previewData Post preview data. |
| 46 | + * @return {Object} Post props. |
| 47 | + */ |
| 48 | +export async function getStaticProps() { |
| 49 | + const props = await getPostTypeStaticProps({slug: '/'}, postType) |
| 50 | + |
| 51 | + return !props.props.error |
| 52 | + ? props |
| 53 | + : // Fallback to empty props if homepage not set in WP. |
| 54 | + { |
| 55 | + props: { |
| 56 | + post: null |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +HomePage.propTypes = { |
| 62 | + post: PropTypes.object |
| 63 | +} |
0 commit comments