@luhtech/site-template 0.14.8 → 0.15.1
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.
- package/README.md +36 -0
- package/bin/luhtech-seo-check.mjs +92 -0
- package/bin/luhtech-sync-icons.mjs +140 -0
- package/package.json +5 -2
- package/src/layouts/SiteLayout.astro +21 -1
- package/src/lib/seoHead.ts +78 -0
package/README.md
CHANGED
|
@@ -336,6 +336,42 @@ A `tsconfig.json` was added (this repo had none) extending
|
|
|
336
336
|
diagnostics across `ToolCalculator.astro` and the diagram components that
|
|
337
337
|
the repo's prior no-tsconfig default resolution wasn't catching.
|
|
338
338
|
|
|
339
|
+
## Browser icons, canonical URLs and search metadata (0.15.0)
|
|
340
|
+
|
|
341
|
+
`SiteLayout` now emits, beyond `page.seo`'s title/description/ogImage:
|
|
342
|
+
|
|
343
|
+
- `<link rel="canonical">` and `og:url`, from the site's `astro.config`
|
|
344
|
+
`site` plus the page path (nothing is emitted if `site` is unset);
|
|
345
|
+
- `og:locale` (`en_US`), and -- when the page passes `brand={brand}` --
|
|
346
|
+
`og:site_name` (`brand.identity.name`) and `theme-color`
|
|
347
|
+
(`brand.color.foundation.ink.hex`, the header colour);
|
|
348
|
+
- icon and manifest links for whichever of `favicon.svg`,
|
|
349
|
+
`icon-192.png`, `apple-touch-icon.png` and `site.webmanifest` exist in
|
|
350
|
+
the site's `public/`. A file that is not there gets no link, so nothing
|
|
351
|
+
dangles before a site has generated its icons.
|
|
352
|
+
|
|
353
|
+
`brand` is optional, so existing pages keep building unchanged; pass it
|
|
354
|
+
wherever the page already calls `loadBrand()`:
|
|
355
|
+
|
|
356
|
+
```astro
|
|
357
|
+
<SiteLayout site={site} page={page} brand={brand}>
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Two bins go with it:
|
|
361
|
+
|
|
362
|
+
- **`luhtech-sync-icons <brand.json> [--public <dir>] [--check]`** renders
|
|
363
|
+
`public/favicon.svg` to `apple-touch-icon.png` (180px, on the brand
|
|
364
|
+
canvas colour), `icon-192.png`, `icon-512.png` and `favicon.ico`
|
|
365
|
+
(16/32/48px -- never linked, but browsers and crawlers request it on
|
|
366
|
+
their own; 0.15.1), and writes `site.webmanifest` from the brand's name
|
|
367
|
+
and colours. Commit the output;
|
|
368
|
+
`--check` exits 1 if any of it is missing or stale.
|
|
369
|
+
- **`luhtech-seo-check [dist]`** checks every built page after
|
|
370
|
+
`astro build`. Errors (exit 1): no title, description, canonical, icon
|
|
371
|
+
link, `og:image` or `<html lang>`. Warnings: title over 60 characters,
|
|
372
|
+
description outside 70-160, anything but one `<h1>` -- those are content
|
|
373
|
+
edits, reported rather than blocking.
|
|
374
|
+
|
|
339
375
|
## Migrating a venture site
|
|
340
376
|
|
|
341
377
|
A migration is: author `content/site.<venture>.json` + `content/pages/*.json`
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Checks every built HTML page under a site's dist/ for the browser and
|
|
4
|
+
* search metadata SiteLayout is expected to emit. Run after `astro build`.
|
|
5
|
+
*
|
|
6
|
+
* Errors (exit 1): missing <title>, meta description, canonical link,
|
|
7
|
+
* icon link, og:image, or <html lang>.
|
|
8
|
+
* Warnings (exit 0): title over 60 characters, description outside
|
|
9
|
+
* 70-160 characters, anything other than exactly one <h1>. These are
|
|
10
|
+
* content edits (page.seo / brand identity), reported so they are seen,
|
|
11
|
+
* not so they block a build.
|
|
12
|
+
*
|
|
13
|
+
* Usage: luhtech-seo-check [dist-dir] (default: dist)
|
|
14
|
+
*/
|
|
15
|
+
import { readFileSync, readdirSync, statSync, realpathSync } from 'node:fs';
|
|
16
|
+
import { join, relative, resolve } from 'node:path';
|
|
17
|
+
import { fileURLToPath } from 'node:url';
|
|
18
|
+
|
|
19
|
+
export const TITLE_MAX = 60;
|
|
20
|
+
export const DESCRIPTION_MIN = 70;
|
|
21
|
+
export const DESCRIPTION_MAX = 160;
|
|
22
|
+
|
|
23
|
+
const decode = (s) =>
|
|
24
|
+
s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'");
|
|
25
|
+
|
|
26
|
+
function metaContent(html, attr, value) {
|
|
27
|
+
for (const tag of html.match(/<meta\b[^>]*>/gi) ?? []) {
|
|
28
|
+
if (new RegExp(`${attr}=["']${value}["']`, 'i').test(tag)) {
|
|
29
|
+
const m = tag.match(/content=["']([^"']*)["']/i);
|
|
30
|
+
return m ? decode(m[1]) : '';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hasLinkRel(html, rel) {
|
|
37
|
+
return (html.match(/<link\b[^>]*>/gi) ?? []).some((tag) =>
|
|
38
|
+
new RegExp(`rel=["'][^"']*\\b${rel}\\b[^"']*["']`, 'i').test(tag)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function checkHtml(html) {
|
|
43
|
+
const errors = [];
|
|
44
|
+
const warnings = [];
|
|
45
|
+
const title = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1]?.trim();
|
|
46
|
+
const description = metaContent(html, 'name', 'description');
|
|
47
|
+
|
|
48
|
+
if (!title) errors.push('missing <title>');
|
|
49
|
+
else if (decode(title).length > TITLE_MAX) warnings.push(`title is ${decode(title).length} characters (over ${TITLE_MAX})`);
|
|
50
|
+
if (!description) errors.push('missing meta description');
|
|
51
|
+
else if (description.length < DESCRIPTION_MIN || description.length > DESCRIPTION_MAX)
|
|
52
|
+
warnings.push(`description is ${description.length} characters (aim ${DESCRIPTION_MIN}-${DESCRIPTION_MAX})`);
|
|
53
|
+
if (!hasLinkRel(html, 'canonical')) errors.push('missing canonical link');
|
|
54
|
+
if (!hasLinkRel(html, 'icon')) errors.push('missing icon link');
|
|
55
|
+
if (metaContent(html, 'property', 'og:image') === null) errors.push('missing og:image');
|
|
56
|
+
if (!/<html\b[^>]*\blang=["'][^"']+["']/i.test(html)) errors.push('missing <html lang>');
|
|
57
|
+
const h1 = (html.match(/<h1\b/gi) ?? []).length;
|
|
58
|
+
if (h1 !== 1) warnings.push(`${h1} <h1> headings (expected 1)`);
|
|
59
|
+
return { errors, warnings };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function htmlFiles(dir) {
|
|
63
|
+
const out = [];
|
|
64
|
+
for (const entry of readdirSync(dir)) {
|
|
65
|
+
const full = join(dir, entry);
|
|
66
|
+
if (statSync(full).isDirectory()) out.push(...htmlFiles(full));
|
|
67
|
+
else if (entry.endsWith('.html')) out.push(full);
|
|
68
|
+
}
|
|
69
|
+
return out.sort();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function main() {
|
|
73
|
+
const dist = resolve(process.cwd(), process.argv[2] ?? 'dist');
|
|
74
|
+
const files = htmlFiles(dist);
|
|
75
|
+
let errorCount = 0;
|
|
76
|
+
let warningCount = 0;
|
|
77
|
+
for (const file of files) {
|
|
78
|
+
const { errors, warnings } = checkHtml(readFileSync(file, 'utf-8'));
|
|
79
|
+
if (!errors.length && !warnings.length) continue;
|
|
80
|
+
console.log(relative(dist, file));
|
|
81
|
+
for (const e of errors) console.log(` error: ${e}`);
|
|
82
|
+
for (const w of warnings) console.log(` warning: ${w}`);
|
|
83
|
+
errorCount += errors.length;
|
|
84
|
+
warningCount += warnings.length;
|
|
85
|
+
}
|
|
86
|
+
console.log(`\n${files.length} pages checked: ${errorCount} errors, ${warningCount} warnings.`);
|
|
87
|
+
if (errorCount > 0) process.exitCode = 1;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
91
|
+
main();
|
|
92
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Generates a site's raster icons and web manifest from its own
|
|
4
|
+
* public/favicon.svg and brand-identity instance, into public/:
|
|
5
|
+
*
|
|
6
|
+
* apple-touch-icon.png 180x180, mark on the brand canvas colour
|
|
7
|
+
* (iOS fills transparency with black otherwise)
|
|
8
|
+
* icon-192.png 192x192, transparent
|
|
9
|
+
* icon-512.png 512x512, transparent
|
|
10
|
+
* site.webmanifest name, short_name, icons, theme/background colour
|
|
11
|
+
* favicon.ico 16, 32 and 48px, transparent -- browsers and crawlers
|
|
12
|
+
* request /favicon.ico whatever the page links
|
|
13
|
+
*
|
|
14
|
+
* SiteLayout links each of these only when the file exists, so running
|
|
15
|
+
* this is what turns them on for a site. Re-run when favicon.svg or the
|
|
16
|
+
* brand's name/colours change; output is deterministic for the same inputs.
|
|
17
|
+
*
|
|
18
|
+
* Usage:
|
|
19
|
+
* luhtech-sync-icons <path-to-brand-identity.json> [--public <dir>] [--check]
|
|
20
|
+
* luhtech-sync-icons content/brand/hilja.json
|
|
21
|
+
*
|
|
22
|
+
* --check writes nothing and exits 1 if any output is missing or stale.
|
|
23
|
+
*/
|
|
24
|
+
import { readFileSync, writeFileSync, existsSync, realpathSync } from 'node:fs';
|
|
25
|
+
import { join, resolve } from 'node:path';
|
|
26
|
+
import { fileURLToPath } from 'node:url';
|
|
27
|
+
|
|
28
|
+
export const APPLE_PADDING = 0.125; // fraction of the canvas left clear on each side
|
|
29
|
+
|
|
30
|
+
export function buildManifest(brand) {
|
|
31
|
+
const name = brand?.identity?.name;
|
|
32
|
+
if (!name) throw new Error('brand identity.name is required');
|
|
33
|
+
const ink = brand?.color?.foundation?.ink?.hex;
|
|
34
|
+
const canvas = brand?.color?.foundation?.canvas?.hex;
|
|
35
|
+
const manifest = {
|
|
36
|
+
name,
|
|
37
|
+
short_name: name,
|
|
38
|
+
icons: [
|
|
39
|
+
{ src: '/icon-192.png', sizes: '192x192', type: 'image/png' },
|
|
40
|
+
{ src: '/icon-512.png', sizes: '512x512', type: 'image/png' },
|
|
41
|
+
],
|
|
42
|
+
start_url: '/',
|
|
43
|
+
display: 'browser',
|
|
44
|
+
};
|
|
45
|
+
if (ink) manifest.theme_color = ink;
|
|
46
|
+
if (canvas) manifest.background_color = canvas;
|
|
47
|
+
return JSON.stringify(manifest, null, 2) + '\n';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Places the mark inside a square of the given background colour with
|
|
51
|
+
// padding, by wrapping the source SVG as a nested <svg> element.
|
|
52
|
+
export function paddedSvg(svg, background, padding = APPLE_PADDING) {
|
|
53
|
+
const viewBox = svg.match(/viewBox=["']([^"']+)["']/)?.[1];
|
|
54
|
+
if (!viewBox) throw new Error('favicon.svg has no viewBox');
|
|
55
|
+
const inner = svg.replace(/^[\s\S]*?<svg\b/, '<svg').replace(/<svg\b([^>]*)>/, (_, attrs) => {
|
|
56
|
+
const kept = attrs.replace(/\s(width|height|x|y)=["'][^"']*["']/g, '');
|
|
57
|
+
const pos = (padding * 100).toFixed(3);
|
|
58
|
+
const size = ((1 - 2 * padding) * 100).toFixed(3);
|
|
59
|
+
return `<svg${kept} x="${pos}" y="${pos}" width="${size}" height="${size}">`;
|
|
60
|
+
});
|
|
61
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect width="100" height="100" fill="${background}"/>${inner}</svg>`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const ICO_SIZES = [16, 32, 48];
|
|
65
|
+
|
|
66
|
+
// An ICO file holding PNG images (supported by every current browser and
|
|
67
|
+
// by Windows since Vista): a 6-byte header, one 16-byte directory entry
|
|
68
|
+
// per image, then the PNG bytes themselves.
|
|
69
|
+
export function buildIco(pngs) {
|
|
70
|
+
const header = Buffer.alloc(6);
|
|
71
|
+
header.writeUInt16LE(0, 0); // reserved
|
|
72
|
+
header.writeUInt16LE(1, 2); // type: icon
|
|
73
|
+
header.writeUInt16LE(pngs.length, 4);
|
|
74
|
+
const entries = [];
|
|
75
|
+
let offset = 6 + 16 * pngs.length;
|
|
76
|
+
for (const { size, data } of pngs) {
|
|
77
|
+
const e = Buffer.alloc(16);
|
|
78
|
+
e.writeUInt8(size >= 256 ? 0 : size, 0); // width (0 means 256)
|
|
79
|
+
e.writeUInt8(size >= 256 ? 0 : size, 1); // height
|
|
80
|
+
e.writeUInt8(0, 2); // palette colours
|
|
81
|
+
e.writeUInt8(0, 3); // reserved
|
|
82
|
+
e.writeUInt16LE(1, 4); // colour planes
|
|
83
|
+
e.writeUInt16LE(32, 6); // bits per pixel
|
|
84
|
+
e.writeUInt32LE(data.length, 8);
|
|
85
|
+
e.writeUInt32LE(offset, 12);
|
|
86
|
+
entries.push(e);
|
|
87
|
+
offset += data.length;
|
|
88
|
+
}
|
|
89
|
+
return Buffer.concat([header, ...entries, ...pngs.map((p) => p.data)]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function render(svg, size) {
|
|
93
|
+
const { Resvg } = await import('@resvg/resvg-js');
|
|
94
|
+
return new Resvg(svg, { fitTo: { mode: 'width', value: size } }).render().asPng();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function main() {
|
|
98
|
+
const args = process.argv.slice(2);
|
|
99
|
+
const brandPath = args.find((a) => !a.startsWith('--') && args[args.indexOf(a) - 1] !== '--public');
|
|
100
|
+
const publicIdx = args.indexOf('--public');
|
|
101
|
+
const publicDir = resolve(process.cwd(), publicIdx >= 0 ? args[publicIdx + 1] : 'public');
|
|
102
|
+
const check = args.includes('--check');
|
|
103
|
+
if (!brandPath) {
|
|
104
|
+
console.error('Usage: luhtech-sync-icons <path-to-brand-identity.json> [--public <dir>] [--check]');
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
const brand = JSON.parse(readFileSync(brandPath, 'utf-8'));
|
|
108
|
+
const svgPath = join(publicDir, 'favicon.svg');
|
|
109
|
+
if (!existsSync(svgPath)) {
|
|
110
|
+
console.error(`No favicon.svg at ${svgPath}`);
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
113
|
+
const svg = readFileSync(svgPath, 'utf-8');
|
|
114
|
+
const canvas = brand?.color?.foundation?.canvas?.hex ?? '#FFFFFF';
|
|
115
|
+
|
|
116
|
+
const outputs = [
|
|
117
|
+
['apple-touch-icon.png', await render(paddedSvg(svg, canvas), 180)],
|
|
118
|
+
['icon-192.png', await render(svg, 192)],
|
|
119
|
+
['icon-512.png', await render(svg, 512)],
|
|
120
|
+
['site.webmanifest', Buffer.from(buildManifest(brand))],
|
|
121
|
+
['favicon.ico', buildIco(await Promise.all(ICO_SIZES.map(async (size) => ({ size, data: await render(svg, size) }))))],
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
let stale = 0;
|
|
125
|
+
for (const [file, data] of outputs) {
|
|
126
|
+
const dest = join(publicDir, file);
|
|
127
|
+
const current = existsSync(dest) ? readFileSync(dest) : null;
|
|
128
|
+
if (current && Buffer.compare(current, data) === 0) continue;
|
|
129
|
+
stale++;
|
|
130
|
+
console.log(` ${current ? '~' : '+'} ${file}`);
|
|
131
|
+
if (!check) writeFileSync(dest, data);
|
|
132
|
+
}
|
|
133
|
+
console.log(`${check ? '[check] ' : ''}${stale} of ${outputs.length} ${check ? 'missing or stale' : 'written'}.`);
|
|
134
|
+
if (check && stale > 0) process.exitCode = 1;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Run as a CLI (including through node_modules/.bin's symlink), not on import from tests.
|
|
138
|
+
if (process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
139
|
+
await main();
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luhtech/site-template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "Shared Astro component library + content-instance contract for LuhTech venture marketing sites -- one template renders every site from content/page.schema.json + content/site.schema.json instances.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,7 +32,9 @@
|
|
|
32
32
|
"luhtech-page-claims": "bin/luhtech-page-claims.mjs",
|
|
33
33
|
"luhtech-sync-marks": "bin/luhtech-sync-marks.mjs",
|
|
34
34
|
"luhtech-sync-animated-marks": "bin/luhtech-sync-animated-marks.mjs",
|
|
35
|
-
"luhtech-sync-venture-directory": "bin/luhtech-sync-venture-directory.mjs"
|
|
35
|
+
"luhtech-sync-venture-directory": "bin/luhtech-sync-venture-directory.mjs",
|
|
36
|
+
"luhtech-sync-icons": "bin/luhtech-sync-icons.mjs",
|
|
37
|
+
"luhtech-seo-check": "bin/luhtech-seo-check.mjs"
|
|
36
38
|
},
|
|
37
39
|
"scripts": {
|
|
38
40
|
"test": "node --experimental-strip-types --test test/*.test.mjs"
|
|
@@ -43,6 +45,7 @@
|
|
|
43
45
|
},
|
|
44
46
|
"dependencies": {
|
|
45
47
|
"@lucide/astro": "^1.42.0",
|
|
48
|
+
"@resvg/resvg-js": "^2.6.2",
|
|
46
49
|
"ajv": "^8.17.1",
|
|
47
50
|
"ajv-formats": "^3.0.1"
|
|
48
51
|
},
|
|
@@ -11,12 +11,26 @@
|
|
|
11
11
|
import type { LoadedPage } from '../content/loadPage.js';
|
|
12
12
|
import type { LoadedSite } from '../content/loadSite.js';
|
|
13
13
|
import Icon from '../components/Icon.astro';
|
|
14
|
+
import { existsSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import type { LoadedBrand } from '../content/loadBrand.js';
|
|
17
|
+
import { ICON_FILES, buildSeoHead } from '../lib/seoHead.js';
|
|
14
18
|
|
|
15
19
|
export interface Props {
|
|
16
20
|
site: LoadedSite;
|
|
17
21
|
page: LoadedPage;
|
|
22
|
+
/** Optional: supplies og:site_name and theme-color. */
|
|
23
|
+
brand?: LoadedBrand;
|
|
18
24
|
}
|
|
19
|
-
const { site, page } = Astro.props as Props;
|
|
25
|
+
const { site, page, brand } = Astro.props as Props;
|
|
26
|
+
|
|
27
|
+
// Icon/manifest links are emitted only for files the site really ships in
|
|
28
|
+
// public/ (generated by luhtech-sync-icons), so a site that hasn't run it
|
|
29
|
+
// yet gets no dangling links. Resolved at build time from the site root.
|
|
30
|
+
const publicFiles = new Set(
|
|
31
|
+
Object.values(ICON_FILES).filter((f) => existsSync(join(process.cwd(), 'public', f)))
|
|
32
|
+
);
|
|
33
|
+
const head = buildSeoHead({ site: Astro.site, pathname: Astro.url.pathname, brand, publicFiles });
|
|
20
34
|
|
|
21
35
|
const seo = page.seo as { title: string; description: string; ogImage?: string; noindex?: boolean };
|
|
22
36
|
const footer = site.footer as { legalLine: string; parentHref?: string };
|
|
@@ -53,6 +67,9 @@ const linkItems = nav.filter((item) => item !== ctaItem);
|
|
|
53
67
|
{seo.noindex && <meta name="robots" content="noindex, nofollow" />}
|
|
54
68
|
|
|
55
69
|
<meta property="og:type" content="website" />
|
|
70
|
+
{head.canonical && <meta property="og:url" content={head.canonical} />}
|
|
71
|
+
{head.siteName && <meta property="og:site_name" content={head.siteName} />}
|
|
72
|
+
<meta property="og:locale" content={head.locale} />
|
|
56
73
|
<meta property="og:title" content={seo.title} />
|
|
57
74
|
<meta property="og:description" content={seo.description} />
|
|
58
75
|
{seo.ogImage && <meta property="og:image" content={seo.ogImage} />}
|
|
@@ -61,6 +78,9 @@ const linkItems = nav.filter((item) => item !== ctaItem);
|
|
|
61
78
|
<meta name="twitter:description" content={seo.description} />
|
|
62
79
|
{seo.ogImage && <meta name="twitter:image" content={seo.ogImage} />}
|
|
63
80
|
|
|
81
|
+
{head.themeColor && <meta name="theme-color" content={head.themeColor} />}
|
|
82
|
+
{head.links.map((l) => <link rel={l.rel} href={l.href} type={l.type} sizes={l.sizes} />)}
|
|
83
|
+
|
|
64
84
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
|
65
85
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
|
66
86
|
<link
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Browser + search metadata SiteLayout emits beyond page.seo's own
|
|
2
|
+
// title/description/ogImage. Pure so it can be unit-tested; the layout
|
|
3
|
+
// passes it what only Astro knows (Astro.site, Astro.url.pathname) and
|
|
4
|
+
// which icon files the consuming site actually ships in public/.
|
|
5
|
+
//
|
|
6
|
+
// d-2026-09-26-sites-browser-seo-baseline: a live audit of all 10 sites
|
|
7
|
+
// found 29 of 32 pages with no icon link (every site serves
|
|
8
|
+
// /favicon.svg, nothing linked it, so browsers asked for /favicon.ico
|
|
9
|
+
// and got a 404), 29 of 32 with no canonical/og:url, and none with
|
|
10
|
+
// og:site_name, og:locale, theme-color, apple-touch-icon or a manifest.
|
|
11
|
+
|
|
12
|
+
export const ICON_FILES = {
|
|
13
|
+
svg: 'favicon.svg',
|
|
14
|
+
appleTouch: 'apple-touch-icon.png',
|
|
15
|
+
png192: 'icon-192.png',
|
|
16
|
+
png512: 'icon-512.png',
|
|
17
|
+
manifest: 'site.webmanifest',
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
// favicon.ico is generated too (luhtech-sync-icons) but never linked:
|
|
21
|
+
// browsers and crawlers request /favicon.ico on their own.
|
|
22
|
+
|
|
23
|
+
export type IconFile = (typeof ICON_FILES)[keyof typeof ICON_FILES];
|
|
24
|
+
|
|
25
|
+
export interface BrandLike {
|
|
26
|
+
identity?: { name?: string };
|
|
27
|
+
color?: { foundation?: { ink?: { hex?: string }; canvas?: { hex?: string } } };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SeoHeadInput {
|
|
31
|
+
/** Astro.site -- the site's configured origin (astro.config `site`). */
|
|
32
|
+
site?: URL | string;
|
|
33
|
+
/** Astro.url.pathname for the page being rendered. */
|
|
34
|
+
pathname: string;
|
|
35
|
+
brand?: BrandLike;
|
|
36
|
+
/** Which ICON_FILES exist in the consuming site's public/ directory. */
|
|
37
|
+
publicFiles: ReadonlySet<string>;
|
|
38
|
+
locale?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LinkTag {
|
|
42
|
+
rel: string;
|
|
43
|
+
href: string;
|
|
44
|
+
type?: string;
|
|
45
|
+
sizes?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SeoHead {
|
|
49
|
+
canonical: string | null;
|
|
50
|
+
siteName: string | null;
|
|
51
|
+
locale: string;
|
|
52
|
+
themeColor: string | null;
|
|
53
|
+
links: LinkTag[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function canonicalUrl(site: URL | string | undefined, pathname: string): string | null {
|
|
57
|
+
if (!site) return null;
|
|
58
|
+
return new URL(pathname || '/', site).href;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function buildSeoHead(input: SeoHeadInput): SeoHead {
|
|
62
|
+
const { brand, publicFiles } = input;
|
|
63
|
+
const links: LinkTag[] = [];
|
|
64
|
+
const canonical = canonicalUrl(input.site, input.pathname);
|
|
65
|
+
if (canonical) links.push({ rel: 'canonical', href: canonical });
|
|
66
|
+
if (publicFiles.has(ICON_FILES.svg)) links.push({ rel: 'icon', href: `/${ICON_FILES.svg}`, type: 'image/svg+xml' });
|
|
67
|
+
if (publicFiles.has(ICON_FILES.png192)) links.push({ rel: 'icon', href: `/${ICON_FILES.png192}`, type: 'image/png', sizes: '192x192' });
|
|
68
|
+
if (publicFiles.has(ICON_FILES.appleTouch)) links.push({ rel: 'apple-touch-icon', href: `/${ICON_FILES.appleTouch}`, sizes: '180x180' });
|
|
69
|
+
if (publicFiles.has(ICON_FILES.manifest)) links.push({ rel: 'manifest', href: `/${ICON_FILES.manifest}` });
|
|
70
|
+
return {
|
|
71
|
+
canonical,
|
|
72
|
+
siteName: brand?.identity?.name ?? null,
|
|
73
|
+
locale: input.locale ?? 'en_US',
|
|
74
|
+
// The header renders on bg-ink, so the browser chrome matches it.
|
|
75
|
+
themeColor: brand?.color?.foundation?.ink?.hex ?? null,
|
|
76
|
+
links,
|
|
77
|
+
};
|
|
78
|
+
}
|