perf: improve SEO and page performance #3

Merged
chrnx merged 16 commits from feature/seo-performance into main 2026-08-03 00:28:09 +00:00
Showing only changes of commit 96a0412531 - Show all commits
+84 -42
View File
@@ -36,18 +36,28 @@ function verifyPageMetadata({ label, html }) {
assert.equal(canonicals.length, 1, `${label} must have exactly one canonical URL`); assert.equal(canonicals.length, 1, `${label} must have exactly one canonical URL`);
const canonicalUrl = attribute(canonicals[0], 'href'); const canonicalUrl = attribute(canonicals[0], 'href');
assert.ok(canonicalUrl, `${label} canonical URL must not be empty`); assert.ok(canonicalUrl, `${label} canonical URL must not be empty`);
assert.doesNotThrow(() => new URL(canonicalUrl), `${label} canonical URL must be valid`); let parsedCanonical;
assert.doesNotThrow(
() => {
parsedCanonical = new URL(canonicalUrl);
},
`${label} canonical URL must be valid`,
);
assert.ok(
['https:', 'http:'].includes(parsedCanonical.protocol),
`${label} canonical URL must use HTTP or HTTPS`,
);
const descriptions = metaTags(html, 'name', 'description'); const descriptions = metaTags(html, 'name', 'description');
assert.equal(descriptions.length, 1, `${label} must have exactly one meta description`); assert.equal(descriptions.length, 1, `${label} must have exactly one meta description`);
assert.ok(attribute(descriptions[0], 'content')?.trim(), `${label} meta description must not be empty`); assert.ok(attribute(descriptions[0], 'content')?.trim(), `${label} meta description must not be empty`);
} }
function findHtmlFiles(directory) { function findGeneratedTextAssets(directory) {
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
const entryPath = join(directory, entry.name); const entryPath = join(directory, entry.name);
if (entry.isDirectory()) return findHtmlFiles(entryPath); if (entry.isDirectory()) return findGeneratedTextAssets(entryPath);
return entry.isFile() && entry.name.endsWith('.html') ? [entryPath] : []; return entry.isFile() && /\.(?:html|css)$/i.test(entry.name) ? [entryPath] : [];
}); });
} }
@@ -73,13 +83,23 @@ function parseJsonLd(html, label) {
}); });
} }
function containsType(value, expectedType) { function findNodesByType(value, expectedType, matches = []) {
if (Array.isArray(value)) return value.some((item) => containsType(item, expectedType)); if (Array.isArray(value)) {
if (value === null || typeof value !== 'object') return false; for (const item of value) findNodesByType(item, expectedType, matches);
return matches;
}
if (value === null || typeof value !== 'object') return matches;
const types = Array.isArray(value['@type']) ? value['@type'] : [value['@type']]; const types = Array.isArray(value['@type']) ? value['@type'] : [value['@type']];
if (types.includes(expectedType)) return true; if (types.includes(expectedType)) matches.push(value);
return Object.values(value).some((item) => containsType(item, expectedType)); for (const item of Object.values(value)) findNodesByType(item, expectedType, matches);
return matches;
}
function requireValidDate(value, message) {
assert.equal(typeof value, 'string', `${message} must be a string`);
assert.ok(value.trim(), `${message} must not be empty`);
assert.ok(!Number.isNaN(Date.parse(value)), `${message} must be a valid date`);
} }
function requireSingleMeta(html, label, property, expectedContent) { function requireSingleMeta(html, label, property, expectedContent) {
@@ -94,47 +114,68 @@ function requireSingleMeta(html, label, property, expectedContent) {
} else { } else {
assert.ok(attribute(matching[0], 'content')?.trim(), `${label} ${property} must not be empty`); assert.ok(attribute(matching[0], 'content')?.trim(), `${label} ${property} must not be empty`);
} }
return matching[0];
} }
const requiredPages = [ assert.ok(existsSync(distDir), 'Missing generated dist directory');
{ label: 'home page', path: join(distDir, 'index.html') }, const generatedTextAssets = findGeneratedTextAssets(distDir);
{ label: '404 page', path: join(distDir, '404.html') }, const htmlPages = generatedTextAssets
{ label: 'blog index', path: join(distDir, 'blog', 'index.html') }, .filter((filePath) => filePath.toLowerCase().endsWith('.html'))
]; .map((filePath) => ({
label: relative(distDir, filePath),
const blogDir = join(distDir, 'blog'); path: filePath,
assert.ok(existsSync(blogDir), 'Missing generated blog directory'); html: readHtml(filePath),
const postPages = readdirSync(blogDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => ({
label: `blog post ${entry.name}`,
path: join(blogDir, entry.name, 'index.html'),
})); }));
assert.ok(htmlPages.length > 0, 'Expected at least one generated HTML page');
for (const page of htmlPages) verifyPageMetadata(page);
function requirePage(relativePath, label) {
const page = htmlPages.find((candidate) => candidate.label === relativePath);
assert.ok(page, `Missing generated ${label}: ${relativePath}`);
return { ...page, label };
}
const home = requirePage('index.html', 'home page');
const notFound = requirePage('404.html', '404 page');
requirePage(join('blog', 'index.html'), 'blog index');
const postPages = htmlPages
.filter((page) => /^blog[/\\][^/\\]+[/\\]index\.html$/.test(page.label))
.map((page) => ({ ...page, label: `blog post ${page.label.split(/[/\\]/)[1]}` }));
assert.ok(postPages.length > 0, 'Expected at least one generated blog post'); assert.ok(postPages.length > 0, 'Expected at least one generated blog post');
const pages = [...requiredPages, ...postPages].map((page) => ({
...page,
html: readHtml(page.path),
}));
for (const page of pages) verifyPageMetadata(page);
const home = pages.find((page) => page.label === 'home page');
const homeJsonLd = parseJsonLd(home.html, home.label); const homeJsonLd = parseJsonLd(home.html, home.label);
assert.ok( assert.ok(
containsType(homeJsonLd, 'SoftwareApplication'), findNodesByType(homeJsonLd, 'SoftwareApplication').length > 0,
'home page JSON-LD must contain SoftwareApplication', 'home page JSON-LD must contain SoftwareApplication',
); );
for (const post of pages.filter((page) => page.label.startsWith('blog post '))) { for (const post of postPages) {
const jsonLd = parseJsonLd(post.html, post.label); const jsonLd = parseJsonLd(post.html, post.label);
assert.ok(containsType(jsonLd, 'BlogPosting'), `${post.label} JSON-LD must contain BlogPosting`); const blogPostings = findNodesByType(jsonLd, 'BlogPosting');
assert.equal(blogPostings.length, 1, `${post.label} JSON-LD must contain one BlogPosting`);
const [blogPosting] = blogPostings;
requireValidDate(blogPosting.datePublished, `${post.label} BlogPosting datePublished`);
requireValidDate(blogPosting.dateModified, `${post.label} BlogPosting dateModified`);
assert.equal(typeof blogPosting.author, 'object', `${post.label} BlogPosting author must be an object`);
assert.ok(
!Array.isArray(blogPosting.author) && blogPosting.author !== null,
`${post.label} BlogPosting author must be an object`,
);
assert.ok(
typeof blogPosting.author.name === 'string' && blogPosting.author.name.trim(),
`${post.label} BlogPosting author.name must not be empty`,
);
requireSingleMeta(post.html, post.label, 'og:type', 'article'); requireSingleMeta(post.html, post.label, 'og:type', 'article');
requireSingleMeta(post.html, post.label, 'article:published_time'); const publishedTime = requireSingleMeta(post.html, post.label, 'article:published_time');
requireSingleMeta(post.html, post.label, 'article:author'); requireSingleMeta(post.html, post.label, 'article:author');
requireValidDate(
attribute(publishedTime, 'content'),
`${post.label} article:published_time content`,
);
} }
const notFound = pages.find((page) => page.label === '404 page');
const robots = metaTags(notFound.html, 'name', 'robots'); const robots = metaTags(notFound.html, 'name', 'robots');
assert.equal(robots.length, 1, '404 page must have exactly one robots meta tag'); assert.equal(robots.length, 1, '404 page must have exactly one robots meta tag');
assert.equal( assert.equal(
@@ -143,13 +184,14 @@ assert.equal(
'404 page robots metadata must be noindex, follow', '404 page robots metadata must be noindex, follow',
); );
for (const htmlPath of findHtmlFiles(distDir)) { for (const assetPath of generatedTextAssets) {
const html = readHtml(htmlPath); const source = readFileSync(assetPath, 'utf8');
assert.doesNotMatch( for (const forbiddenHost of ['fonts.googleapis.com', 'fonts.gstatic.com']) {
html, assert.ok(
/fonts\.(?:googleapis|gstatic)\.com/i, !source.toLowerCase().includes(forbiddenHost),
`${relative(distDir, htmlPath)} must not reference Google Fonts`, `${relative(distDir, assetPath)} must not reference ${forbiddenHost}`,
); );
}
} }
assert.ok(existsSync(join(distDir, '.htaccess')), 'Missing generated dist/.htaccess'); assert.ok(existsSync(join(distDir, '.htaccess')), 'Missing generated dist/.htaccess');