|
| 1 | +--- |
| 2 | +/** |
| 3 | + * Emits schema.org JSON-LD (as a single @graph) for AI/GEO discoverability and |
| 4 | + * rich results. Every page carries the identity nodes (Organization, WebSite, |
| 5 | + * SoftwareApplication) so a crawler that fetches a single page gets a complete, |
| 6 | + * self-resolving entity graph; content (non-home) pages additionally carry a |
| 7 | + * TechArticle and a BreadcrumbList. |
| 8 | + * |
| 9 | + * Deliberately omitted (do not add without a real data source): |
| 10 | + * - dateModified: no date exists in frontmatter — fabricating one is worse than none. |
| 11 | + * - SearchAction: docs search is a client-side Algolia modal, not a query-string |
| 12 | + * URL, so there is no honest urlTemplate to advertise. |
| 13 | + */ |
| 14 | +import { normalizeLangTag } from '../i18n/bcp-normalize'; |
| 15 | +import languages from '../i18n/languages'; |
| 16 | +import { useTranslations } from '../i18n/util'; |
| 17 | +import { getLanguageFromURL } from '../util'; |
| 18 | +import { getBreadcrumbTrail } from '../util/getBreadcrumb'; |
| 19 | +import { getOgImageUrl } from '../util/getOgImageUrl'; |
| 20 | +
|
| 21 | +export interface Props { |
| 22 | + content: any; |
| 23 | + canonicalURL: URL; |
| 24 | +} |
| 25 | +
|
| 26 | +const { content = {}, canonicalURL } = Astro.props; |
| 27 | +const t = useTranslations(Astro); |
| 28 | +const site = Astro.site!; // configured as https://docs.openreplay.com/ |
| 29 | +const abs = (path: string) => new URL(path, site).href; |
| 30 | +
|
| 31 | +const lang = getLanguageFromURL(canonicalURL.pathname || '/'); |
| 32 | +const bcpLang = normalizeLangTag(lang); |
| 33 | +
|
| 34 | +// Stable global identifiers (resolved against the OpenReplay product site). |
| 35 | +const ORG_ID = 'https://openreplay.com/#organization'; |
| 36 | +const WEBSITE_ID = `${site.origin}/#website`; |
| 37 | +const SOFTWARE_ID = 'https://openreplay.com/#software'; |
| 38 | +
|
| 39 | +// Is this the (localized) docs homepage? Strip lang + optional version + trailing slash. |
| 40 | +const pageSlug = canonicalURL.pathname |
| 41 | + .replace(/^\/[a-z-]{2,5}(\/v\d+\.\d+\.\d+)?\//, '') |
| 42 | + .replace(/\/+$/, ''); |
| 43 | +const isHome = pageSlug === '' || pageSlug === 'home'; |
| 44 | +
|
| 45 | +const organization = { |
| 46 | + '@type': 'Organization', |
| 47 | + '@id': ORG_ID, |
| 48 | + name: 'OpenReplay', |
| 49 | + url: 'https://openreplay.com', |
| 50 | + logo: { |
| 51 | + '@type': 'ImageObject', |
| 52 | + url: abs('/android-chrome-512x512.png'), |
| 53 | + width: 512, |
| 54 | + height: 512, |
| 55 | + caption: 'OpenReplay', |
| 56 | + }, |
| 57 | + description: |
| 58 | + 'OpenReplay is an open-source session replay suite for developers — session replay, co-browsing, and product analytics, self-hosted or cloud.', |
| 59 | + sameAs: [ |
| 60 | + 'https://github.com/openreplay', |
| 61 | + 'https://www.linkedin.com/company/openreplay', |
| 62 | + 'https://x.com/OpenReplayHQ', |
| 63 | + 'https://www.youtube.com/@openreplay', |
| 64 | + ], |
| 65 | +}; |
| 66 | +
|
| 67 | +const website = { |
| 68 | + '@type': 'WebSite', |
| 69 | + '@id': WEBSITE_ID, |
| 70 | + name: t('site.title'), |
| 71 | + url: abs(`/${lang}/home/`), |
| 72 | + publisher: { '@id': ORG_ID }, |
| 73 | + inLanguage: Object.keys(languages).map((l) => normalizeLangTag(l)), |
| 74 | +}; |
| 75 | +
|
| 76 | +const software = { |
| 77 | + '@type': 'SoftwareApplication', |
| 78 | + '@id': SOFTWARE_ID, |
| 79 | + name: 'OpenReplay', |
| 80 | + applicationCategory: 'DeveloperApplication', |
| 81 | + operatingSystem: 'Web, Linux', |
| 82 | + description: |
| 83 | + 'Open-source session replay suite: session replay, co-browsing, and product analytics. Self-hosted or cloud.', |
| 84 | + url: 'https://openreplay.com', |
| 85 | + downloadUrl: 'https://github.com/openreplay/openreplay', |
| 86 | + softwareHelp: abs(`/${lang}/home/`), |
| 87 | + license: 'https://github.com/openreplay/openreplay/blob/main/LICENSE', |
| 88 | + // Free to self-host and on the free cloud tier. `offers` is intentionally |
| 89 | + // omitted: a SoftwareApplication Offer is only rich-result-eligible alongside |
| 90 | + // an aggregateRating, and we have no honest rating source to cite. |
| 91 | + isAccessibleForFree: true, |
| 92 | + publisher: { '@id': ORG_ID }, |
| 93 | +}; |
| 94 | +
|
| 95 | +const graph: Record<string, unknown>[] = [organization, website, software]; |
| 96 | +
|
| 97 | +if (!isHome) { |
| 98 | + const title = content.title ?? t('site.title'); |
| 99 | + const description = content.description || content.metaDescription || t('site.description'); |
| 100 | +
|
| 101 | + const ogImageUrl = getOgImageUrl(canonicalURL.pathname, !!Astro.params.fallback); |
| 102 | + const imageSrc = content?.image?.src ?? ogImageUrl ?? t('site.og.imageSrc'); |
| 103 | +
|
| 104 | + // Build the breadcrumb trail; guarantee the current page is the final crumb |
| 105 | + // even when it isn't present in the nav tree. |
| 106 | + const trail = getBreadcrumbTrail(canonicalURL.pathname); |
| 107 | + const stripSlash = (s: string) => s.replace(/\/+$/, ''); |
| 108 | + if (stripSlash(trail[trail.length - 1]?.path ?? '') !== stripSlash(canonicalURL.pathname)) { |
| 109 | + trail.push({ name: title, path: canonicalURL.pathname }); |
| 110 | + } |
| 111 | + // Top-level docs section (the crumb right after "Docs"), when the page isn't |
| 112 | + // itself a top-level page. Free to derive from the trail. |
| 113 | + const articleSection = trail.length > 2 ? trail[1].name : undefined; |
| 114 | +
|
| 115 | + graph.push({ |
| 116 | + '@type': 'TechArticle', |
| 117 | + '@id': `${canonicalURL.href}#article`, |
| 118 | + headline: title, |
| 119 | + name: title, |
| 120 | + description, |
| 121 | + url: canonicalURL.href, |
| 122 | + mainEntityOfPage: canonicalURL.href, |
| 123 | + inLanguage: bcpLang, |
| 124 | + ...(articleSection ? { articleSection } : {}), |
| 125 | + image: new URL(imageSrc, site).href, |
| 126 | + isPartOf: { '@id': WEBSITE_ID }, |
| 127 | + author: { '@id': ORG_ID }, |
| 128 | + publisher: { '@id': ORG_ID }, |
| 129 | + about: { '@id': SOFTWARE_ID }, |
| 130 | + breadcrumb: { '@id': `${canonicalURL.href}#breadcrumb` }, |
| 131 | + }); |
| 132 | +
|
| 133 | + graph.push({ |
| 134 | + '@type': 'BreadcrumbList', |
| 135 | + '@id': `${canonicalURL.href}#breadcrumb`, |
| 136 | + itemListElement: trail.map((c, i) => ({ |
| 137 | + '@type': 'ListItem', |
| 138 | + position: i + 1, |
| 139 | + name: c.name, |
| 140 | + item: abs(c.path), |
| 141 | + })), |
| 142 | + }); |
| 143 | +} |
| 144 | +
|
| 145 | +const jsonLd = { '@context': 'https://schema.org', '@graph': graph }; |
| 146 | +// Escape "<" so a value containing "</script>" can't break out of the tag. |
| 147 | +const serialized = JSON.stringify(jsonLd).replace(/</g, '\\u003c'); |
| 148 | +--- |
| 149 | + |
| 150 | +<script type="application/ld+json" is:inline set:html={serialized} /> |
0 commit comments