SEO & Site Optimization
Relevant source files
The following files were used as context for generating this wiki page:
SEO and Site Optimization in the WHK project encompass a multi-layered approach to ensure high visibility, fast performance, and structural integrity of the generated documentation. The system leverages automated metadata generation, server-side rendering for complex mathematical content, and a post-processing build pipeline to optimize static assets.
The architecture focuses on three primary pillars: AI-driven frontmatter generation for search engine relevance, plugin-based static site enhancements via MkDocs, and a Bun-native build script for "in-place" optimization of HTML, CSS, and JavaScript.
Automated Metadata Generation
The project utilizes a specialized script, scripts/frontmatter/generate_frontmatter.js, to automate the creation of SEO-optimized metadata for Markdown files. This script functions as a "Frontmatter Turbo" tool that uses large language models (LLMs) to analyze article content and produce high-quality tags, titles, and descriptions.
AI Metadata Extraction Logic
The system scans markdown files in the docs/science directory and sends the content, along with extracted headings, to an AI model (NVIDIA NIM API). The model is prompted as an "Expert Document Architect & SEO Strategist" to generate:
- SEO Creative Titles: Restricted to approximately 22±3 Chinese characters following a specific "[Keyword] Explained: [Mechanism] and [Application]" pattern.
- Value-Driven Descriptions: 100-200 characters detailing technical routes or scientific mechanisms.
- Atomic Tags: A dense collection of 15-25 tags covering dimensions such as subject, core terms, detail terms, application scenarios, and target audiences.
Sources: scripts/frontmatter/generate_frontmatter.js:12-16, scripts/frontmatter/generate_frontmatter.js:145-175
This diagram illustrates the automated workflow for generating SEO-compliant metadata using AI.
Sources: scripts/frontmatter/generate_frontmatter.js:80-120, scripts/frontmatter/generate_frontmatter.js:203-227
Build Pipeline & Asset Optimization
The project employs a custom build.ts script executed via Bun to perform deep optimization on the generated static site. Unlike traditional builders that output to a new directory, this script performs "in-place" optimization within the site folder.
HTML and Head Reordering
The build process utilizes PostHTML to restructure the <head> of HTML documents for better browser performance and SEO. It prioritizes critical tags and removes redundancies.
| Category | Priority | Elements |
|---|---|---|
| Critical Meta | 0-1 | Charset, Viewport |
| Identity | 2 | Title |
| Discovery | 3-5 | Meta tags, Preconnect, Preload |
| Styles | 6-7 | Stylesheets, Inline Styles |
| Scripts | 8-11 | Modules, Defer, Async, Blocking |
Sources: build.ts:167-210, build.ts:314-323
Asset Compression Techniques
The pipeline integrates several industry-standard tools for asset minification:
- CSS: Processed with
PostCSS,Autoprefixer, andcssnano. It includes rules for merging declarations and sorting properties. - JavaScript: Compressed using
Terserwith two-pass compression and source map generation. - HTML: Minified via
html-minifier-terser, which collapses whitespace, removes comments, and minifies inline CSS/JS. - Image SEO: Automatically generates
altattributes based on filenames if they are missing.
Sources: build.ts:57-75, build.ts:86-97, build.ts:333-350, build.ts:413-435
The sequence of post-processing steps executed during the final build phase.
Sources: build.ts:360-460
Sitemap and Discovery
The project generates a dynamic sitemap using an MkDocs override to help search engines crawl the site effectively.
Sitemap Configuration
The overrides/sitemap.xml template iterates through all pages and includes:
- Canonical URLs: Prioritizes
canonical_urlfrom page metadata. - Last Modified Dates: Derived from the
document_dates_updatedmetadata field. - Change Frequency: Hardcoded to
dailyfor all entries. - Priority: Dynamically assigned if a
priorityfield exists in the page frontmatter.
Sources: overrides/sitemap.xml:4-24
Integrated SEO Plugins
The project configuration in pyproject.toml lists several dependencies specifically for SEO and site performance.
| Plugin/Dependency | Purpose |
|---|---|
mkdocs-advanced-seo | Advanced metadata and SEO management |
mkdocs-minify-html-plugin | Minification of HTML output |
mkdocs-rss-plugin | Generation of RSS feeds for content discovery |
mkdocs-katex-ssr | Server-side rendering of math to improve SEO crawlability |
mkdocs-document-dates | Tracks document creation/update for sitemap freshness |
Sources: pyproject.toml:9-23, README.md:95-104
Technical Implementation Details
CSS Optimization Config
const postcss_plugins = [
postcssNested(),
autoprefixer({ overrideBrowserslist: BROWSERSLIST }),
cssnano({
preset: [
"default",
{
cssDeclarationSorter: true,
mergeRules: true,
mergeIdents: true,
zindex: false,
},
],
}),
];
Sources: build.ts:57-75
Meta Deduplication Logic
The build script includes custom logic to prevent duplicate descriptions, authors, or keywords, ensuring that only the last occurrence of a specific meta tag is retained in the <head>.
Sources: build.ts:100-130
Conclusion
SEO and Site Optimization in the WHK project is a comprehensive system that balances content relevance through AI-generated metadata with technical performance via a rigorous build pipeline. By automating frontmatter creation, reordering document heads for faster rendering, and ensuring clean, minified assets, the project provides a highly discoverable and performant platform for educational content.