metadata.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import type { Metadata } from 'next';
  2. import type { BlogArticle, BlogLocale } from './types';
  3. import { absoluteUrl, articlePath, availableLocales, blogPath, indexingEnabled, translations } from './data';
  4. import { copy } from './translations';
  5. export function blogMetadata(locale: BlogLocale, article?: BlogArticle): Metadata {
  6. const title = article?.title ?? copy[locale].title;
  7. const description = article?.description ?? copy[locale].description;
  8. const path = article ? articlePath(article) : blogPath(locale);
  9. const variants = article
  10. ? translations(article).map(a => [a.locale, absoluteUrl(articlePath(a))])
  11. : availableLocales().map(l => [l, absoluteUrl(blogPath(l))]);
  12. const languages = Object.fromEntries(variants);
  13. if (languages.en) languages['x-default'] = languages.en;
  14. return {
  15. metadataBase: new URL(absoluteUrl('/')), title: `${title} | STRATA`, description,
  16. robots: { index: indexingEnabled && (!article || article.published), follow: true },
  17. alternates: { canonical: absoluteUrl(path), languages },
  18. openGraph: { title, description, url: absoluteUrl(path), siteName: 'STRATA', type: article ? 'article' : 'website', locale,
  19. alternateLocale: variants.map(([l]) => l).filter(l => l !== locale),
  20. ...(article ? { modifiedTime: article.reviewedAt } : {}) },
  21. twitter: { card: 'summary', title, description },
  22. };
  23. }
  24. export function articleSchema(article: BlogArticle) {
  25. const url = absoluteUrl(articlePath(article));
  26. return [
  27. { '@context': 'https://schema.org', '@type': 'Article', '@id': `${url}#article`, headline: article.title,
  28. description: article.description, inLanguage: article.locale, mainEntityOfPage: url,
  29. dateModified: article.reviewedAt, author: { '@type': 'Organization', name: 'STRATA', url: absoluteUrl('/') },
  30. publisher: { '@type': 'Organization', name: 'STRATA', url: absoluteUrl('/') },
  31. citation: article.sources.map(s => s.url) },
  32. { '@context': 'https://schema.org', '@type': 'BreadcrumbList', itemListElement: [
  33. { '@type': 'ListItem', position: 1, name: copy[article.locale].guides, item: absoluteUrl(blogPath(article.locale)) },
  34. { '@type': 'ListItem', position: 2, name: article.title, item: url },
  35. ] },
  36. ];
  37. }