|
| 1 | +// Prerenders every page as static HTML with per-page meta tags and structured data, |
| 2 | +// and writes sitemap.xml, robots.txt, llms.txt, llms-full.txt and a Markdown version of |
| 3 | +// each page. Runs after `vite build` (client) and `vite build --ssr src/entry-server.tsx`. |
| 4 | +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 5 | +import { dirname, join } from 'node:path' |
| 6 | +import { fileURLToPath, pathToFileURL } from 'node:url' |
| 7 | +import { mdxToMarkdown } from './mdx-to-md.mjs' |
| 8 | + |
| 9 | +const root = join(dirname(fileURLToPath(import.meta.url)), '..') |
| 10 | +const dist = join(root, 'dist') |
| 11 | +const SITE = 'https://learn.datacontract.com' |
| 12 | +const LANGS = ['en', 'de'] |
| 13 | +const PUBLISHED = '2026-10-04' |
| 14 | + |
| 15 | +const { render, chapters, parts, exerciseNumber, href, t } = await import(pathToFileURL(join(root, 'dist-ssr/entry-server.js')).href) |
| 16 | +const template = readFileSync(join(dist, 'index.html'), 'utf8') |
| 17 | +const videos = JSON.parse(readFileSync(join(root, 'public/videos/videos.json'), 'utf8')) |
| 18 | + |
| 19 | +const esc = (s) => String(s).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>') |
| 20 | +const url = (lang, slug) => SITE + href(lang, slug) |
| 21 | +const mdxPath = (lang, slug) => join(root, 'src/content', lang, `${slug}.mdx`) |
| 22 | +const stepsWithTitles = (lang, slug) => |
| 23 | + [...readFileSync(mdxPath(lang, slug), 'utf8').matchAll(/<Step id="([^"]+)" title="([^"]+)">/g)].map((m) => ({ id: m[1], title: m[2] })) |
| 24 | +const isoDuration = (seconds) => `PT${Math.floor(seconds / 60)}M${Math.round(seconds % 60)}S` |
| 25 | + |
| 26 | +const publisher = { '@type': 'Organization', name: 'Entropy Data', url: 'https://www.entropy-data.com', logo: `${SITE}/entropy-data-logo.svg` } |
| 27 | + |
| 28 | +function pageTitle(lang, chapter) { |
| 29 | + const ui = t(lang) |
| 30 | + const number = exerciseNumber(chapter.slug) |
| 31 | + if (chapter.slug === 'welcome') return `${ui.title}: ${ui.subtitle}` |
| 32 | + return `${number ? `${ui.exercise} ${number}: ` : ''}${chapter.title[lang]} · ${ui.title}` |
| 33 | +} |
| 34 | + |
| 35 | +function structuredData(lang, chapter) { |
| 36 | + const ui = t(lang) |
| 37 | + const part = parts.find((p) => p.id === chapter.part) |
| 38 | + const data = [{ |
| 39 | + '@context': 'https://schema.org', |
| 40 | + '@type': 'BreadcrumbList', |
| 41 | + itemListElement: [ |
| 42 | + { '@type': 'ListItem', position: 1, name: ui.title, item: url(lang, 'welcome') }, |
| 43 | + ...(chapter.slug === 'welcome' ? [] : [{ '@type': 'ListItem', position: 2, name: chapter.title[lang], item: url(lang, chapter.slug) }]), |
| 44 | + ], |
| 45 | + }] |
| 46 | + if (chapter.slug === 'welcome') { |
| 47 | + data.push({ |
| 48 | + '@context': 'https://schema.org', |
| 49 | + '@type': 'Course', |
| 50 | + name: ui.title, |
| 51 | + description: ui.subtitle, |
| 52 | + url: url(lang, 'welcome'), |
| 53 | + inLanguage: lang, |
| 54 | + isAccessibleForFree: true, |
| 55 | + educationalLevel: 'Beginner', |
| 56 | + teaches: ['Data contracts', 'Open Data Contract Standard (ODCS)', 'Open Data Product Standard (ODPS)', 'Data Contract CLI', 'Contract-first data products', 'Consumer-driven contracts', 'CI/CD for data contracts', 'Semantics and ontologies'], |
| 57 | + keywords: 'data contract, ODCS, ODPS, data product, data mesh, Data Contract CLI, data quality, CI/CD, GitHub Actions', |
| 58 | + provider: publisher, |
| 59 | + publisher, |
| 60 | + hasCourseInstance: { '@type': 'CourseInstance', courseMode: 'online', courseWorkload: 'PT6H' }, |
| 61 | + hasPart: chapters.filter((c) => c.exercise).map((c) => ({ '@type': 'LearningResource', name: c.title[lang], url: url(lang, c.slug) })), |
| 62 | + offers: { '@type': 'Offer', price: 0, priceCurrency: 'EUR', category: 'Free' }, |
| 63 | + }) |
| 64 | + } else { |
| 65 | + const steps = stepsWithTitles(lang, chapter.slug) |
| 66 | + if (steps.length) { |
| 67 | + data.push({ |
| 68 | + '@context': 'https://schema.org', |
| 69 | + '@type': 'HowTo', |
| 70 | + name: chapter.title[lang], |
| 71 | + description: chapter.summary[lang], |
| 72 | + inLanguage: lang, |
| 73 | + totalTime: `PT${chapter.minutes}M`, |
| 74 | + isPartOf: { '@type': 'Course', name: ui.title, url: url(lang, 'welcome') }, |
| 75 | + about: part?.title[lang], |
| 76 | + publisher, |
| 77 | + step: steps.map((s, i) => ({ '@type': 'HowToStep', position: i + 1, name: s.title, url: `${url(lang, chapter.slug)}#step-${s.id}` })), |
| 78 | + }) |
| 79 | + } |
| 80 | + } |
| 81 | + if (chapter.video && videos[chapter.slug]) { |
| 82 | + data.push({ |
| 83 | + '@context': 'https://schema.org', |
| 84 | + '@type': 'VideoObject', |
| 85 | + name: `${chapter.title[lang]} (walkthrough)`, |
| 86 | + description: chapter.summary[lang], |
| 87 | + thumbnailUrl: `${SITE}/videos/${chapter.slug}.webp`, |
| 88 | + contentUrl: `${SITE}/videos/${chapter.slug}.mp4`, |
| 89 | + uploadDate: PUBLISHED, |
| 90 | + duration: isoDuration(videos[chapter.slug].duration), |
| 91 | + inLanguage: 'en', |
| 92 | + publisher, |
| 93 | + }) |
| 94 | + } |
| 95 | + return data |
| 96 | +} |
| 97 | + |
| 98 | +function head(lang, chapter, canonical) { |
| 99 | + const title = pageTitle(lang, chapter) |
| 100 | + const description = chapter.slug === 'welcome' |
| 101 | + ? (lang === 'de' |
| 102 | + ? 'Kostenloses Hands-on-Tutorial: Datenkontrakte mit ODCS 3.2, Datenprodukte mit ODPS 1.1, Data Contract CLI, KI-Kontext und CI/CD mit GitHub Actions.' |
| 103 | + : 'A free, hands-on tutorial: data contracts with ODCS 3.2, data products with ODPS 1.1, the Data Contract CLI, AI context, and CI/CD with GitHub Actions.') |
| 104 | + : `${chapter.summary[lang]} ${t(lang).title}: ${t(lang).subtitle}.` |
| 105 | + return [ |
| 106 | + `<title>${esc(title)}</title>`, |
| 107 | + `<meta name="description" content="${esc(description)}" />`, |
| 108 | + `<link rel="canonical" href="${canonical}" />`, |
| 109 | + ...LANGS.map((l) => `<link rel="alternate" hreflang="${l}" href="${url(l, chapter.slug)}" />`), |
| 110 | + `<link rel="alternate" hreflang="x-default" href="${url('en', chapter.slug)}" />`, |
| 111 | + `<link rel="alternate" type="text/markdown" href="${SITE}/${lang}/${chapter.slug}.md" />`, |
| 112 | + `<meta property="og:url" content="${canonical}" />`, |
| 113 | + `<meta property="og:title" content="${esc(title)}" />`, |
| 114 | + `<meta property="og:description" content="${esc(description)}" />`, |
| 115 | + `<meta property="og:locale" content="${lang === 'de' ? 'de_DE' : 'en_US'}" />`, |
| 116 | + `<meta property="og:image:alt" content="Data Contracts in Practice: a free, hands-on tutorial at learn.datacontract.com" />`, |
| 117 | + `<meta name="twitter:title" content="${esc(title)}" />`, |
| 118 | + `<meta name="twitter:description" content="${esc(description)}" />`, |
| 119 | + ...structuredData(lang, chapter).map((d) => `<script type="application/ld+json">${JSON.stringify(d).replace(/</g, '\\u003c')}</script>`), |
| 120 | + ].join('\n ') |
| 121 | +} |
| 122 | + |
| 123 | +async function page(lang, chapter, file, canonical = url(lang, chapter.slug)) { |
| 124 | + const body = await render({ lang, slug: chapter.slug }) |
| 125 | + const html = template |
| 126 | + .replace('<html lang="en">', `<html lang="${lang}">`) |
| 127 | + .replace('<!--head-->', head(lang, chapter, canonical)) |
| 128 | + .replace('<div id="root"></div>', `<div id="root">${body}</div>`) |
| 129 | + mkdirSync(dirname(file), { recursive: true }) |
| 130 | + writeFileSync(file, html) |
| 131 | +} |
| 132 | + |
| 133 | +const welcome = chapters.find((c) => c.slug === 'welcome') |
| 134 | +for (const lang of LANGS) { |
| 135 | + for (const chapter of chapters) { |
| 136 | + const dir = chapter.slug === 'welcome' ? join(dist, lang) : join(dist, lang, chapter.slug) |
| 137 | + await page(lang, chapter, join(dir, 'index.html')) |
| 138 | + writeFileSync(join(dist, lang, `${chapter.slug}.md`), |
| 139 | + `# ${chapter.title[lang]}\n\n${chapter.summary[lang]}\n\nSource: ${url(lang, chapter.slug)}\n\n` + mdxToMarkdown(readFileSync(mdxPath(lang, chapter.slug), 'utf8'), mdxPath(lang, chapter.slug))) |
| 140 | + } |
| 141 | +} |
| 142 | +// the root page: the English welcome page for crawlers (visitors are redirected to their language) |
| 143 | +await page('en', welcome, join(dist, 'index.html'), url('en', 'welcome')) |
| 144 | +await page('en', { ...welcome, title: { en: 'Page not found', de: 'Seite nicht gefunden' } }, join(dist, '404.html'), url('en', 'welcome')) |
| 145 | + |
| 146 | +// sitemap with language alternates |
| 147 | +const urls = LANGS.flatMap((lang) => chapters.map((c) => ({ lang, slug: c.slug }))) |
| 148 | +writeFileSync(join(dist, 'sitemap.xml'), `<?xml version="1.0" encoding="UTF-8"?> |
| 149 | +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> |
| 150 | +${urls.map(({ lang, slug }) => ` <url> |
| 151 | + <loc>${url(lang, slug)}</loc> |
| 152 | + <lastmod>${PUBLISHED}</lastmod> |
| 153 | +${LANGS.map((l) => ` <xhtml:link rel="alternate" hreflang="${l}" href="${url(l, slug)}" />`).join('\n')} |
| 154 | + <xhtml:link rel="alternate" hreflang="x-default" href="${url('en', slug)}" /> |
| 155 | + </url>`).join('\n')} |
| 156 | +</urlset> |
| 157 | +`) |
| 158 | + |
| 159 | +// search engines and AI crawlers are welcome |
| 160 | +writeFileSync(join(dist, 'robots.txt'), `User-agent: * |
| 161 | +Allow: / |
| 162 | +
|
| 163 | +# AI crawlers and assistants are explicitly welcome |
| 164 | +${['GPTBot', 'OAI-SearchBot', 'ChatGPT-User', 'ClaudeBot', 'Claude-User', 'Claude-SearchBot', 'PerplexityBot', 'Perplexity-User', 'Google-Extended', 'Applebot-Extended', 'CCBot', 'Meta-ExternalAgent'].map((b) => `User-agent: ${b}\nAllow: /`).join('\n\n')} |
| 165 | +
|
| 166 | +Sitemap: ${SITE}/sitemap.xml |
| 167 | +`) |
| 168 | + |
| 169 | +// llms.txt: an index for AI systems, llms-full.txt: the whole English tutorial as Markdown |
| 170 | +const en = t('en') |
| 171 | +const llms = [`# ${en.title}`, '', |
| 172 | + `> A free, hands-on tutorial on data contracts: put a PostgreSQL dataset under contract with the Open Data Contract Standard (ODCS 3.2), describe data products with the Open Data Product Standard (ODPS 1.1), design contract-first, use consumer-driven contracts, automate everything with GitHub Actions, and publish to a data product platform. Uses the open-source Data Contract CLI and Data Product CLI. Available in English and German. Maintained by Entropy Data.`, '', |
| 173 | + ...parts.flatMap((p) => [`## ${p.title.en}`, '', ...chapters.filter((c) => c.part === p.id).map((c) => `- [${c.title.en}](${SITE}/en/${c.slug}.md): ${c.summary.en}`), '']), |
| 174 | + '## German version', '', ...chapters.map((c) => `- [${c.title.de}](${SITE}/de/${c.slug}.md): ${c.summary.de}`), '', |
| 175 | + '## Optional', '', `- [Full tutorial in one file](${SITE}/llms-full.txt)`, `- [Repository with exercises and reference solutions](https://github.com/datacontract/learn.datacontract.com)`, |
| 176 | + '- [Open Data Contract Standard](https://bitol-io.github.io/open-data-contract-standard/)', '- [Open Data Product Standard](https://bitol-io.github.io/open-data-product-standard/)', '- [Data Contract CLI](https://cli.datacontract.com)', ''] |
| 177 | +writeFileSync(join(dist, 'llms.txt'), llms.join('\n')) |
| 178 | +writeFileSync(join(dist, 'llms-full.txt'), [`# ${en.title}`, '', ...chapters.map((c) => readFileSync(join(dist, 'en', `${c.slug}.md`), 'utf8'))].join('\n\n---\n\n')) |
| 179 | + |
| 180 | +console.log(`prerendered ${urls.length} pages, sitemap.xml, robots.txt, llms.txt, llms-full.txt`) |
0 commit comments