belt 0.6.9 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,172 +0,0 @@
1
- /**
2
- * Post-build SEO/AEO step (runs as part of `npm run build`).
3
- *
4
- * Search and AI answer-engine crawlers mostly don't run JavaScript, so an SPA's
5
- * empty <div id="root"> tells them nothing. Using src/seo.js, this writes into dist/:
6
- *
7
- * index.html, <path>.html — each page in seo.pages rendered to HTML (via
8
- * dist-ssr/entry-server.js) with its own <title>,
9
- * description, canonical URL, Open Graph tags and JSON-LD
10
- * robots.txt — allows crawlers in indexable environments, `Disallow: /` elsewhere
11
- * sitemap.xml — every public page (needs a site URL)
12
- * llms.txt — plain-text site summary for LLMs (https://llmstxt.org)
13
- * .belt-pages.json — S3 key → file map; `belt deploy frontend` uploads each page
14
- * to its extensionless key (/about → about.html) as text/html
15
- *
16
- * Environment:
17
- * VITE_SITE_URL canonical origin (belt deploy injects the frontend URL)
18
- * BELT_ENV environment being built (belt deploy injects it)
19
- * SEO_INDEXABLE "true"/"false" to override seo.indexableEnvironments
20
- */
21
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
22
- import { dirname, join } from 'node:path'
23
- import { fileURLToPath, pathToFileURL } from 'node:url'
24
- import seo from '../src/seo.js'
25
-
26
- const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..')
27
- const DIST = join(ROOT, 'dist')
28
- const SSR_ENTRY = join(ROOT, 'dist-ssr', 'entry-server.js')
29
-
30
- const siteUrl = (process.env.VITE_SITE_URL || seo.siteUrl || '').replace(/\/+$/, '')
31
- const indexable = process.env.SEO_INDEXABLE
32
- ? process.env.SEO_INDEXABLE === 'true'
33
- : (seo.indexableEnvironments || ['prod']).includes(process.env.BELT_ENV)
34
-
35
- const normalize = (path) => `/${path.replace(/^\/+|\/+$/g, '')}`
36
- const privatePaths = (seo.privatePaths || []).map(normalize)
37
- const isPrivate = (path) => privatePaths.some((p) => path === p || path.startsWith(`${p}/`))
38
- const pages = (seo.pages || [])
39
- .map((page) => ({ ...page, path: normalize(page.path) }))
40
- .filter((page) => !isPrivate(page.path))
41
-
42
- const escapeHtml = (s) =>
43
- String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
44
- const absolute = (path) => `${siteUrl}${path === '/' ? '/' : path}`
45
-
46
- async function loadRenderer() {
47
- if (!existsSync(SSR_ENTRY)) {
48
- console.warn(`⚠️ ${SSR_ENTRY} not found — pages get SEO tags but no prerendered content`)
49
- return null
50
- }
51
- try {
52
- return (await import(pathToFileURL(SSR_ENTRY).href)).render
53
- } catch (error) {
54
- console.warn(`⚠️ Could not load the SSR bundle (${error.message}) — pages get SEO tags but no prerendered content`)
55
- return null
56
- }
57
- }
58
-
59
- function renderBody(render, path) {
60
- if (!render) return ''
61
- try {
62
- return render(path)
63
- } catch (error) {
64
- // Usually a component touching window/document/localStorage during render.
65
- // Move that into useEffect to get the content prerendered.
66
- console.warn(`⚠️ Prerender failed for ${path} (${error.message}) — serving SEO tags only`)
67
- return ''
68
- }
69
- }
70
-
71
- function jsonLd(page) {
72
- const data = page.jsonLd || {
73
- '@context': 'https://schema.org',
74
- '@type': page.path === '/' ? 'WebSite' : 'WebPage',
75
- name: page.title,
76
- description: page.description,
77
- ...(siteUrl ? { url: absolute(page.path) } : {})
78
- }
79
- return JSON.stringify(data).replace(/</g, '\\u003c')
80
- }
81
-
82
- function headTags(page) {
83
- const tags = []
84
- if (siteUrl) tags.push(`<link rel="canonical" href="${absolute(page.path)}" />`)
85
- if (!indexable) tags.push('<meta name="robots" content="noindex, nofollow" />')
86
- tags.push(
87
- `<meta property="og:type" content="website" />`,
88
- `<meta property="og:site_name" content="${escapeHtml(seo.siteName)}" />`,
89
- `<meta property="og:title" content="${escapeHtml(page.title)}" />`,
90
- `<meta property="og:description" content="${escapeHtml(page.description)}" />`
91
- )
92
- if (siteUrl) tags.push(`<meta property="og:url" content="${absolute(page.path)}" />`)
93
- if (page.image) tags.push(`<meta property="og:image" content="${escapeHtml(page.image)}" />`)
94
- tags.push(
95
- `<meta name="twitter:card" content="${page.image ? 'summary_large_image' : 'summary'}" />`,
96
- '<link rel="alternate" type="text/plain" href="/llms.txt" title="llms.txt" />',
97
- `<script type="application/ld+json">${jsonLd(page)}</script>`
98
- )
99
- return tags.map((tag) => ` ${tag}\n`).join('')
100
- }
101
-
102
- function pageHtml(template, page, body) {
103
- return template
104
- .replace(/<title>[\s\S]*?<\/title>/, () => `<title>${escapeHtml(page.title)}</title>`)
105
- .replace(/\s*<meta name="description"[^>]*>/, '')
106
- .replace(
107
- '</head>',
108
- () => ` <meta name="description" content="${escapeHtml(page.description)}" />\n${headTags(page)} </head>`
109
- )
110
- .replace(
111
- '<div id="root"></div>',
112
- // data-prerendered tells main.jsx to hydrate rather than render fresh
113
- () => (body ? `<div id="root" data-prerendered="${escapeHtml(page.path)}">${body}</div>` : '<div id="root"></div>')
114
- )
115
- }
116
-
117
- function robotsTxt() {
118
- if (!indexable) return 'User-agent: *\nDisallow: /\n'
119
- const lines = ['User-agent: *', 'Allow: /', ...privatePaths.map((p) => `Disallow: ${p}`)]
120
- if (siteUrl) lines.push('', `Sitemap: ${siteUrl}/sitemap.xml`)
121
- return `${lines.join('\n')}\n`
122
- }
123
-
124
- function sitemapXml() {
125
- const urls = pages.map((page) => ` <url><loc>${escapeHtml(absolute(page.path))}</loc></url>`).join('\n')
126
- return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>\n`
127
- }
128
-
129
- function llmsTxt() {
130
- const lines = [`# ${seo.siteName}`, '', `> ${seo.description}`, '', '## Pages', '']
131
- for (const page of pages) lines.push(`- [${page.title}](${absolute(page.path)}): ${page.description}`)
132
- return `${lines.join('\n')}\n`
133
- }
134
-
135
- async function main() {
136
- const indexFile = join(DIST, 'index.html')
137
- if (!existsSync(indexFile)) {
138
- console.error('dist/index.html not found — run `vite build` first')
139
- process.exit(1)
140
- }
141
- if (!pages.some((page) => page.path === '/')) pages.unshift({ path: '/', title: seo.siteName })
142
- for (const page of pages) {
143
- page.title ||= seo.siteName
144
- page.description ||= seo.description
145
- }
146
-
147
- const template = readFileSync(indexFile, 'utf-8')
148
- const render = await loadRenderer()
149
- const manifest = {}
150
-
151
- for (const page of pages) {
152
- const html = pageHtml(template, page, renderBody(render, page.path))
153
- const key = page.path.slice(1)
154
- const file = key ? `${key}.html` : 'index.html'
155
- mkdirSync(dirname(join(DIST, file)), { recursive: true })
156
- writeFileSync(join(DIST, file), html)
157
- if (key) manifest[key] = file
158
- }
159
-
160
- writeFileSync(join(DIST, '.belt-pages.json'), `${JSON.stringify(manifest, null, 2)}\n`)
161
- writeFileSync(join(DIST, 'robots.txt'), robotsTxt())
162
- writeFileSync(join(DIST, 'llms.txt'), llmsTxt())
163
- if (siteUrl) writeFileSync(join(DIST, 'sitemap.xml'), sitemapXml())
164
-
165
- if (!siteUrl) console.warn('⚠️ No site URL (VITE_SITE_URL or seo.siteUrl) — skipped canonical URLs and sitemap.xml')
166
- console.log(
167
- `SEO: prerendered ${pages.length} page(s), ${indexable ? 'indexable' : 'noindex'}` +
168
- `${process.env.BELT_ENV ? ` (${process.env.BELT_ENV})` : ''}`
169
- )
170
- }
171
-
172
- main()