fix: some meta tags

This commit is contained in:
RizqiSyahrendra 2025-03-04 18:30:59 +07:00
parent 3373b75557
commit cab69a41e9
12 changed files with 111 additions and 24 deletions

View File

@ -3,44 +3,57 @@ import HeroOther from "@/components/HeroOther";
import Page from "@/components/Pages/Page"; import Page from "@/components/Pages/Page";
import { fetchBlogDetail } from "@/services/payload/blog"; import { fetchBlogDetail } from "@/services/payload/blog";
import { fetchPageBySlug } from "@/services/payload/page"; import { fetchPageBySlug } from "@/services/payload/page";
import { getDefaultMetadata } from "@/utils/metadata";
import { Metadata } from "next"; import { Metadata } from "next";
import { Suspense } from "react"; import { Suspense } from "react";
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> { export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
const name = "Cochise Oncology"; const defaultMetadata = await getDefaultMetadata();
const name = defaultMetadata.openGraph?.siteName ?? "";
let title = "Page"; let title = "Page";
let description = "Page"; let description = "Page";
let publishedAt = "";
let updatedAt = "";
let imgUrl = ""; let imgUrl = "";
const slug = (await params).slug; const slug = (await params).slug;
const blog = await fetchBlogDetail(slug); const blog = await fetchBlogDetail(slug);
// check for blog data
if (!!blog) { if (!!blog) {
// check for blog data
title = `${!!blog.data?.meta?.title ? blog.data?.meta?.title : blog.data.title} - ${name}`; title = `${!!blog.data?.meta?.title ? blog.data?.meta?.title : blog.data.title} - ${name}`;
description = `${!!blog.data?.meta?.description ? blog.data?.meta?.description : blog.data.title} - ${name}`; description = `${!!blog.data?.meta?.description ? blog.data?.meta?.description : blog.data.title} - ${name}`;
imgUrl = blog.imgUrl; imgUrl = blog.imgUrl;
} publishedAt = blog.data.createdAt;
updatedAt = blog.data.updatedAt;
} else {
// check for page data when blog is not found // check for page data when blog is not found
if (!blog) {
const page = await fetchPageBySlug({ slug }); const page = await fetchPageBySlug({ slug });
if (!!page) { if (!!page) {
title = `${!!page?.meta?.title ? page?.meta?.title : page.title} - ${name}`; title = `${!!page?.meta?.title ? page?.meta?.title : page.title} - ${name}`;
description = `${!!page?.meta?.description ? page?.meta?.description : page.title} - ${name}`; description = `${!!page?.meta?.description ? page?.meta?.description : page.title} - ${name}`;
imgUrl = page.heroImg?.url; imgUrl = page.heroImg?.url;
publishedAt = page.createdAt;
updatedAt = page.updatedAt;
} }
} }
return { defaultMetadata.title = title;
title: title, defaultMetadata.description = description;
description: description, if (!!defaultMetadata.openGraph) {
openGraph: { defaultMetadata.openGraph.title = title;
title: title, // @ts-ignore
description: description, defaultMetadata.openGraph.type = "article";
images: !!imgUrl ? { url: imgUrl } : undefined, defaultMetadata.openGraph.description = description;
}, defaultMetadata.openGraph.title = title;
defaultMetadata.openGraph.images = !!imgUrl ? { url: imgUrl } : undefined;
}
defaultMetadata.other = {
"article:published_time": publishedAt,
"article:modified_time": updatedAt,
}; };
return defaultMetadata;
} }
export default async function SinglePage({ params }: { params: Promise<{ slug: string }> }) { export default async function SinglePage({ params }: { params: Promise<{ slug: string }> }) {

View File

@ -3,9 +3,11 @@ import Footer from "@/components/Footer";
import Header from "@/components/Header"; import Header from "@/components/Header";
import InitialScript from "@/components/InitialScript"; import InitialScript from "@/components/InitialScript";
import { navMenuData } from "@/data/menu"; import { navMenuData } from "@/data/menu";
import { getDefaultMetadata } from "@/utils/metadata";
import "@public/assets/css/styles.css"; import "@public/assets/css/styles.css";
import "jarallax/dist/jarallax.min.css"; import "jarallax/dist/jarallax.min.css";
import { Metadata } from "next";
import { Roboto } from "next/font/google"; import { Roboto } from "next/font/google";
import "photoswipe/dist/photoswipe.css"; import "photoswipe/dist/photoswipe.css";
import "react-modal-video/css/modal-video.css"; import "react-modal-video/css/modal-video.css";
@ -15,6 +17,11 @@ import "tippy.js/dist/tippy.css";
const roboto = Roboto({ subsets: ["latin"] }); const roboto = Roboto({ subsets: ["latin"] });
export async function generateMetadata(): Promise<Metadata> {
const metadata = await getDefaultMetadata();
return metadata;
}
export default function MainLayout({ export default function MainLayout({
children, children,
}: Readonly<{ }: Readonly<{

View File

@ -1,4 +1,4 @@
import formatSlug from "@/utils/formatSlug"; import formatSlug from "@/utils/payload/formatSlug";
import type { CollectionConfig } from "payload"; import type { CollectionConfig } from "payload";
export const BlogCategories: CollectionConfig = { export const BlogCategories: CollectionConfig = {

View File

@ -1,4 +1,4 @@
import formatSlug from "@/utils/formatSlug"; import formatSlug from "@/utils/payload/formatSlug";
import type { CollectionConfig } from "payload"; import type { CollectionConfig } from "payload";
export const BlogTags: CollectionConfig = { export const BlogTags: CollectionConfig = {

View File

@ -1,6 +1,6 @@
import type { CollectionConfig } from "payload"; import formatSlug from "@/utils/payload/formatSlug";
import { lexicalEditor } from "@payloadcms/richtext-lexical"; import { lexicalEditor } from "@payloadcms/richtext-lexical";
import formatSlug from "@/utils/formatSlug"; import type { CollectionConfig } from "payload";
export const Blogs: CollectionConfig = { export const Blogs: CollectionConfig = {
slug: "blogs", slug: "blogs",
@ -59,8 +59,8 @@ export const Blogs: CollectionConfig = {
type: "textarea", type: "textarea",
}, },
{ {
name: "cannonical_url", name: "canonical_url",
label: "Cannonical Url", label: "Canonical Url",
type: "text", type: "text",
}, },
], ],
@ -74,6 +74,24 @@ export const Blogs: CollectionConfig = {
position: "sidebar", position: "sidebar",
}, },
}, },
{
name: "createdBy",
type: "relationship",
relationTo: "users",
hidden: true,
// hooks: {
// beforeChange: [setAuthor],
// },
},
{
name: "updatedBy",
type: "relationship",
relationTo: "users",
hidden: true,
// hooks: {
// beforeChange: [setAuthor],
// },
},
], ],
admin: { admin: {
hideAPIURL: true, hideAPIURL: true,

View File

@ -7,7 +7,7 @@ import { GoogleReviewBlock } from "@/blocks/GoogleReview";
import { HorizontalImageContentBlock } from "@/blocks/HorizontalImageContent"; import { HorizontalImageContentBlock } from "@/blocks/HorizontalImageContent";
import { ImageSliderBlock } from "@/blocks/ImageSlider"; import { ImageSliderBlock } from "@/blocks/ImageSlider";
import { OurTeamBlock } from "@/blocks/OurTeam"; import { OurTeamBlock } from "@/blocks/OurTeam";
import formatSlug from "@/utils/formatSlug"; import formatSlug from "@/utils/payload/formatSlug";
import { CollectionConfig } from "payload"; import { CollectionConfig } from "payload";
export const Pages: CollectionConfig = { export const Pages: CollectionConfig = {

View File

@ -11,5 +11,10 @@ export const Users: CollectionConfig = {
fields: [ fields: [
// Email added by default // Email added by default
// Add more fields as needed // Add more fields as needed
{
name: "name",
label: "Name",
type: "text",
},
], ],
}; };

View File

@ -8,7 +8,10 @@ export function middleware(request: NextRequest) {
const host = request.headers.get("x-forwarded-host") || request.nextUrl.hostname; const host = request.headers.get("x-forwarded-host") || request.nextUrl.hostname;
const path = request.nextUrl.pathname + request.nextUrl.search; const path = request.nextUrl.pathname + request.nextUrl.search;
// Construct the full URL // Construct the full URL
const fullUrl = `${protocol}://${host}${path}`; const mainUrl = `${protocol}://${host}`;
const fullUrl = `${mainUrl}${path}`;
request.headers.set("x-main-url", mainUrl);
request.headers.set("x-full-url", fullUrl); request.headers.set("x-full-url", fullUrl);
return NextResponse.next({ return NextResponse.next({

View File

@ -83,6 +83,7 @@ export interface UserAuthOperations {
*/ */
export interface User { export interface User {
id: number; id: number;
name?: string | null;
updatedAt: string; updatedAt: string;
createdAt: string; createdAt: string;
email: string; email: string;
@ -143,9 +144,11 @@ export interface Blog {
meta?: { meta?: {
title?: string | null; title?: string | null;
description?: string | null; description?: string | null;
cannonical_url?: string | null; canonical_url?: string | null;
}; };
is_published?: boolean | null; is_published?: boolean | null;
createdBy?: (number | null) | User;
updatedBy?: (number | null) | User;
updatedAt: string; updatedAt: string;
createdAt: string; createdAt: string;
} }
@ -593,6 +596,7 @@ export interface PayloadMigration {
* via the `definition` "users_select". * via the `definition` "users_select".
*/ */
export interface UsersSelect<T extends boolean = true> { export interface UsersSelect<T extends boolean = true> {
name?: T;
updatedAt?: T; updatedAt?: T;
createdAt?: T; createdAt?: T;
email?: T; email?: T;
@ -638,9 +642,11 @@ export interface BlogsSelect<T extends boolean = true> {
| { | {
title?: T; title?: T;
description?: T; description?: T;
cannonical_url?: T; canonical_url?: T;
}; };
is_published?: T; is_published?: T;
createdBy?: T;
updatedBy?: T;
updatedAt?: T; updatedAt?: T;
createdAt?: T; createdAt?: T;
} }

21
src/utils/metadata.ts Normal file
View File

@ -0,0 +1,21 @@
import { Metadata } from "next";
import { headers } from "next/headers";
export async function getDefaultMetadata(): Promise<Metadata> {
const headersList = await headers();
const mainUrl = headersList.get("x-main-url") ?? "";
const fullUrl = headersList.get("x-full-url") ?? "";
return {
metadataBase: new URL(mainUrl),
openGraph: {
type: "website",
locale: "en_US",
url: fullUrl,
siteName: "Cochise Oncology",
},
alternates: {
canonical: "./",
},
};
}

View File

@ -0,0 +1,14 @@
import { FieldHook } from "payload";
const setAuthor: FieldHook = ({ req, operation, data, value }) => {
// if (req.user && !!data) {
// if (operation === "create") {
// data.createdBy = req.user.id;
// }
// data.updatedBy = req.user.id;
// }
return data;
};
export default setAuthor;