Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(kb) - add structured ld+json data to KB pages (#51498)

Kody Jackson89930014710b
diff --git a/apps/kb/src/layouts/ArticleLayout.astro b/apps/kb/src/layouts/ArticleLayout.astro
index 5e169613c5c..e8cb9479bd7 100644
--- a/apps/kb/src/layouts/ArticleLayout.astro
+++ b/apps/kb/src/layouts/ArticleLayout.astro
@@ -15,16 +15,17 @@ interface Props {
github_url?: string
viewGitHubLink?: string
date?: string
+ jsonLd?: Record<string, unknown> | Record<string, unknown>[]
}
-const { title, description, topics = [], github_url, viewGitHubLink, date } = Astro.props
+const { title, description, topics = [], github_url, viewGitHubLink, date, jsonLd } = Astro.props
function isKnownTopic(topic: string): topic is Topic {
return (TOPIC_NAMES as readonly string[]).includes(topic)
}
---
-<Layout title={title} description={description}>
+<Layout title={title} description={description} jsonLd={jsonLd}>
<article class="prose max-w-3xl mx-auto px-6 py-16">
<h1>{title}</h1>
{date && <p class="text-sm pb-4">{date}</p>}
diff --git a/apps/kb/src/layouts/Layout.astro b/apps/kb/src/layouts/Layout.astro
index 959404eef0b..0140669e673 100644
--- a/apps/kb/src/layouts/Layout.astro
+++ b/apps/kb/src/layouts/Layout.astro
@@ -3,13 +3,15 @@ import { genFaviconLinks } from 'common/MetaFavicons/icons'
import Footer from '../components/Footer.astro'
import { Header } from '../components/Header'
import '../styles/globals.css'
+import { serializeJsonLd } from '../lib/json-ld'
interface Props {
title: string
description?: string
+ jsonLd?: Record<string, unknown> | Record<string, unknown>[]
}
-const { title, description } = Astro.props
+const { title, description, jsonLd } = Astro.props
const faviconRoute = import.meta.env.DEV ? '/favicon/local' : '/favicon'
---
@@ -44,6 +46,7 @@ const faviconRoute = import.meta.env.DEV ? '/favicon/local' : '/favicon'
/>
<link rel="sitemap" href={`${import.meta.env.BASE_URL}/sitemap-index.xml`} />
<title>{title} | Supabase Knowledge Base</title>
+ {jsonLd && <script type="application/ld+json" is:inline set:html={serializeJsonLd(jsonLd)} />}
</head>
<body>
<Header client:load />
diff --git a/apps/kb/src/lib/json-ld.ts b/apps/kb/src/lib/json-ld.ts
new file mode 100644
index 00000000000..5d34bcce6e1
--- /dev/null
+++ b/apps/kb/src/lib/json-ld.ts
@@ -0,0 +1,63 @@
+// Canonical origin + base path from astro.config.mjs (`site` and `base`).
+const KB_ORIGIN = 'https://supabase.com'
+export const KB_BASE_URL = `${KB_ORIGIN}/kb`
+
+type JsonLdSchema = Record<string, unknown> | Record<string, unknown>[]
+
+export function serializeJsonLd(schema: JsonLdSchema): string {
+ return JSON.stringify(schema)
+ .replace(/</g, '\\u003c')
+ .replace(/>/g, '\\u003e')
+ .replace(/&/g, '\\u0026')
+}
+
+interface TechArticleSchemaInput {
+ url: string
+ headline: string
+ description?: string
+}
+
+// Guides carry no publish or modified dates in their frontmatter, so the
+// schema omits them rather than inventing values.
+export function techArticleSchema(input: TechArticleSchemaInput) {
+ return {
+ '@context': 'https://schema.org',
+ '@type': 'TechArticle',
+ '@id': `${input.url}#page`,
+ mainEntityOfPage: {
+ '@type': 'WebPage',
+ '@id': input.url,
+ },
+ url: input.url,
+ headline: input.headline,
+ description: input.description,
+ inLanguage: 'en',
+ publisher: {
+ '@type': 'Organization',
+ name: 'Supabase',
+ url: KB_ORIGIN,
+ logo: {
+ '@type': 'ImageObject',
+ url: `${KB_ORIGIN}/images/og/supabase-og.png`,
+ },
+ },
+ }
+}
+
+export interface BreadcrumbItem {
+ name: string
+ url: string
+}
+
+export function breadcrumbListSchema(items: BreadcrumbItem[]) {
+ return {
+ '@context': 'https://schema.org',
+ '@type': 'BreadcrumbList',
+ itemListElement: items.map((item, index) => ({
+ '@type': 'ListItem',
+ position: index + 1,
+ name: item.name,
+ item: item.url,
+ })),
+ }
+}
diff --git a/apps/kb/src/pages/guides/[...slug].astro b/apps/kb/src/pages/guides/[...slug].astro
index 1b1b208f64f..8b630013c1d 100644
--- a/apps/kb/src/pages/guides/[...slug].astro
+++ b/apps/kb/src/pages/guides/[...slug].astro
@@ -3,6 +3,7 @@ import { getCollection, render } from 'astro:content'
import { Admonition } from 'ui-patterns/Admonition'
import ArticleLayout from '../../layouts/ArticleLayout.astro'
+import { breadcrumbListSchema, KB_BASE_URL, techArticleSchema } from '../../lib/json-ld'
export async function getStaticPaths() {
const guides = await getCollection('guides')
@@ -18,8 +19,25 @@ const { Content } = await render(guide)
// guide.filePath is relative to the kb app root, e.g. "src/content/guides/foo.mdx"
const viewGitHubLink = `https://github.com/supabase/supabase/blob/master/apps/kb/${guide.filePath}`
+
+const url = `${KB_BASE_URL}/guides/${guide.id}`
+const jsonLd = [
+ techArticleSchema({ url, headline: title, description }),
+ breadcrumbListSchema([
+ { name: 'Knowledge Base', url: KB_BASE_URL },
+ { name: title, url },
+ ]),
+]
---
-<ArticleLayout title={title} description={description} topics={topics} github_url={github_url} viewGitHubLink={viewGitHubLink} date={date}>
+<ArticleLayout
+ title={title}
+ description={description}
+ topics={topics}
+ github_url={github_url}
+ viewGitHubLink={viewGitHubLink}
+ date={date}
+ jsonLd={jsonLd}
+>
<Content components={{ Admonition }} />
</ArticleLayout>
diff --git a/apps/kb/src/pages/topics/[topic].astro b/apps/kb/src/pages/topics/[topic].astro
index 7e29d2afdaf..ddb7a703156 100644
--- a/apps/kb/src/pages/topics/[topic].astro
+++ b/apps/kb/src/pages/topics/[topic].astro
@@ -2,6 +2,7 @@
import { getCollection } from 'astro:content'
import Layout from '../../layouts/Layout.astro'
+import { breadcrumbListSchema, KB_BASE_URL } from '../../lib/json-ld'
import { TOPICS, topicToSlug } from '../../lib/topics'
export async function getStaticPaths() {
@@ -25,9 +26,14 @@ export async function getStaticPaths() {
}
const { topic, description, guides } = Astro.props
+const url = `${KB_BASE_URL}/topics/${topicToSlug(topic)}`
+const jsonLd = breadcrumbListSchema([
+ { name: 'Knowledge Base', url: KB_BASE_URL },
+ { name: topic, url },
+])
---
-<Layout title={topic} description={description}>
+<Layout title={topic} description={description} jsonLd={jsonLd}>
<article class="prose max-w-3xl mx-auto px-6 py-16">
<h1>{topic}</h1>
<p class="lead">{description}</p>
diff --git a/apps/kb/src/pages/troubleshooting/[...slug].astro b/apps/kb/src/pages/troubleshooting/[...slug].astro
index f3e810e7fc3..574d90d245d 100644
--- a/apps/kb/src/pages/troubleshooting/[...slug].astro
+++ b/apps/kb/src/pages/troubleshooting/[...slug].astro
@@ -3,6 +3,7 @@ import { getCollection, render } from 'astro:content'
import { Admonition } from 'ui-patterns/Admonition'
import ArticleLayout from '../../layouts/ArticleLayout.astro'
+import { breadcrumbListSchema, KB_BASE_URL, techArticleSchema } from '../../lib/json-ld'
import { getGithubUrlsBySlug } from '../../lib/troubleshooting-db'
export async function getStaticPaths() {
@@ -22,8 +23,17 @@ export async function getStaticPaths() {
const { entry, githubUrl } = Astro.props
const { title, description, topics } = entry.data
const { Content } = await render(entry)
+
+const url = `${KB_BASE_URL}/troubleshooting/${entry.id}`
+const jsonLd = [
+ techArticleSchema({ url, headline: title, description }),
+ breadcrumbListSchema([
+ { name: 'Knowledge Base', url: KB_BASE_URL },
+ { name: title, url },
+ ]),
+]
---
-<ArticleLayout title={title} description={description} topics={topics} github_url={githubUrl}>
+<ArticleLayout title={title} description={description} topics={topics} github_url={githubUrl} jsonLd={jsonLd}>
<Content components={{ Admonition }} />
</ArticleLayout>