示例工程 40-seo-metadata
40-seo-metadata/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SEO 与分享 · 元数据完整指南</title>
<meta name="description" content="title、description、canonical、robots、Open Graph、Twitter Card 与 JSON-LD 的写法与边界。" />
<meta name="theme-color" content="#2563eb" />
<meta name="robots" content="index,follow" />
<link rel="canonical" href="/40-seo-metadata/" />
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='6' fill='%232563eb'/%3E%3Cpath d='M8 20 L16 8 L24 20' stroke='white' stroke-width='3' fill='none'/%3E%3C/svg%3E" />
<meta property="og:type" content="article" />
<meta property="og:title" content="SEO 与分享 · 元数据完整指南" />
<meta property="og:description" content="从 title 到 JSON-LD,把页面的身份写清楚。" />
<meta property="og:url" content="https://example.com/40-seo-metadata/" />
<meta property="og:image" content="https://example.com/og/seo-metadata.png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="og:locale" content="zh_CN" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="SEO 与分享 · 元数据完整指南" />
<meta name="twitter:description" content="从 title 到 JSON-LD,把页面的身份写清楚。" />
<meta name="twitter:image" content="https://example.com/og/seo-metadata.png" />
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "SEO 与分享:元数据完整指南",
"description": "从 title 到 JSON-LD,把页面的身份写清楚。",
"author": { "@type": "Person", "name": "林小满" },
"datePublished": "2026-11-28",
"dateModified": "2026-11-28",
"image": "https://example.com/og/seo-metadata.png",
"mainEntityOfPage": "https://example.com/40-seo-metadata/"
}
</script>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 30px 20px 40px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 800px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 26px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 10px; font-size: clamp(30px, 6vw, 42px); line-height: 1.15; }
.lead { margin: 0 0 20px; color: #334155; font-size: 17px; }
section { margin-top: 26px; padding-top: 18px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 21px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
.preview { margin: 14px 0; border: 1px solid #e2e8f0; border-radius: 10px; overflow: hidden; background: #fff; }
.preview .image { height: 180px; background: linear-gradient(135deg, #dbeafe, #bfdbfe); display: grid; place-items: center; color: #1e40af; font-size: 13px; }
.preview .body { padding: 12px 14px; }
.preview .domain { color: #64748b; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; }
.preview strong { display: block; margin: 4px 0; font-size: 16px; }
.preview p { margin: 0; font-size: 13px; color: #475569; }
table { width: 100%; border-collapse: collapse; font-size: 14px; background: #fff; }
caption { padding: 8px; text-align: left; color: #475569; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; vertical-align: top; }
thead th { background: #f1f5f9; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
pre { margin: 10px 0 0; padding: 14px; overflow-x: auto; border-radius: 10px; background: #0f172a; color: #e2e8f0; font-size: 12px; line-height: 1.7; }
pre code { padding: 0; background: none; color: inherit; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
footer { margin-top: 28px; padding-top: 14px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 18px 14px 32px; }
.site-header { flex-direction: column; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">SEO / 2026</span>
<nav aria-label="页内导航">
<a href="#title">标题</a>
<a href="#canonical">索引</a>
<a href="#social">分享</a>
<a href="#jsonld">结构化</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 40</p>
<h1>SEO 与分享</h1>
<p class="lead">
元数据不显示在正文里,却决定页面在搜索与分享中长什么样。写准,比写多重要。
</p>
<section id="title">
<h2>1. 标题与描述</h2>
<p><code>title</code> 是搜索结果的标题,<code>description</code> 是摘要。</p>
<p>标题写「页面主题 · 站点名」,描述写一句具体的价值。</p>
<p>每页标题唯一,描述不复制正文第一段。</p>
<div class="preview">
<div class="image">OG 图 1200×630</div>
<div class="body">
<span class="domain">example.com</span>
<strong>SEO 与分享 · 元数据完整指南</strong>
<p>从 title 到 JSON-LD,把页面的身份写清楚。</p>
</div>
</div>
</section>
<section id="canonical">
<h2>2. canonical 与 robots</h2>
<p><code>canonical</code> 告诉搜索引擎哪个是主版本。</p>
<p><code>robots</code> 控制索引与跟随;草稿页用 <code>noindex</code>。</p>
<p class="note">示例中的 canonical 用相对路径;生产环境应写绝对 URL。</p>
<table>
<caption>常见 robots 指令</caption>
<thead><tr><th scope="col">指令</th><th scope="col">作用</th></tr></thead>
<tbody>
<tr><th scope="row"><code>index</code></th><td>允许索引</td></tr>
<tr><th scope="row"><code>noindex</code></th><td>不进索引,但仍可抓取</td></tr>
<tr><th scope="row"><code>follow</code></th><td>跟随链接</td></tr>
<tr><th scope="row"><code>nofollow</code></th><td>不跟随链接</td></tr>
</tbody>
</table>
</section>
<section id="social">
<h2>3. 分享元数据</h2>
<p>Open Graph 决定分享卡片的标题、描述与图片。</p>
<p>Twitter Card 补充平台特定信息。</p>
<p>图片用 1200×630,加载稳定,不要放会过期的数字。</p>
</section>
<section id="jsonld">
<h2>4. 结构化数据</h2>
<p>JSON-LD 描述页面实体,例如文章、产品、FAQ。</p>
<p>数据必须与页面可见内容一致。</p>
<pre><code><script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "…",
"author": { "@type": "Person", "name": "…" }
}
</script></code></pre>
</section>
<section id="rules">
<h2>5. 规则</h2>
<ul>
<li>每页 title 唯一,描述具体。</li>
<li>canonical 指向主版本,别指向 404。</li>
<li>草稿用 noindex,不要用 robots.txt 阻止抓取。</li>
<li>OG 图要稳定、可访问、尺寸正确。</li>
<li>JSON-LD 与页面内容一致,不伪造评分。</li>
<li>sitemap 只放正式页面。</li>
</ul>
</section>
</main>
<footer>
<p>示例内容为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>