# categories 清单成员

> categories 成员是一个字符串数组（按惯例小写），告诉应用商店和目录把 Web 应用归入哪些分区；浏览器不会据此渲染任何内容。

`categories` 清单成员用字符串数组列出 Web 应用所属的商店分区，例如 `"productivity"` 或
`"games"`。W3C Application Information 注册表把它定义为给目录和商店的提示；浏览器引擎不会因
该成员改变应用的安装、启动与渲染方式。

MDN 把它的浏览器兼容性标为不适用，因为使用该成员的是商店而不是浏览器；[browser-compat-data 的 `manifests/webapp` 目录](https://github.com/mdn/browser-compat-data/tree/main/manifests/webapp)（github.com）下也没有 `categories.json` 文件。Chromium 的清单解析器（`manifest_parser.cc`）没有处理该成员的代码，MDN 说明其他浏览器可能解析它但不产生任何效果。PWABuilder
这类商店打包工具在生成 Microsoft Store 或 Play Store 封装的列表元数据时会读取这个数组。

## 成员

| 属性 | 值 |
|---|---|
| 类型 | 字符串数组。注册表公布了一份已知值列表，包括 `business`、`education`、`entertainment`、`finance`、`food`、`games`、`health`、`kids`、`lifestyle`、`music`、`news`、`productivity`、`shopping`、`social`、`sports`、`travel`、`utilities` 与 `weather`。其他字符串也合法；商店会把它们映射到自己的分类体系或忽略。 |
| 默认值 | 空数组。找不到可用分类的商店会自行归类。 |
| 示例值 | `["food", "health", "lifestyle"]`。 |

注册表鼓励作者写小写值，没有规定比较规则。Chromium 不读取这个成员，
所以值不是数组时，清单的其余部分不受影响。

商店可以把应用列在清单没有提到的分区下；清单列出的分类也不会
出现在已安装应用或浏览器安装对话框的任何位置。

:::note
Chromium 的清单解析器（[`manifest_parser.cc`](https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/modules/manifest/manifest_parser.cc)）及其 `Manifest` mojom 定义中都没有处理 `categories` 的代码，DevTools 的 [`AppManifestView.ts`](https://github.com/ChromeDevTools/devtools-frontend/blob/main/front_end/panels/application/AppManifestView.ts) 也没有对应的行。这个成员不会被读取，所以类型错误的取值（例如 `"categories": "games"`）不会在 **Errors and warnings** 下产生任何条目。
:::

## 示例

这个成员没有运行时 API，可以检查部署后的清单来验证取值。

### 为一个应用声明多个商店分区

一款膳食规划应用适合不止一个分区。列出三个分类，支持多分类列表的商店可以把它放进每一个分区；
只支持单一分类的商店取它认得的第一个。

```json
{
  "name": "Meal Planner",
  "short_name": "Meals",
  "start_url": "/",
  "display": "standalone",
  "categories": ["food", "health", "lifestyle"]
}
```

把最贴切的分类放在第一位：必须选出唯一主分类的打包工具没有别的依据可用。

### 在构建步骤中校验上线的值

没有任何 API 能报告浏览器或商店是否采用了该成员，所以有用的检查是在构建或测试阶段对将要部署的
清单做断言。函数按注册表的小写建议把每一项转为小写，并标出注册表已知列表之外的值。

```js
const KNOWN = new Set([
  'beauty', 'books', 'books & reference', 'business', 'cars', 'dating',
  'design', 'developer', 'developer tools', 'development', 'education', 'entertainment',
  'events', 'fashion', 'finance', 'fitness', 'food', 'fundraising',
  'games', 'government', 'graphics', 'graphics & design', 'health', 'health & fitness',
  'kids', 'lifestyle', 'magazines', 'medical', 'multimedia', 'multimedia design',
  'music', 'navigation', 'network', 'networking', 'news', 'personalization',
  'parenting', 'pets', 'photo', 'photo & video', 'politics', 'productivity',
  'reference', 'security', 'shopping', 'social', 'social networking', 'sports',
  'transportation', 'travel', 'utilities', 'video', 'weather',
]);

export function checkCategories(manifest) {
  const raw = manifest.categories;
  if (raw === undefined) return { declared: [], unknown: [] }; // 可选成员
  if (!Array.isArray(raw)) throw new Error("categories must be an array of strings");
  const declared = raw.map((c) => String(c).toLowerCase());
  const unknown = declared.filter((c) => !KNOWN.has(c));
  return { declared, unknown };
}
```

缺少该成员的清单是合法的，所以函数成功返回空列表；未知值只报告、不拒绝，因为商店接受
自由格式的字符串。

### 从页面自己的清单里读取该值

设置页或"关于"页若需要显示商店元数据，可以抓取文档链接的清单并读取数组。回退分支处理没有
清单链接的页面。

```js
async function declaredCategories() {
  const link = document.querySelector('link[rel="manifest"]');
  if (!link) return []; // 该文档没有链接清单
  const manifest = await fetch(link.href).then((r) => r.json());
  return Array.isArray(manifest.categories) ? manifest.categories : [];
}
```

它只能确认站点声明了什么，不能说明商店是否真的把应用列在了那些分区下。

## 另请参阅

- [description 清单成员](/zh/reference/manifest/description/)
- [screenshots 清单成员](/zh/reference/manifest/screenshots/)
- [通过应用商店分发 PWA](/zh/guides/app-store-distribution/)
- [Web App Manifest - Application Information: categories member](https://w3c.github.io/manifest-app-info/#categories-member)（w3c.github.io）
- [categories](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Manifest/Reference/categories)（developer.mozilla.org）