Skip to content

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

chore(kb) - add in canonical URLs (and refer to them in JSON+LD) (#52017)

Kody Jacksonb909d47efd4d
diff --git a/apps/kb/src/layouts/Layout.astro b/apps/kb/src/layouts/Layout.astro
index 0e72af6fd3b..afd496f169c 100644
--- a/apps/kb/src/layouts/Layout.astro
+++ b/apps/kb/src/layouts/Layout.astro
@@ -6,7 +6,7 @@ import { Header } from '../components/Header'
import { toSummary, toTroubleshootingSummary } from '../lib/guides'
import { getListedTopics } from '../lib/topics'
import '../styles/globals.css'
-import { serializeJsonLd } from '../lib/json-ld'
+import { canonicalUrl, serializeJsonLd } from '../lib/json-ld'
interface Props {
title?: string
@@ -21,6 +21,7 @@ const navTopics = getListedTopics({
troubleshooting: (await getCollection('troubleshooting')).map(toTroubleshootingSummary),
}).map((topic) => topic.name)
const faviconRoute = import.meta.env.DEV ? '/favicon/local' : '/favicon'
+const canonicalHref = canonicalUrl(Astro.url.pathname)
---
<!doctype html>
@@ -58,6 +59,7 @@ const faviconRoute = import.meta.env.DEV ? '/favicon/local' : '/favicon'
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Manrope:wght@400;500;600;700;800&display=swap"
/>
+ <link rel="canonical" href={canonicalHref} />
<link rel="sitemap" href={`${import.meta.env.BASE_URL}/sitemap-index.xml`} />
<title>{title ? `${title} | Supabase Knowledge Base` : 'Supabase Knowledge Base'}</title>
{jsonLd && <script type="application/ld+json" is:inline set:html={serializeJsonLd(jsonLd)} />}
diff --git a/apps/kb/src/lib/json-ld.ts b/apps/kb/src/lib/json-ld.ts
index 5d34bcce6e1..a70455d3cc1 100644
--- a/apps/kb/src/lib/json-ld.ts
+++ b/apps/kb/src/lib/json-ld.ts
@@ -2,6 +2,12 @@
const KB_ORIGIN = 'https://supabase.com'
export const KB_BASE_URL = `${KB_ORIGIN}/kb`
+// Canonical URL for a rendered page, from `Astro.url.pathname`. Always on the production origin, even on preview
+// deployments, and trailing slashes are dropped so the index and every other route share one form.
+export function canonicalUrl(pathname: string): string {
+ return new URL(pathname.replace(/\/+$/, '') || '/', KB_ORIGIN).href
+}
+
type JsonLdSchema = Record<string, unknown> | Record<string, unknown>[]
export function serializeJsonLd(schema: JsonLdSchema): string {
diff --git a/apps/kb/src/pages/guides/[...slug].astro b/apps/kb/src/pages/guides/[...slug].astro
index 902e13d253d..371f5d95439 100644
--- a/apps/kb/src/pages/guides/[...slug].astro
+++ b/apps/kb/src/pages/guides/[...slug].astro
@@ -4,7 +4,7 @@ import { Admonition } from 'ui-patterns/Admonition'
import ArticleLayout from '../../layouts/ArticleLayout.astro'
import { relatedGuides, toSummary } from '../../lib/guides'
-import { breadcrumbListSchema, KB_BASE_URL, techArticleSchema } from '../../lib/json-ld'
+import { breadcrumbListSchema, canonicalUrl, KB_BASE_URL, techArticleSchema } from '../../lib/json-ld'
export async function getStaticPaths() {
const guides = await getCollection('guides')
@@ -25,7 +25,7 @@ const toc = headings
// 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 url = canonicalUrl(Astro.url.pathname)
const jsonLd = [
techArticleSchema({ url, headline: title, description }),
breadcrumbListSchema([
diff --git a/apps/kb/src/pages/topics/[topic].astro b/apps/kb/src/pages/topics/[topic].astro
index 932e0935036..bdfb77c6440 100644
--- a/apps/kb/src/pages/topics/[topic].astro
+++ b/apps/kb/src/pages/topics/[topic].astro
@@ -6,7 +6,7 @@ import { GuideBrowser } from '../../components/GuideBrowser'
import { TopicHero } from '../../components/Hero'
import Layout from '../../layouts/Layout.astro'
import { toSummary, toTroubleshootingSummary } from '../../lib/guides'
-import { breadcrumbListSchema, KB_BASE_URL } from '../../lib/json-ld'
+import { breadcrumbListSchema, canonicalUrl, KB_BASE_URL } from '../../lib/json-ld'
import { isInTopic, TOPICS, topicToSlug } from '../../lib/topics'
export async function getStaticPaths() {
@@ -31,7 +31,7 @@ export async function getStaticPaths() {
}
const { topic, description, guides, troubleshooting } = Astro.props
-const url = `${KB_BASE_URL}/topics/${topicToSlug(topic)}`
+const url = canonicalUrl(Astro.url.pathname)
const jsonLd = breadcrumbListSchema([
{ name: 'Knowledge Base', url: KB_BASE_URL },
{ name: topic, url },
diff --git a/apps/kb/src/pages/troubleshooting/[...slug].astro b/apps/kb/src/pages/troubleshooting/[...slug].astro
index 574d90d245d..6ce4465317a 100644
--- a/apps/kb/src/pages/troubleshooting/[...slug].astro
+++ b/apps/kb/src/pages/troubleshooting/[...slug].astro
@@ -3,7 +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 { breadcrumbListSchema, canonicalUrl, KB_BASE_URL, techArticleSchema } from '../../lib/json-ld'
import { getGithubUrlsBySlug } from '../../lib/troubleshooting-db'
export async function getStaticPaths() {
@@ -24,7 +24,7 @@ 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 url = canonicalUrl(Astro.url.pathname)
const jsonLd = [
techArticleSchema({ url, headline: title, description }),
breadcrumbListSchema([