From 5e818d804d5d38beff0f3754a006333752fd5082 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 22 Apr 2026 16:11:58 +0000 Subject: refactor: update schema handling and improve SEO metadata - Changed schema type from WithContext to Thing[] in Head, BaseLayout, and JsonLd components for better flexibility. - Added optional robots meta tag to Head component. - Updated JSON-LD generation in JsonLd component to include a structured payload. - Enhanced page and blog schemas to support breadcrumb and person schemas for improved SEO. - Introduced new utility schemas for website and person to streamline schema generation. - Refactored existing schemas to align with the new structure and ensure consistency across components. --- src/components/Head.astro | 9 ++++--- src/components/JsonLd.astro | 12 ++++++--- src/layouts/BaseLayout.astro | 9 ++++--- src/pages/404.astro | 20 ++++++++------ src/pages/blog/[...slug].astro | 44 +++++++++++++++++++++---------- src/pages/blog/index.astro | 20 ++++++++++---- src/pages/index.astro | 12 ++++----- src/utils/schemas/blogPostSchema.ts | 49 ++++++++++++++++++----------------- src/utils/schemas/blogSchema.ts | 46 +++++++++++++++++++------------- src/utils/schemas/breadcrumbSchema.ts | 21 +++++++++++++++ src/utils/schemas/ids.ts | 3 +++ src/utils/schemas/pageSchema.ts | 43 +++++++++++++++++++----------- src/utils/schemas/personSchema.ts | 17 ++++++++++++ src/utils/schemas/websiteSchema.ts | 23 ++++++++++++++++ 14 files changed, 227 insertions(+), 101 deletions(-) create mode 100644 src/utils/schemas/breadcrumbSchema.ts create mode 100644 src/utils/schemas/ids.ts create mode 100644 src/utils/schemas/personSchema.ts create mode 100644 src/utils/schemas/websiteSchema.ts diff --git a/src/components/Head.astro b/src/components/Head.astro index 3fded95..258162d 100644 --- a/src/components/Head.astro +++ b/src/components/Head.astro @@ -1,15 +1,16 @@ --- -import type { WithContext, Thing } from "schema-dts"; +import type { Thing } from "schema-dts"; import JsonLd from "./JsonLd.astro"; type Props = { readonly description: string; readonly preview: string; - readonly schema: WithContext; + readonly robots?: string; + readonly schema: Thing[]; readonly title: string; }; -const { description, preview, schema, title } = Astro.props; +const { description, preview, robots = "index, follow", schema, title } = Astro.props; const canonicalUrl = new URL(Astro.url.pathname, Astro.site); const previewUrl = new URL(preview, Astro.site); @@ -21,7 +22,7 @@ const previewUrl = new URL(preview, Astro.site); - + diff --git a/src/components/JsonLd.astro b/src/components/JsonLd.astro index b58efd7..d12cb78 100644 --- a/src/components/JsonLd.astro +++ b/src/components/JsonLd.astro @@ -1,12 +1,18 @@ --- -import type { WithContext, Thing } from "schema-dts"; +import type { Thing } from "schema-dts"; type Props = { - readonly schema: WithContext; + readonly schema: Thing[]; }; const { schema } = Astro.props; -const json = JSON.stringify(schema); + +const payload = { + "@context": "https://schema.org", + "@graph": schema, +}; + +const json = JSON.stringify(payload); --- diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index ed3baeb..a2f0327 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -1,5 +1,5 @@ --- -import type { WithContext, Thing } from "schema-dts"; +import type { Thing } from "schema-dts"; import Analytics from "../components/Analytics.astro"; import Head from "../components/Head.astro"; import Header from "../components/Header.astro"; @@ -9,15 +9,16 @@ type Props = { readonly description: string; readonly lang: string; readonly preview: string; - readonly schema: WithContext; + readonly robots?: string; + readonly schema: Thing[]; readonly title: string; }; -const { description, lang, preview, schema, title } = Astro.props; +const { description, lang, preview, robots, schema, title } = Astro.props; --- - +
diff --git a/src/pages/404.astro b/src/pages/404.astro index 3ec9feb..e76215e 100644 --- a/src/pages/404.astro +++ b/src/pages/404.astro @@ -8,16 +8,20 @@ const description = "The page you're looking for doesn't exist!"; const preview = config.og.defaultPreview; const lang = "en"; -const schema = pageSchema({ - siteUrl: new URL("/", Astro.site).toString(), - page: "/404", - title, - description, - lang, -}); +const siteUrl = new URL("/", Astro.site).toString(); + +const schema = [ + pageSchema({ + siteUrl, + page: "/404", + title, + description, + lang, + }), +]; --- - +

404

Page not found

diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro index 3bd2c61..5ba6b3b 100644 --- a/src/pages/blog/[...slug].astro +++ b/src/pages/blog/[...slug].astro @@ -1,9 +1,13 @@ --- import { type CollectionEntry, getCollection, render } from "astro:content"; -import Comments from "../../components/Comments.astro"; -import Layout from "../../layouts/BaseLayout.astro"; import blogPostSchema from "../../utils/schemas/blogPostSchema"; +import breadcrumbSchema from "../../utils/schemas/breadcrumbSchema"; +import Comments from "../../components/Comments.astro"; import dayjs from "dayjs"; +import Layout from "../../layouts/BaseLayout.astro"; +import personSchema from "../../utils/schemas/personSchema"; +import websiteSchema from "../../utils/schemas/websiteSchema"; +import { config } from "../../config"; type Props = CollectionEntry<"blog">; @@ -33,17 +37,31 @@ const dateModified = post.data.dateModified?.toISOString(); const datePublished = post.data.datePublished.toISOString(); const formattedDate = dayjs(post.data.datePublished.toString()).format("MMMM DD, YYYY"); -const schema = blogPostSchema({ - siteUrl: new URL("/", Astro.site).toString(), - dateModified, - datePublished, - description, - isBasedOn, - lang, - preview, - slug, - title, -}); +const siteUrl = new URL("/", Astro.site).toString(); + +const schema = [ + websiteSchema({ siteUrl, name: config.og.website, description, lang }), + personSchema({ siteUrl }), + blogPostSchema({ + siteUrl, + dateModified, + datePublished, + description, + isBasedOn, + lang, + preview, + slug, + title, + }), + breadcrumbSchema({ + siteUrl, + items: [ + { name: "Home", url: "/" }, + { name: "Blog", url: "/blog/" }, + { name: title, url: `/blog/${slug}` }, + ], + }), +]; ---