From aec1306eaab4dbb007ce15ce6fc2b0c640a4bb0f Mon Sep 17 00:00:00 2001 From: Clemence Kyara Date: Fri, 16 Aug 2024 16:15:33 +0300 Subject: [PATCH] Use next-seo and payload to pull seo and analytics settings SEO information will for the following order: 1. Post SEO wins 2. Page SEO wins whenever there isn't Post SEO 2. Settings/Site SEO wins whenever there isn't Page SEO --- apps/roboshield/payload-types.ts | 8 ++ apps/roboshield/payload.config.ts | 22 ++--- apps/roboshield/src/components/Page/Page.tsx | 5 +- apps/roboshield/src/lib/data/common/index.ts | 20 +++-- apps/roboshield/src/lib/data/seo.ts | 94 ++++++++++++++++++++ apps/roboshield/src/lib/payload/index.ts | 6 +- apps/roboshield/src/next-seo.config.js | 26 ++++++ apps/roboshield/src/utils/site.ts | 25 ++++++ 8 files changed, 186 insertions(+), 20 deletions(-) create mode 100644 apps/roboshield/src/lib/data/seo.ts create mode 100644 apps/roboshield/src/next-seo.config.js create mode 100644 apps/roboshield/src/utils/site.ts diff --git a/apps/roboshield/payload-types.ts b/apps/roboshield/payload-types.ts index 9ca401cb1..71e1419a5 100644 --- a/apps/roboshield/payload-types.ts +++ b/apps/roboshield/payload-types.ts @@ -390,6 +390,9 @@ export interface SettingsSite { title: string; embedCode: string; }; + analytics?: { + analyticsId?: string | null; + }; initiative: { title: string; description: { @@ -411,6 +414,11 @@ export interface SettingsSite { }[] | null; }; + meta?: { + title?: string | null; + description?: string | null; + image?: string | Media | null; + }; updatedAt?: string | null; createdAt?: string | null; } diff --git a/apps/roboshield/payload.config.ts b/apps/roboshield/payload.config.ts index 487f2b6da..1b9a0c6d3 100644 --- a/apps/roboshield/payload.config.ts +++ b/apps/roboshield/payload.config.ts @@ -1,19 +1,19 @@ -import { buildConfig } from "payload/config"; -import { slateEditor } from "@payloadcms/richtext-slate"; -import { mongooseAdapter } from "@payloadcms/db-mongodb"; +import { loadEnvConfig } from "@next/env"; import { webpackBundler } from "@payloadcms/bundler-webpack"; -import { CollectionConfig, GlobalConfig } from "payload/types"; +import { mongooseAdapter } from "@payloadcms/db-mongodb"; import { cloudStorage } from "@payloadcms/plugin-cloud-storage"; -import Site from "./src/payload/globals/Site"; +import { s3Adapter } from "@payloadcms/plugin-cloud-storage/s3"; +import nestedDocs from "@payloadcms/plugin-nested-docs"; +import seo from "@payloadcms/plugin-seo"; +import { slateEditor } from "@payloadcms/richtext-slate"; +import { buildConfig } from "payload/config"; +import { CollectionConfig, GlobalConfig } from "payload/types"; +import { Config } from "./payload-types"; import Media from "./src/payload/collections/Media"; import Pages from "./src/payload/collections/Pages"; -import seo from "@payloadcms/plugin-seo"; -import nestedDocs from "@payloadcms/plugin-nested-docs"; -import { s3Adapter } from "@payloadcms/plugin-cloud-storage/s3"; import Users from "./src/payload/collections/Users"; +import Site from "./src/payload/globals/Site"; import { defaultLocale, locales } from "./src/payload/utils/locales"; -import { loadEnvConfig } from "@next/env"; -import { Config } from "./payload-types"; const projectDir = process.cwd(); loadEnvConfig(projectDir); @@ -109,7 +109,7 @@ export default buildConfig({ }), seo({ collections: ["pages"], - globals: [], + globals: ["settings-site"], uploadsCollection: "media", generateTitle: ({ doc }: any) => doc?.title?.value as string, generateURL: ({ doc }: any) => diff --git a/apps/roboshield/src/components/Page/Page.tsx b/apps/roboshield/src/components/Page/Page.tsx index f2eb50082..8056f0d5e 100644 --- a/apps/roboshield/src/components/Page/Page.tsx +++ b/apps/roboshield/src/components/Page/Page.tsx @@ -1,4 +1,5 @@ import React from "react"; +import { NextSeo, NextSeoProps } from "next-seo"; import Footer from "@/roboshield/components/Footer"; import type { FooterProps } from "@/roboshield/components/Footer"; @@ -30,6 +31,7 @@ interface Props { children?: React.ReactNode; navbar?: Navbar; footer?: FooterProps; + seo?: NextSeoProps; slug?: string; } @@ -37,9 +39,10 @@ interface Props { * While the layout (navbar, footer) remain the same, the main component * changes from page to page. Use `slug` to track page changes. */ -function Page({ children, footer, navbar, slug }: Props) { +function Page({ children, footer, navbar, seo, slug }: Props) { return ( <> + {navbar ? : null} {children ?
{children}
: null} {footer ?