AI Resource Pro / website · airesourcepro.com / src / lib / seo.ts
| 1 | // Page metadata and structured data shared by every page: canonical URL, Open Graph and X cards, and |
| 2 | // schema.org JSON-LD. Share images are 1200 x 630 JPEGs in public/og, made from the covers by `npm run covers`. |
| 3 | import fs from "fs"; |
| 4 | import path from "path"; |
| 5 | import type { Metadata } from "next"; |
| 6 | import type { Track } from "@/lib/tracks"; |
| 7 | import { lastModified, type TutorialMeta } from "@/lib/tutorial-types"; |
| 8 | |
| 9 | export const SITE_URL = "https://airesourcepro.com"; |
| 10 | export const SITE_NAME = "AI Resource Pro"; |
| 11 | export const SITE_TITLE = "AI Resource Pro | Learn AI by building with it"; |
| 12 | export const SITE_DESCRIPTION = |
| 13 | "Free, hands-on AI tutorials and videos: Claude and Codex best practices, computer-use agents, and open-weight AI models on your own GPU."; |
| 14 | export const SITE_OG_IMAGE = "/og/site.jpg"; |
| 15 | // Describes the site share image for screen readers (pages with their own cover use their title) |
| 16 | export const SITE_OG_IMAGE_ALT = "AI Resource PRO: Learn AI by building with it. The Impossible A logo drawn in black-and-white particles."; |
| 17 | // The RSS feed, announced in every page's head (app/feed.xml) |
| 18 | export const RSS_ALTERNATE = { "application/rss+xml": [{ url: "/feed.xml", title: `${SITE_NAME} tutorials` }] }; |
| 19 | |
| 20 | // Who writes the tutorials: the AI Resource Pro team (no personal byline) |
| 21 | export const AUTHOR = { |
| 22 | name: "AI Resource Pro", |
| 23 | path: "/about", |
| 24 | photo: "/android-chrome-192x192.png", |
| 25 | }; |
| 26 | |
| 27 | const absolute = (p: string) => new URL(p, SITE_URL).toString(); |
| 28 | |
| 29 | // A cover's share image (public/og/<same path>.jpg), or the site's when that cover has none yet |
| 30 | export function ogImageFor(cover: string): string { |
| 31 | const og = cover.replace(/^\/covers\//, "/og/").replace(/\.webp$/, ".jpg"); |
| 32 | return fs.existsSync(path.join(process.cwd(), "public", og)) ? og : SITE_OG_IMAGE; |
| 33 | } |
| 34 | |
| 35 | type PageMeta = { |
| 36 | title: string; |
| 37 | description: string; |
| 38 | // Site path, e.g. "/claude"; becomes the canonical URL |
| 39 | path: string; |
| 40 | image?: string; |
| 41 | // The home page's title goes out as is instead of through the "%s | AI Resource Pro" template |
| 42 | absoluteTitle?: boolean; |
| 43 | type?: "website" | "article"; |
| 44 | publishedTime?: string; |
| 45 | modifiedTime?: string; |
| 46 | noindex?: boolean; |
| 47 | }; |
| 48 | |
| 49 | export function pageMetadata({ |
| 50 | title, |
| 51 | description, |
| 52 | path: pagePath, |
| 53 | image = SITE_OG_IMAGE, |
| 54 | absoluteTitle, |
| 55 | type = "website", |
| 56 | publishedTime, |
| 57 | modifiedTime, |
| 58 | noindex, |
| 59 | }: PageMeta): Metadata { |
| 60 | const images = [{ url: image, width: 1200, height: 630, alt: image === SITE_OG_IMAGE ? SITE_OG_IMAGE_ALT : title }]; |
| 61 | return { |
| 62 | title: absoluteTitle ? { absolute: title } : title, |
| 63 | description, |
| 64 | alternates: { canonical: pagePath, types: RSS_ALTERNATE }, |
| 65 | openGraph: { |
| 66 | title, |
| 67 | description, |
| 68 | url: pagePath, |
| 69 | siteName: SITE_NAME, |
| 70 | locale: "en_US", |
| 71 | images, |
| 72 | ...(type === "article" ? { type, publishedTime, modifiedTime, authors: [absolute(AUTHOR.path)] } : { type }), |
| 73 | }, |
| 74 | twitter: { card: "summary_large_image", title, description, images }, |
| 75 | ...(type === "article" ? { authors: [{ name: AUTHOR.name, url: AUTHOR.path }] } : {}), |
| 76 | ...(noindex ? { robots: { index: false, follow: true } } : {}), |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | // ---------- JSON-LD ---------- |
| 81 | |
| 82 | const ORGANIZATION_ID = `${SITE_URL}/#organization`; |
| 83 | |
| 84 | const publisher = { |
| 85 | "@type": "Organization", |
| 86 | "@id": ORGANIZATION_ID, |
| 87 | name: SITE_NAME, |
| 88 | url: SITE_URL, |
| 89 | logo: { "@type": "ImageObject", url: absolute("/android-chrome-512x512.png"), width: 512, height: 512 }, |
| 90 | }; |
| 91 | |
| 92 | export function siteJsonLd() { |
| 93 | return [ |
| 94 | { |
| 95 | "@context": "https://schema.org", |
| 96 | ...publisher, |
| 97 | description: SITE_DESCRIPTION, |
| 98 | }, |
| 99 | { |
| 100 | "@context": "https://schema.org", |
| 101 | "@type": "WebSite", |
| 102 | "@id": `${SITE_URL}/#website`, |
| 103 | url: SITE_URL, |
| 104 | name: SITE_NAME, |
| 105 | description: SITE_DESCRIPTION, |
| 106 | inLanguage: "en", |
| 107 | publisher: { "@id": ORGANIZATION_ID }, |
| 108 | }, |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | export function breadcrumbJsonLd(items: { name: string; path: string }[]) { |
| 113 | return { |
| 114 | "@context": "https://schema.org", |
| 115 | "@type": "BreadcrumbList", |
| 116 | itemListElement: items.map((item, i) => ({ |
| 117 | "@type": "ListItem", |
| 118 | position: i + 1, |
| 119 | name: item.name, |
| 120 | item: absolute(item.path), |
| 121 | })), |
| 122 | }; |
| 123 | } |
| 124 | |
| 125 | // A track or the full list: the page and the tutorials it lists, in order |
| 126 | export function collectionJsonLd({ name, description, path: pagePath, tutorials }: { |
| 127 | name: string; |
| 128 | description: string; |
| 129 | path: string; |
| 130 | tutorials: TutorialMeta[]; |
| 131 | }) { |
| 132 | return { |
| 133 | "@context": "https://schema.org", |
| 134 | "@type": "CollectionPage", |
| 135 | name, |
| 136 | description, |
| 137 | url: absolute(pagePath), |
| 138 | isPartOf: { "@id": `${SITE_URL}/#website` }, |
| 139 | mainEntity: { |
| 140 | "@type": "ItemList", |
| 141 | itemListElement: tutorials.map((t, i) => ({ |
| 142 | "@type": "ListItem", |
| 143 | position: i + 1, |
| 144 | url: absolute(`/${t.track}/${t.slug}`), |
| 145 | name: t.title, |
| 146 | })), |
| 147 | }, |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | // Tutorials are dated by day only; Google wants a time zone, so send the start of that day in New York |
| 152 | function startOfDay(day: string): string { |
| 153 | // 05:00 UTC is just after midnight in New York, so DST changeover days get midnight's own offset |
| 154 | const nearMidnight = new Date(`${day}T05:00:00Z`); |
| 155 | const zone = new Intl.DateTimeFormat("en-US", { timeZone: "America/New_York", timeZoneName: "longOffset" }) |
| 156 | .formatToParts(nearMidnight) |
| 157 | .find((p) => p.type === "timeZoneName")?.value; |
| 158 | const offset = zone?.replace("GMT", "") || "+00:00"; |
| 159 | return `${day}T00:00:00${offset}`; |
| 160 | } |
| 161 | |
| 162 | export function tutorialJsonLd(tutorial: TutorialMeta, track: Track) { |
| 163 | const url = absolute(`/${tutorial.track}/${tutorial.slug}`); |
| 164 | const article = { |
| 165 | "@context": "https://schema.org", |
| 166 | "@type": "TechArticle", |
| 167 | headline: tutorial.title, |
| 168 | description: tutorial.description, |
| 169 | url, |
| 170 | mainEntityOfPage: url, |
| 171 | image: [absolute(ogImageFor(tutorial.cover)), absolute(tutorial.cover)], |
| 172 | ...(tutorial.publishedAt ? { datePublished: startOfDay(tutorial.publishedAt), dateModified: startOfDay(lastModified(tutorial)) } : {}), |
| 173 | author: { "@id": ORGANIZATION_ID }, |
| 174 | publisher, |
| 175 | articleSection: track.name, |
| 176 | proficiencyLevel: tutorial.level, |
| 177 | timeRequired: `PT${tutorial.minutes}M`, |
| 178 | inLanguage: "en", |
| 179 | isAccessibleForFree: true, |
| 180 | }; |
| 181 | if (!tutorial.video) return [article]; |
| 182 | const video = { |
| 183 | "@context": "https://schema.org", |
| 184 | "@type": "VideoObject", |
| 185 | name: tutorial.title, |
| 186 | description: tutorial.description, |
| 187 | thumbnailUrl: `https://airesourcepro.com/media/video/${tutorial.video}/poster.jpg`, |
| 188 | ...(tutorial.videoUploadedAt ? { uploadDate: tutorial.videoUploadedAt } : {}), |
| 189 | contentUrl: `https://airesourcepro.com/media/video/${tutorial.video}/${tutorial.video}-1080p.mp4`, |
| 190 | publisher, |
| 191 | }; |
| 192 | return [article, video]; |
| 193 | } |