Skip to content

Commit 6da7215

Browse files
committed
Added og to sitemap + optimized alternates logic
1 parent acf05c2 commit 6da7215

7 files changed

Lines changed: 21 additions & 22 deletions

File tree

‎src/app/categories/[category]/page.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PageLayout from "@/components/layout"
22
import Search from "@/components/pages/search"
33
import { KEYWORDS, POSTS_IN_SEARCH } from "@/lib/constants"
44
import { getAllCategories, getPostsByCategory } from "@/lib/helpers"
5-
import { absoluteURL } from "@/lib/helpers/seo"
5+
import { absoluteURL, metaAlternatives } from "@/lib/helpers/seo"
66
import { Metadata } from "next"
77
import { notFound } from "next/navigation"
88

@@ -63,9 +63,7 @@ export const generateMetadata = async({params, searchParams}: PageProps): Promis
6363
title: meta.title,
6464
description: meta.description,
6565
},
66-
alternates: {
67-
canonical: absoluteURL(`/categories/${category}`)
68-
}
66+
alternates: metaAlternatives(`/categories/${category}`)
6967
}
7068
}
7169

‎src/app/layout.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "./globals.css";
44
import { ThemeProvider } from "@/components/theme-provider";
55
import { KEYWORDS } from "@/lib/constants";
66
import { Toaster } from "@/components/ui/sonner";
7-
import { absoluteURL } from "@/lib/helpers/seo";
7+
import { absoluteURL, metaAlternatives } from "@/lib/helpers/seo";
88

99
const sora = Sora({
1010
variable: "--font-sora",
@@ -35,9 +35,7 @@ export const metadata: Metadata = {
3535
verification: {
3636
google: "fO_3worN0840UBpRpX9H9HVtu-U2Z3y5LFJT5qzXpUE"
3737
},
38-
alternates: {
39-
canonical: absoluteURL("/")
40-
},
38+
alternates: metaAlternatives("/"),
4139
openGraph: {
4240
title: "ArsenTech's Official Blog Website",
4341
description: "Your go-to hub for tech tutorials, antivirus comparisons, and in-depth guides for programmers, tech enthusiasts, and more tech related curious minds.",

‎src/app/page.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PageLayout from "@/components/layout";
22
import LandingPage from "@/components/pages/landing";
33
import { POSTS_PER_PAGE } from "@/lib/constants";
44
import { getAllPosts, getCategories } from "@/lib/helpers";
5-
import { absoluteURL } from "@/lib/helpers/seo";
5+
import { absoluteURL, metaAlternatives } from "@/lib/helpers/seo";
66
import type { Metadata } from "next";
77

88
interface HomepageProps{
@@ -19,9 +19,7 @@ export const generateMetadata = async({searchParams}: HomepageProps): Promise<Me
1919
previous: currentPage > 1 ? absoluteURL(`/?page=${currentPage - 1}`) : undefined,
2020
next: currentPage < totalPages ? absoluteURL(`/?page=${currentPage + 1}`) : undefined
2121
},
22-
alternates: {
23-
canonical: currentPage === 1 ? absoluteURL("/") : absoluteURL(`/?page=${currentPage}`)
24-
}
22+
alternates: metaAlternatives(currentPage === 1 ? "/" : `/?page=${currentPage}`)
2523
}
2624
}
2725
export default async function Home({searchParams}: HomepageProps) {

‎src/app/posts/[slug]/page.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PageLayout from "@/components/layout";
22
import BlogPost from "@/components/pages/blog-post";
33
import { KEYWORDS } from "@/lib/constants";
44
import { getAllSlugs, getPostBySlug, getRelatedPosts } from "@/lib/helpers";
5-
import { absoluteURL } from "@/lib/helpers/seo";
5+
import { absoluteURL, metaAlternatives } from "@/lib/helpers/seo";
66
import { Metadata } from "next";
77
import { notFound } from "next/navigation";
88

@@ -62,9 +62,7 @@ export const generateMetadata = async({params}: SinglePostPageProps): Promise<Me
6262
title: currPost.title,
6363
description: currPost.description,
6464
},
65-
alternates: {
66-
canonical: absoluteURL(`/posts/${slug}`)
67-
}
65+
alternates: metaAlternatives(`/posts/${slug}`)
6866
}
6967
}
7068

‎src/app/sitemap.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap>{
2525
url: absoluteURL(`/posts/${slug}`),
2626
lastModified: post?.date ?? new Date(),
2727
changeFrequency: "daily",
28-
priority: 0.8
28+
priority: 0.8,
29+
images: post ? [
30+
absoluteURL(`/api/og?title=${encodeURIComponent(post.title)}&description=${encodeURIComponent(post.description)}&date=${post.date.toISOString()}&bg=image`)
31+
] : undefined
2932
}
3033
}))
3134
const latestDate = searchPostPages.length>0 ? new Date(Math.max(...searchPostPages.map(p => new Date(p.lastModified as string).getTime()))) : new Date()
@@ -34,7 +37,8 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap>{
3437
url: SITE_URL,
3538
lastModified: latestDate,
3639
changeFrequency: "monthly",
37-
priority: 1
40+
priority: 1,
41+
images: [absoluteURL("/og.png")]
3842
},
3943
...searchPostPages,
4044
...searchTagPages,

‎src/app/tags/[tag]/page.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PageLayout from "@/components/layout"
22
import Search from "@/components/pages/search"
33
import { KEYWORDS, POSTS_IN_SEARCH } from "@/lib/constants"
44
import { getAllTags, getPostsByTag } from "@/lib/helpers"
5-
import { absoluteURL } from "@/lib/helpers/seo"
5+
import { absoluteURL, metaAlternatives } from "@/lib/helpers/seo"
66
import { Metadata } from "next"
77
import { notFound } from "next/navigation"
88

@@ -63,9 +63,7 @@ export const generateMetadata = async({params, searchParams}: PageProps): Promis
6363
title: meta.title,
6464
description: meta.description,
6565
},
66-
alternates: {
67-
canonical: absoluteURL(`/tags/${tag}`)
68-
}
66+
alternates: metaAlternatives(`/tags/${tag}`)
6967
}
7068
}
7169

‎src/lib/helpers/seo.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Metadata } from "next";
12
import { SITE_URL } from "../constants";
23

34
export function getValueFromKey(searchParams: URLSearchParams, key: string, fallbackValue=""):string{
@@ -18,3 +19,7 @@ export function getRangeAsText(page: number, pageSize: number, totalResults: num
1819

1920
return `Showing ${start} - ${end} of ${totalResults} ${totalResults <= 1 ? itemName : `${itemName}s`}`;
2021
}
22+
23+
export const metaAlternatives = (url?: string): Metadata["alternates"] => ({
24+
canonical: absoluteURL(url)
25+
})

0 commit comments

Comments
 (0)