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

Commit 1ee9cf3

Browse files
author
Greg Rickaby
authored
Merge pull request #63 from WebDevStudios/feature/33-homepage
Feature/33 homepage
2 parents 48b0ce6 + 0991f2a commit 1ee9cf3

File tree

5 files changed

+82
-15
lines changed

5 files changed

+82
-15
lines changed

api/wordpress/_global/getPostTypeStaticPaths.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,19 @@ export default async function getPostTypeStaticPaths(postType) {
4545
// Process paths.
4646
const paths = !posts?.data?.[pluralName]?.edges
4747
? []
48-
: posts.data[pluralName].edges.map((post) => {
49-
// Trim leading and trailing slashes then split into array on inner slashes.
50-
const slug = post.node[pathField].replace(/^\/|\/$/g, '').split('/')
48+
: posts.data[pluralName].edges
49+
.map((post) => {
50+
// Trim leading and trailing slashes then split into array on inner slashes.
51+
const slug = post.node[pathField].replace(/^\/|\/$/g, '').split('/')
5152

52-
return {
53-
params: {
54-
slug
53+
return {
54+
params: {
55+
slug
56+
}
5557
}
56-
}
57-
})
58+
})
59+
// Filter out certain posts with custom routes (e.g., homepage).
60+
.filter((post) => !!post.params.slug.join('/').length)
5861

5962
return {
6063
paths,

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ export default async function getPostTypeStaticProps(
4545
slug
4646
)
4747

48+
const props = {
49+
post,
50+
error,
51+
errorMessage
52+
}
53+
54+
// Custom handling for homepage.
55+
if (error) {
56+
// Fallback to empty props if homepage not set in WP.
57+
props.post = null
58+
props.error = false
59+
}
60+
4861
// Merge in query results as Apollo state.
4962
return addApolloState(apolloClient, {
50-
props: {
51-
post,
52-
error,
53-
errorMessage
54-
},
63+
props,
5564
revalidate: 60 * 5
5665
})
5766
}

api/wordpress/pages/queryHomepage.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const {gql} = require('@apollo/client')
2+
const {singlePageFragment} = require('./queryPageById')
3+
4+
// Query: retrieve homepage.
5+
const queryHomepage = gql`
6+
query GET_HOMEPAGE($imageSize: MediaItemSizeEnum = LARGE) {
7+
homepageSettings {
8+
frontPage {
9+
...SinglePageFields
10+
}
11+
}
12+
}
13+
${singlePageFragment}
14+
`
15+
16+
export default queryHomepage

api/wordpress/pages/queryPageById.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import featuredImagePostFields from '../_partials/featuredImagePostFields'
66
const {gql} = require('@apollo/client')
77

88
// Fragment: retrieve single page fields.
9-
const singlePageFragment = gql`
9+
export const singlePageFragment = gql`
1010
fragment SinglePageFields on Page {
1111
${globalPostFields}
1212
blocksJSON

pages/index.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
import PropTypes from 'prop-types'
12
import Layout from '@/components/common/Layout'
23
import Hero from '@/components/molecules/Hero'
4+
import getPostTypeStaticProps from '@/api/wordpress/_global/getPostTypeStaticProps'
5+
import Page from './[...slug]'
36
import {NextSeo} from 'next-seo'
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>
827
<NextSeo
@@ -24,6 +43,26 @@ export default function HomePage() {
2443
title="Next.js Starter"
2544
description="A slightly opinionated, yet bare-bones Next.js starter."
2645
/>
46+
<p>
47+
To display your WordPress homepage dynamically, set your homepage to a
48+
static page via the WP dashboard (Settings: Reading Settings).
49+
</p>
2750
</Layout>
2851
)
2952
}
53+
54+
/**
55+
* Get post static props.
56+
*
57+
* @param {Object} context Context for current post.
58+
* @param {boolean} context.preview Whether requesting preview of post.
59+
* @param {Object} context.previewData Post preview data.
60+
* @return {Object} Post props.
61+
*/
62+
export async function getStaticProps() {
63+
return await getPostTypeStaticProps({slug: '/'}, postType)
64+
}
65+
66+
HomePage.propTypes = {
67+
post: PropTypes.object
68+
}

0 commit comments

Comments
 (0)