Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit c26a232

Browse files
committed
Display homepage dynamically
1 parent 951dafb commit c26a232

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

pages/index.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
1+
import PropTypes from 'prop-types'
12
import Layout from '@/components/common/Layout'
23
import Hero from '@/components/molecules/Hero'
34
import config from '@/functions/config'
5+
import getPostTypeStaticProps from '@/api/wordpress/_global/getPostTypeStaticProps'
6+
import Page from './[...slug]'
47

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.
625
return (
726
<Layout title={config.siteTitle} description={config.siteDescription}>
827
<Hero
928
background="https://images.unsplash.com/photo-1513106021000-168e5f56609d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2560&q=70"
1029
title="Next.js Starter"
1130
description="A slightly opinionated, yet bare-bones Next.js starter."
1231
/>
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>
1336
</Layout>
1437
)
1538
}
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

Comments
 (0)