Skip to content

Commit 58bfc78

Browse files
committed
Announce the pages to crawlers
The build writes sitemap.xml and robots.txt from the content files, so a new case study or post lists itself. Each page names its extensionless URL as canonical, and carries Person or Article structured data built from what the page already states.
1 parent f266d45 commit 58bfc78

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎src/components/Seo.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
import { useLocation } from 'react-router-dom'
12
import { Head } from 'vite-react-ssg'
23
import { site } from '../site'
34

4-
export default function Seo({ title, description }: { title: string; description: string }) {
5+
type Props = { title: string; description: string; schema?: object }
6+
7+
export default function Seo({ title, description, schema }: Props) {
8+
const { pathname } = useLocation()
9+
const canonical = site.origin + (pathname === '/' ? '/' : pathname.replace(/\.html$|\/$/, ''))
10+
511
return (
612
<Head>
713
<title>{title}</title>
814
<meta name="description" content={description} />
15+
<link rel="canonical" href={canonical} />
16+
<meta property="og:type" content="website" />
17+
<meta property="og:url" content={canonical} />
918
<meta property="og:title" content={title} />
1019
<meta property="og:description" content={description} />
1120
<meta property="og:image" content={site.avatar} />
1221
<meta name="twitter:card" content="summary" />
22+
{schema && <script type="application/ld+json">{JSON.stringify(schema)}</script>}
1323
</Head>
1424
)
1525
}

‎src/pages/Article.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ export default function Article({ entry, series }: Props) {
1515

1616
return (
1717
<article className="section-white py-5">
18-
<Seo title={`${meta.title} — ${site.name}`} description={meta.description} />
18+
<Seo
19+
title={`${meta.title} — ${site.name}`}
20+
description={meta.description}
21+
schema={{
22+
'@context': 'https://schema.org',
23+
'@type': 'Article',
24+
headline: meta.title,
25+
description: meta.description,
26+
url: `${site.origin}/${entry.kind}/${entry.slug}`,
27+
author: { '@type': 'Person', name: site.name, url: site.origin },
28+
publisher: { '@type': 'Person', name: site.name, url: site.origin },
29+
...(meta.date && { datePublished: meta.date }),
30+
}}
31+
/>
1932
<div aria-hidden className="read-progress" />
2033
<div className="container pt-md-4" style={{ maxWidth: 768 }}>
2134
<a href={isBlog ? '/blog' : '/#case-studies'} className="d-inline-flex gap-1 small text-body-secondary mb-4">

‎src/pages/Home.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@ import { site } from '../site'
99
export default function Home() {
1010
return (
1111
<>
12-
<Seo title={`${site.name} — ${site.role}`} description={site.description} />
12+
<Seo
13+
title={`${site.name} — ${site.role}`}
14+
description={site.description}
15+
schema={{
16+
'@context': 'https://schema.org',
17+
'@type': 'Person',
18+
name: site.name,
19+
jobTitle: site.role,
20+
description: site.description,
21+
url: site.origin,
22+
image: site.avatar,
23+
email: `mailto:${site.email}`,
24+
sameAs: [site.linkedin, site.github],
25+
}}
26+
/>
1327
<Hero />
1428
<CaseStudies />
1529
<Testimonials />

‎src/site.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const site = {
2+
origin: 'https://intellycode.dev',
23
name: 'Denis Zenios',
34
brand: 'IntellyCode',
45
role: 'Computational Engineer',

‎vite.config.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
77
import remarkMath from 'remark-math'
88
import rehypeKatex from 'rehype-katex'
99
import { toString } from 'mdast-util-to-string'
10+
import { readdirSync, writeFileSync } from 'node:fs'
11+
12+
const ORIGIN = 'https://intellycode.dev'
13+
14+
function slugsIn(kind: string) {
15+
return readdirSync(`src/content/${kind}`)
16+
.filter((file) => file.endsWith('.mdx'))
17+
.map((file) => `/${kind}/${file.replace('.mdx', '')}`)
18+
}
19+
20+
function writeSitemap(dir: string) {
21+
const paths = ['/', '/blog', ...slugsIn('case-studies'), ...slugsIn('blog')]
22+
const urls = paths.map((path) => ` <url><loc>${ORIGIN}${path}</loc></url>`).join('\n')
23+
24+
writeFileSync(
25+
`${dir}/sitemap.xml`,
26+
`<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>\n`,
27+
)
28+
writeFileSync(dir + '/robots.txt', `User-agent: *\nAllow: /\n\nSitemap: ${ORIGIN}/sitemap.xml\n`)
29+
}
1030

1131
function remarkReadingMinutes() {
1232
return (tree: any) => {
@@ -36,5 +56,6 @@ export default defineConfig({
3656
},
3757
ssgOptions: {
3858
dirStyle: 'flat',
59+
onFinished: writeSitemap,
3960
},
4061
})

0 commit comments

Comments
 (0)