Skip to content

Commit

Permalink
feat: add JSON-LD structured data (#260)
Browse files Browse the repository at this point in the history
- Add author profile to SITE variables
- Add JSON-LD to Layout.astro
  • Loading branch information
davlgd authored Jul 14, 2024
1 parent 242729a commit 8fbb0b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Site, SocialObjects } from "./types";
export const SITE: Site = {
website: "https://astro-paper.pages.dev/", // replace this with your deployed domain
author: "Sat Naing",
profile: "https://satnaing.dev/",
desc: "A minimal, responsive and SEO-friendly Astro blog theme.",
title: "AstroPaper",
ogImage: "astropaper-og.jpg",
Expand Down
24 changes: 24 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const googleSiteVerification = import.meta.env.PUBLIC_GOOGLE_SITE_VERIFICATION;
export interface Props {
title?: string;
author?: string;
profile?: string;
description?: string;
ogImage?: string;
canonicalURL?: string;
Expand All @@ -19,6 +20,7 @@ export interface Props {
const {
title = SITE.title,
author = SITE.author,
profile = SITE.profile,
description = SITE.desc,
ogImage = SITE.ogImage,
canonicalURL = new URL(Astro.url.pathname, Astro.site).href,
Expand All @@ -31,6 +33,22 @@ const socialImageURL = new URL(
ogImage ?? SITE.ogImage ?? "og.png",
Astro.url.origin
).href;
const structuredData = {
"@context": "https://schema.org",
"@type": "BlogPosting",
headline: `${title}`,
image: `${socialImageURL}`,
datePublished: `${pubDatetime?.toISOString()}`,
...(modDatetime && { dateModified: modDatetime.toISOString() }),
author: [
{
"@type": "Person",
name: `${author}`,
url: `${profile}`,
},
],
};
---

<!doctype html>
Expand Down Expand Up @@ -83,6 +101,12 @@ const socialImageURL = new URL(
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={socialImageURL} />

<!-- Google JSON-LD Structured data -->
<script
type="application/ld+json"
set:html={JSON.stringify(structuredData)}
/>

<!-- Google Font -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type socialIcons from "@assets/socialIcons";
export type Site = {
website: string;
author: string;
profile: string;
desc: string;
title: string;
ogImage?: string;
Expand Down

0 comments on commit 8fbb0b4

Please sign in to comment.