Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Page SEO #583

Merged
merged 29 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f347a3f
Add Page SEO
kelvinkipruto Sep 14, 2023
7746a66
Fix canonical url
kelvinkipruto Sep 14, 2023
3c8cb1c
Update jest mock value
kelvinkipruto Sep 14, 2023
e4db579
Update Docker build mongo url
kelvinkipruto Sep 14, 2023
6e71624
Confitional url
kelvinkipruto Sep 14, 2023
5ca44ff
Config url
kelvinkipruto Sep 14, 2023
2069047
clone db before docker build
kelvinkipruto Sep 15, 2023
9eb3042
Clone DB before docker build
kelvinkipruto Sep 15, 2023
df89686
Add review app name
kelvinkipruto Sep 15, 2023
558112f
Use codeforafrica db name
kelvinkipruto Sep 15, 2023
a5b882b
Return empty object from props
kelvinkipruto Sep 15, 2023
34e7a55
Remove cannocial
kelvinkipruto Sep 15, 2023
5c094bc
Add Next public url
kelvinkipruto Sep 15, 2023
e09f19b
Check for empty image
kelvinkipruto Sep 15, 2023
96e5fff
Remove unnecessary changes
kelvinkipruto Sep 15, 2023
c38b8d4
Handle empty props
kelvinkipruto Sep 15, 2023
060cc6d
Remove unnecessary env variable
kelvinkipruto Sep 15, 2023
7f2b2db
Misc Fixes
kelvinkipruto Sep 15, 2023
e01ab92
Misc Fixes
kelvinkipruto Sep 15, 2023
4c48ad6
Fix SEO title and description
kelvinkipruto Sep 18, 2023
88768df
Fix not found pages
kelvinkipruto Sep 18, 2023
7182df8
Add default values for error pages
kelvinkipruto Sep 19, 2023
32a9da1
Default value for error pages
kelvinkipruto Sep 19, 2023
51d56d9
Default value for error pages
kelvinkipruto Sep 19, 2023
0fb6c46
Add notfound
kelvinkipruto Sep 19, 2023
939529a
Merge branch 'develop' into ft/codeforafrica-seo
kelvinkipruto Sep 20, 2023
c2a29e3
Remove unnecessary error props
kelvinkipruto Sep 20, 2023
fb1cac0
Remove unnecessary spaces
kelvinkipruto Sep 20, 2023
925bcb2
Merge branch 'develop' into ft/codeforafrica-seo
kelvinkipruto Sep 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/codeforafrica/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ global.TextDecoder = jest.fn().mockImplementation(() => ({
decode: jest.fn(),
}));

process.env.NEXT_PUBLIC_APP_URL = "http://localhost:3000";

jest.mock("next/router", () => ({
useRouter: jest.fn().mockImplementation(() => ({
asPath: "",
Expand Down
2 changes: 1 addition & 1 deletion apps/codeforafrica/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default buildConfig({
},
}),
seo({
collections: [],
collections: ["pages"],
globals: [],
uploadsCollection: "media",
generateTitle: ({ doc }: any) => doc?.title?.value as string,
Expand Down
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/lib/data/common/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import blockify from "@/codeforafrica/lib/data/blockify";
import pagify from "@/codeforafrica/lib/data/pagify";
import getPageSeoFromMeta from "@/codeforafrica/lib/data/seo";
import { imageFromMedia } from "@/codeforafrica/lib/data/utils";

function getNavBar(settings) {
Expand Down Expand Up @@ -161,10 +162,12 @@ export async function getPageProps(api, context) {
const navbar = getNavBar(settings);
const footer = getFooter(settings);

const seo = getPageSeoFromMeta(page, settings);
return {
blocks,
footer,
navbar,
seo,
};
}

Expand Down
67 changes: 67 additions & 0 deletions apps/codeforafrica/src/lib/data/seo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import site from "@/codeforafrica/utils/site";

function stringifyDescription(description) {
if (!Array.isArray(description)) {
return "";
}
return description.reduce((result, item) => {
if (item.text) {
// eslint-disable-next-line no-param-reassign
result += item.text;
}

if (Array.isArray(item.children)) {
// eslint-disable-next-line no-param-reassign
result += stringifyDescription(item.children);
}
return result;
}, "");
}

export default function getPageSeoFromMeta(page, settings) {
const { title: pageTitle, meta: pageMeta } = page;
const {
title: metaTitle,
description: metaDescription,
image = {},
} = pageMeta;
const { title: siteTitle, description: siteDescription } = settings;
const title =
metaTitle ||
pageTitle ||
siteTitle ||
process.env.NEXT_PUBLIC_APP_NAME ||
null;
const description =
metaDescription || stringifyDescription(siteDescription) || null;
const titleTemplate = siteTitle ? `%s | ${siteTitle}` : null;
const defaultTitle = siteTitle || null;
const canonical = site.url.replace(/\/+$/, "");
const openGraph = {
title,
description,
type: "website",
kelvinkipruto marked this conversation as resolved.
Show resolved Hide resolved
site_name: siteTitle,
};
if (image.url) {
const { alt, height, mimeType: type, url, width } = image;
openGraph.images = [
{
alt: alt || title || defaultTitle,
height,
type,
url,
width,
},
];
}

return {
title,
titleTemplate,
defaultTitle,
description,
canonical,
openGraph,
};
}