Skip to content

Commit 3d34312

Browse files
committed
include integration page
1 parent 1cc2efa commit 3d34312

3 files changed

Lines changed: 412 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ Thanks go to these wonderful people! [[Become a contributor](CONTRIBUTING.md)].
2828

2929
<br />
3030
<br />
31-

‎scripts/check-seo.mjs‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Audits the static HTML produced by `next build` for baseline SEO metadata
3+
* and broken internal links. Run through `pnpm seo:check` so the build exists.
4+
*/
5+
import { existsSync, readFileSync, readdirSync } from "node:fs"
6+
import { dirname, join, relative } from "node:path"
7+
import { fileURLToPath } from "node:url"
8+
9+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..")
10+
const PAGES_DIR = join(ROOT, ".next/server/pages")
11+
const PUBLIC_DIR = join(ROOT, "public")
12+
const ERROR_ROUTES = new Set(["/404", "/500"])
13+
const failures = []
14+
15+
function getHtmlFiles(directory, files = []) {
16+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
17+
const file = join(directory, entry.name)
18+
if (entry.isDirectory()) getHtmlFiles(file, files)
19+
else if (entry.name.endsWith(".html")) files.push(file)
20+
}
21+
return files
22+
}
23+
24+
function routeFromFile(file) {
25+
const route = `/${relative(PAGES_DIR, file).replace(/\.html$/, "")}`
26+
return route === "/index" ? "/" : route.replace(/\/index$/, "")
27+
}
28+
29+
function hasPublicTarget(pathname) {
30+
return existsSync(join(PUBLIC_DIR, pathname))
31+
}
32+
33+
function report(route, message) {
34+
failures.push(`${route}: ${message}`)
35+
}
36+
37+
if (!existsSync(PAGES_DIR)) {
38+
console.error("No production build found. Run `pnpm build` first.")
39+
process.exit(1)
40+
}
41+
42+
const pages = new Map(
43+
getHtmlFiles(PAGES_DIR).map((file) => [
44+
routeFromFile(file),
45+
readFileSync(file, "utf8"),
46+
])
47+
)
48+
49+
for (const [route, html] of pages) {
50+
if (!ERROR_ROUTES.has(route)) {
51+
const title = html.match(/<title[^>]*>(.*?)<\/title>/)?.[1] ?? ""
52+
const description =
53+
html.match(/<meta name="description" content="([^"]*)"/)?.[1] ?? ""
54+
const canonicalCount = (html.match(/rel="canonical"/g) ?? []).length
55+
const h1Count = (html.match(/<h1[ >]/g) ?? []).length
56+
57+
if (!title) report(route, "missing title")
58+
else if (title.length > 65)
59+
report(route, `title is ${title.length} characters (maximum 65)`)
60+
61+
if (!description) report(route, "missing meta description")
62+
else if (description.length > 165)
63+
report(
64+
route,
65+
`meta description is ${description.length} characters (maximum 165)`
66+
)
67+
68+
if (canonicalCount !== 1)
69+
report(route, `expected one canonical URL, found ${canonicalCount}`)
70+
if (h1Count !== 1) report(route, `expected one H1, found ${h1Count}`)
71+
72+
for (const match of html.matchAll(
73+
/<script type="application\/ld\+json"[^>]*>(.*?)<\/script>/g
74+
)) {
75+
try {
76+
JSON.parse(match[1])
77+
} catch {
78+
report(route, "contains invalid JSON-LD")
79+
}
80+
}
81+
}
82+
83+
for (const match of html.matchAll(/href="((?:\/|#)[^"]*)"/g)) {
84+
const href = match[1].replaceAll("&amp;", "&")
85+
if (href.startsWith("/_next") || href.split(/[?#]/)[0].endsWith(".md")) {
86+
continue
87+
}
88+
89+
const url = new URL(href, `https://local.test${route}`)
90+
const targetHtml = pages.get(url.pathname)
91+
92+
if (!targetHtml && !hasPublicTarget(url.pathname)) {
93+
report(route, `broken internal link to ${url.pathname}`)
94+
continue
95+
}
96+
97+
if (url.hash && targetHtml && !ERROR_ROUTES.has(url.pathname)) {
98+
const id = decodeURIComponent(url.hash.slice(1))
99+
if (!targetHtml.includes(`id="${id}"`)) {
100+
report(route, `broken anchor link to ${url.pathname}${url.hash}`)
101+
}
102+
}
103+
}
104+
}
105+
106+
if (failures.length) {
107+
console.error(`SEO check failed with ${failures.length} problem(s):`)
108+
for (const failure of [...new Set(failures)]) console.error(`- ${failure}`)
109+
process.exit(1)
110+
}
111+
112+
console.log(`SEO check passed for ${pages.size} static HTML pages.`)

0 commit comments

Comments
 (0)