Skip to content

Commit 4792f1a

Browse files
committed
Add SEO
1 parent 30a6a24 commit 4792f1a

File tree

8 files changed

+41
-31
lines changed

8 files changed

+41
-31
lines changed

pages/blog/[slug].js

+15-12
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import axios from 'axios';
1010
*/
1111
import Layout from '../../src/components/layout';
1212
import { FALLBACK, handleRedirectsAndReturnData } from '../../src/utils/slug';
13-
import { sanitize } from '../../src/utils/miscellaneous';
13+
import { getFormattedDate, sanitize } from '../../src/utils/miscellaneous';
1414
import { HEADER_FOOTER_ENDPOINT } from '../../src/utils/constants/endpoints';
1515
import { getPost, getPosts } from '../../src/utils/blog';
1616
import Image from '../../src/components/image';
1717
import PostMeta from '../../src/components/post-meta';
1818

1919
const Post = ( { headerFooter, postData } ) => {
2020
const router = useRouter();
21+
22+
console.log( 'postData', postData );
2123

2224
/**
2325
* If the page is not yet generated, this will be displayed
@@ -28,20 +30,21 @@ const Post = ( { headerFooter, postData } ) => {
2830
}
2931

3032
return (
31-
<Layout headerFooter={ headerFooter || {} } seo={ null }>
33+
<Layout headerFooter={ headerFooter || {} } seo={ postData?.yoast_head_json ?? {} }>
3234
<div className="mb-8 w-4/5 m-auto">
3335
<figure className="overflow-hidden mb-4">
3436
<Image
35-
sourceUrl={ postData?.attachment_image?.img_src?.[ 0 ] ?? '' }
36-
title={ postData?.title ?? '' }
37-
width={ postData?.attachment_image?.img_src?.[ 1 ] ?? '600' }
38-
height={ postData?.attachment_image?.img_src?.[ 2 ] ?? '400' }
37+
sourceUrl={ postData?._embedded[ 'wp:featuredmedia' ]?.[ 0 ]?.source_url ?? '' }
38+
title={ postData?.title?.rendered ?? '' }
39+
width="600"
40+
height="400"
3941
layout="fill"
4042
containerClassNames="w-full h-600px"
4143
/>
4244
</figure>
43-
<PostMeta post={ postData }/>
44-
<div dangerouslySetInnerHTML={ { __html: sanitize( postData?.content ?? '' ) } }/>
45+
<PostMeta date={ getFormattedDate( postData?.date ?? '' ) } authorName={ postData?._embedded?.author?.[0]?.name ?? '' }/>
46+
<h1 dangerouslySetInnerHTML={ { __html: sanitize( postData?.title?.rendered ?? '' ) } }/>
47+
<div dangerouslySetInnerHTML={ { __html: sanitize( postData?.content?.rendered ?? '' ) } }/>
4548
</div>
4649
</Layout>
4750
);
@@ -51,13 +54,13 @@ export default Post;
5154

5255
export async function getStaticProps( { params } ) {
5356
const { data: headerFooterData } = await axios.get( HEADER_FOOTER_ENDPOINT );
54-
const { data: postData } = await getPost( params?.slug ?? '' );
55-
// params?.slug ?? ''
57+
// const { data: postData } = await getPost( params?.slug ?? '' );
58+
const postData = await getPost( params?.slug ?? '' );
5659

5760
const defaultProps = {
5861
props: {
5962
headerFooter: headerFooterData?.data ?? {},
60-
postData: postData?.post_data ?? {}
63+
postData: postData?.[0] ?? {}
6164
},
6265
/**
6366
* Revalidate means that if a new request comes to server, then every 1 sec it will check
@@ -67,7 +70,7 @@ export async function getStaticProps( { params } ) {
6770
revalidate: 1,
6871
};
6972

70-
return handleRedirectsAndReturnData( defaultProps, postData, 'post_data' );
73+
return handleRedirectsAndReturnData( defaultProps, postData );
7174
}
7275

7376
/**

src/components/post-meta/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const PostMeta = ({ post }) => {
1+
const PostMeta = ({ date, authorName }) => {
22
return (
33
<div className="font-bold mb-2">
4-
<time className="text-brand-wild-blue" dateTime={ post?.date ?? '' }>{ post?.date ?? '' }</time>
5-
<span className="ml-2"><span className="italic mr-2">by</span>{ post?.meta?.author_name ?? '' }</span>
4+
<time className="text-brand-wild-blue" dateTime={ date || '' }>{ date || '' }</time>
5+
<span className="ml-2"><span className="italic mr-2">by</span>{ authorName || '' }</span>
66
</div>
77
);
88
}

src/components/posts/post.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const Post = ( { post } ) => {
3232
</figure>
3333
</a>
3434
</Link>
35-
<PostMeta post={ post }/>
35+
<PostMeta date={ post?.date ?? '' } authorName={ post?.meta?.author_name ?? '' }/>
3636
<Link href={ `/blog/${ post?.slug }/` }>
3737
<a>
3838
<h2 className="font-bold mb-3 text-lg text-brand-gun-powder font-bold uppercase hover:text-blue-500"

src/components/seo/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextSeo } from 'next-seo';
22
import PropTypes from 'prop-types';
3+
import { isArray } from 'lodash';
34

45
/**
56
* Custom SEO component
@@ -52,7 +53,7 @@ const Seo = ( { seo, uri } ) => {
5253
description: og_description,
5354
images: [
5455
{
55-
url: og_image.length ? ( og_image[0]?.url ?? '' ) : '',
56+
url: isArray( og_image ) ? ( og_image[0]?.url ?? '' ) : '',
5657
width: 1280,
5758
height: 720,
5859
},

src/utils/blog.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,16 @@ export const getPosts = async ( pageNo = 1 ) => {
4040
* @return {Promise<void>}
4141
*/
4242
export const getPost = async ( postSlug = '' ) => {
43-
return await axios.get( `${ GET_POST_ENDPOINT }?post_slug=${ postSlug }` )
43+
return await axios.get( `${ GET_POST_ENDPOINT }?slug=${ postSlug }&_embed` )
4444
.then( res => {
45-
if ( 200 === res.data.status ) {
46-
return res;
45+
if ( 200 === res.status ) {
46+
return res.data;
4747
} else {
48-
return {
49-
post_data: {},
50-
error: 'Post not found',
51-
};
48+
return [];
5249
}
5350
} )
5451
.catch( err => {
5552
console.log( err.response.data.message )
56-
return {
57-
post_data: {},
58-
error: err.response.data.message
59-
};
53+
return [];
6054
} );
6155
};

src/utils/constants/endpoints.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const HEADER_FOOTER_ENDPOINT = `${ process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL }/wp-json/rae/v1/header-footer?header_location_id=hcms-menu-header&footer_location_id=hcms-menu-footer`;
22
export const GET_POSTS_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/posts`;
3-
export const GET_POST_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/rae/v1/post`;
3+
export const GET_POST_ENDPOINT = `${process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL}/wp-json/wp/v2/posts`;
44

55
/**
66
* Cart

src/utils/miscellaneous.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ export const replaceBackendWithFrontendUrl = ( data ) => {
2626
// First replace all the backend-url with front-end url
2727
let formattedData = data.replaceAll( process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL, process.env.NEXT_PUBLIC_SITE_URL );
2828

29-
// Replace only the upload urls for images back to back to back-end url, since images are hosted in the backend.
29+
// Replace only the upload urls for images to back-end url, since images are hosted in the backend.
3030
return formattedData.replaceAll( `${ process.env.NEXT_PUBLIC_SITE_URL }/wp-content/uploads`, `${ process.env.NEXT_PUBLIC_WORDPRESS_SITE_URL }/wp-content/uploads` );
3131
}
32+
33+
/**
34+
* Get Formatted Date.
35+
* @param {String} theDate The date to be formatted.
36+
* @param {String} locales locales.
37+
*
38+
* @return {string} Formatted Date.
39+
*/
40+
export const getFormattedDate = ( theDate = '', locales = 'en-us' ) => {
41+
const options = { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' };
42+
return new Date( theDate ).toLocaleDateString( locales, options );
43+
};

src/utils/slug.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const isCustomPageUri = ( uri ) => {
2626
* @param field
2727
* @return {{notFound: boolean}|*|{redirect: {destination: string, statusCode: number}}}
2828
*/
29-
export const handleRedirectsAndReturnData = ( defaultProps, data, field ) => {
29+
export const handleRedirectsAndReturnData = ( defaultProps, data, field = '' ) => {
3030

3131
// If no data is available then redirect to 503.
3232
if ( isEmpty( data ) ) {

0 commit comments

Comments
 (0)