PWA 的 SEO 与结构化数据
发布于
搜索引擎按普通网站索引以网站形式分发的 PWA,manifest 和 Service Worker 不会改善索引。MDN 的可安装性指南称之为 “making it discoverable through web search”。用与可见 HTML 相同的数据,为每个公开页面生成 WebApplication JSON-LD,再用 Google 富媒体搜索结果测试验证;构建时检查标记,一旦无法解析就让构建失败。
你需要把主要内容渲染为 HTML 的页面(服务端渲染或预渲染)、每页一个公开 URL,以及修改 <head> 模板的权限。
选择 schema.org 类型与属性
链接到“选择 schema.org 类型与属性”一节schema.org 在 Thing > CreativeWork > SoftwareApplication > WebApplication 下定义 WebApplication。它自有的属性是 browserRequirements,描述为 “Specifies browser requirements in human-readable text. For example, ‘requires HTML5 support’”。有用的属性来自 SoftwareApplication:applicationCategory、operatingSystem、screenshot、softwareVersion、featureList,以及(继承自 CreativeWork)表示免费或付费的 offers。取值直接用 manifest 里已有的数据,这样 name、description 与截图 URL 就不会偏离安装界面显示的内容。
由 manifest 数据生成 JSON-LD
链接到“由 manifest 数据生成 JSON-LD”一节Google Search Central 推荐 JSON-LD,因为它比 Microdata 或 RDFa “less prone to user errors”,并接受它出现在 “a <script> tag in the <head> and <body> elements”。在页面模板中,每页序列化一个对象:
<script type="application/ld+json">{ "@context": "https://schema.org", "@type": "WebApplication", "name": "Field Notes", "url": "https://notes.example/", "description": "Offline-first notes that sync when you are back online.", "applicationCategory": "ProductivityApplication", "operatingSystem": "Any", "browserRequirements": "Requires JavaScript and a browser with service worker support.", "screenshot": "https://notes.example/screenshots/desktop-wide.png", "offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" }}</script>Google 的通用准则适用于每个属性:“don’t add structured data about information that is not visible to the user, even if the information is accurate”,以及 “Don’t create blank or empty pages just to hold structured data”。页面上没有显示的 screenshot,或与可见文案不一致的 description,即使事实正确也违反准则。
让内容在没有 Service Worker 时也可抓取
链接到“让内容在没有 Service Worker 时也可抓取”一节爬虫不运行 Service Worker。如果正文要等缓存的应用壳启动后才出现,爬虫就只能索引空壳。把文章、商品或笔记正文渲染进 HTML 响应,再让 Service Worker 缓存同一份响应。Google 表示它 “can read JSON-LD data when it is dynamically injected into the page’s contents”,因此可以在客户端注入标记。直接把静态块写进响应,则无需等待 JavaScript 执行就能读到它。
用富媒体搜索结果测试与构建检查验证
链接到“用富媒体搜索结果测试与构建检查验证”一节打开富媒体搜索结果测试,在 Enter a URL to test 中粘贴公开 URL,或切到 Code 粘贴 HTML,然后运行。结果会列出检测到的条目以及解析或必填属性错误。在构建中加一道检查,解析每段输出的标记,让模板 bug 在部署前就触发失败,避免发布后才在 Search Console 里发现:
import { readFile } from 'node:fs/promises';
export async function assertJsonLd(htmlPath) { const html = await readFile(htmlPath, 'utf8'); const blocks = [...html.matchAll(/<script type="application\/ld\+json">([\s\S]*?)<\/script>/g)]; if (blocks.length === 0) throw new Error(`${htmlPath}: no JSON-LD block`); for (const [, json] of blocks) { const data = JSON.parse(json); // 模板损坏时抛出异常 if (data['@type'] !== 'WebApplication') throw new Error(`${htmlPath}: unexpected @type ${data['@type']}`); }}JSON.parse 能发现格式错误的输出,但不检查 schema.org 词汇,后者正是富媒体搜索结果测试的工作,所以两者都要跑。
另请参阅
链接到“另请参阅”一节- 让它可安装
screenshotsmanifest 成员descriptionmanifest 成员- Introduction to structured data markup in Google Search(developers.google.com)
- WebApplication(schema.org)