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

Commit 46befa6

Browse files
committed
Merge branch 'staging' of https://github.com/WebDevStudios/nextjs-wordpress-starter into feature/component-handling
2 parents 88c5c29 + 47803c0 commit 46befa6

File tree

8 files changed

+75
-18
lines changed

8 files changed

+75
-18
lines changed

api/wordpress/_global/getPostTypeArchive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const archiveQuerySeo = {
2929
* @param {string} cursor Start/end cursor for pagination.
3030
* @param {boolean} getNext Whether to retrieve next set of posts (true) or previous set (false).
3131
* @param {number} perPage Number of posts per page.
32-
* @return {object} Object containing Apollo client instance and post archive data or error object.
32+
* @return {object} Object containing Apollo client instance and post archive data or error object.
3333
*/
3434
export default async function getPostTypeArchive(
3535
postType,
@@ -40,7 +40,7 @@ export default async function getPostTypeArchive(
4040
perPage = 10
4141
) {
4242
// Retrieve post type query.
43-
const query = archiveQuerySeo?.[postType] ?? null
43+
const query = archiveQuerySeo?.[postType]?.query ?? null
4444

4545
// Get/create Apollo instance.
4646
const apolloClient = initializeWpApollo()

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ export default async function getPostTypeStaticProps(
8181
Object.keys(archiveQuerySeo).includes(postType) &&
8282
params.slug.length > 1
8383
) {
84-
const taxonomy = params.slug.shift()
85-
const taxonomySlug = params.slug.join('/')
84+
const taxonomy = params.slug.shift() // First "slug" piece is taxonomy type.
85+
const taxonomySlug = params.slug.pop() // Last "slug" piece is the lowest-level taxonomy term slug.
8686

8787
const {apolloClient, ...archiveData} = await getPostTypeTaxonomyArchive(
8888
taxonomy,

api/wordpress/_global/getPostTypeTaxonomyArchive.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {postTypes} from './postTypes'
33
import formatDefaultSeoData from '@/functions/formatDefaultSeoData'
44
import getMenus from '../menus/getMenus'
55
import queryPostsByTag from '../tags/queryPostsByTag'
6+
import queryPostsByCategory from '../categories/queryPostsByCategory'
67

78
/**
89
* Retrieve post taxnomy archive.
@@ -30,6 +31,7 @@ export default async function getPostTypeTaxonomyArchive(
3031
) {
3132
// Define single post query based on taxonomy.
3233
const postTypeQuery = {
34+
category: queryPostsByCategory,
3335
tag: queryPostsByTag
3436
}
3537

@@ -102,18 +104,26 @@ export default async function getPostTypeTaxonomyArchive(
102104
// Flatten posts array to include inner node post data.
103105
response.posts = posts.map((post) => post.node)
104106

107+
// Use final breadcrumb as alternative canonical URL.
108+
const breadcrumb =
109+
archiveSeo?.breadcrumbs &&
110+
archiveSeo.breadcrumbs.length > 0 &&
111+
archiveSeo.breadcrumbs.slice(-1)[0]?.url
112+
113+
// Manually create fallback taxonomy canonical URL.
114+
const fallback = `${response.defaultSeo?.openGraph?.url ?? ''}/${
115+
postTypes?.[postType]?.route
116+
}/${taxonomy}/${taxonomyId}`
117+
105118
// Structure archive SEO.
106119
response.post = {
107120
seo: {
121+
...archiveSeo,
108122
title:
109123
archiveSeo?.title ??
110124
`${taxonomyId} - ${response.defaultSeo?.openGraph?.siteName ?? ''}`,
111125
metaDesc: archiveSeo?.metaDesc ?? '',
112-
canonical:
113-
archiveSeo?.canonical ??
114-
`${response.defaultSeo?.openGraph?.url ?? ''}/${
115-
postTypes?.[postType]?.route
116-
}/${taxonomy}/${taxonomyId}`,
126+
canonical: archiveSeo?.canonical ?? breadcrumb ?? fallback,
117127
metaRobotsNofollow: archiveSeo?.metaRobotsNofollow ?? 'follow',
118128
metaRobotsNoindex: archiveSeo?.metaRobotsNoindex ?? 'index'
119129
}

api/wordpress/_global/postTypes.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Define valid WP post types (singular and plural GraphQL names).
22
export const postTypes = {
3-
comment: {
4-
pluralName: 'comments',
5-
route: null
6-
},
73
mediaItem: {
84
pluralName: 'mediaItems',
95
route: null

api/wordpress/_partials/seoPostFields.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Query partial: retrieve SEO post fields.
22
const seoPostFields = `
33
seo {
4+
breadcrumbs {
5+
text
6+
url
7+
}
48
canonical
59
title
610
metaDesc
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {gql} from '@apollo/client'
2+
import defaultPageData from '../_partials/defaultPageData'
3+
import {archivePostFragment, archivePosts} from '../posts/queryPostsArchive'
4+
import seoPostFields from '../_partials/seoPostFields'
5+
6+
// Query: retrieve posts category archive.
7+
const queryPostsByCategory = gql`
8+
query GET_POSTS_BY_CATEGORY(
9+
$first: Int
10+
$last: Int
11+
$after: String
12+
$before: String
13+
$orderBy: PostObjectsConnectionOrderbyEnum = DATE
14+
$order: OrderEnum = DESC
15+
$imageSize: MediaItemSizeEnum = THUMBNAIL
16+
$id: ID!
17+
$idType: CategoryIdType = SLUG
18+
) {
19+
${defaultPageData}
20+
category(id: $id, idType: $idType) {
21+
${seoPostFields}
22+
${archivePosts}
23+
}
24+
}
25+
${archivePostFragment}
26+
`
27+
28+
export default queryPostsByCategory

components/common/Layout.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export default function Layout({children, seo, hasJsonLd}) {
2828
}
2929
]
3030

31+
// Extract breadcrumbs from SEO.
32+
const breadcrumbs = seo?.breadcrumbs
33+
3134
return (
3235
<>
3336
<NextSeo
@@ -55,7 +58,21 @@ export default function Layout({children, seo, hasJsonLd}) {
5558
)}
5659
<Meta />
5760
<Header />
58-
<main>{children}</main>
61+
<main>
62+
{/* TODO: extract breadcrumbs to component and make pretty. */}
63+
{!!breadcrumbs && !!breadcrumbs.length && (
64+
<div>
65+
{breadcrumbs.map((breadcrumb, index) => (
66+
<span key={index}>
67+
<a href={breadcrumb?.url}>{breadcrumb?.text}</a>
68+
{index < breadcrumbs.length - 1 && <span> &raquo; </span>}
69+
</span>
70+
))}
71+
</div>
72+
)}
73+
74+
{children}
75+
</main>
5976
<Footer social={seo?.social} siteTitle={seo?.siteTitle} />
6077
</>
6178
)

functions/getPagePropTypes.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ export const seoSocialPropTypes = {
2121
// Yoast SEO prop types.
2222
export const seoPropTypes = {
2323
seo: PropTypes.shape({
24-
siteTitle: PropTypes.string,
25-
siteDescription: PropTypes.string,
26-
title: PropTypes.string,
24+
breadcrumbs: PropTypes.array,
25+
canonical: PropTypes.string,
2726
description: PropTypes.string,
28-
url: PropTypes.string,
2927
metaRobotsIndex: PropTypes.string,
3028
metaRobotsFollow: PropTypes.string,
3129
opengraphAuthor: PropTypes.string,
@@ -35,6 +33,10 @@ export const seoPropTypes = {
3533
altText: PropTypes.string,
3634
sourceUrl: PropTypes.string
3735
}),
36+
siteTitle: PropTypes.string,
37+
siteDescription: PropTypes.string,
38+
title: PropTypes.string,
39+
url: PropTypes.string,
3840
...seoSocialPropTypes
3941
})
4042
}

0 commit comments

Comments
 (0)