# name 与 short_name 清单成员

> name 是已安装 Web 应用的完整标签，short_name 是主屏幕等狭窄位置使用的标签；Chromium 要求至少具备其中之一才会提供安装。

清单成员 `name` 指定 Web 应用显示给用户的完整名称。主屏幕网格、启动器、任务栏和应用切换器等位置如果放不下全名，就用 `short_name`。规范把两个成员都列为可选，但 Chromium 要求至少有一个才会提供安装。

Chrome 39、Edge 79、Samsung Internet 4.0、Android 上的 Firefox 79、iOS 上的 Safari 11.3 与
macOS 上的 Safari 17 读取这两个成员（BCD `manifests.webapp.name`、`manifests.webapp.short_name`）。
桌面端 Firefox 157 不从清单安装 Web 应用，所以两者都不读。各语言版本放在 `name_localized`
与 `short_name_localized` 中，Chrome 与 Edge 148 会应用它们。

## 成员

| 属性 | 值 |
|---|---|
| 类型 | 两者都是字符串。纯文本；操作系统原样渲染或丢弃 HTML、emoji 和控制字符，不解释它们。 |
| 默认值 | `name` 没有默认值。`short_name` 缺省时，浏览器在原本使用 `short_name` 的地方使用 `name`，并按界面需要截断。 |
| 示例值 | `"name": "Acme Invoice Tracker"`、`"short_name": "Invoices"`。 |

Chromium 在安装对话框、启动画面、Windows 开始菜单、macOS 的"应用程序"文件夹和窗口标题中显示 `name`。Android 主屏幕图标下方、ChromeOS 任务栏以及固定到 Windows 任务栏的应用使用 `short_name`。Lighthouse 的 `manifest-short-name-length` 审计会判定
超过 12 个字符的 `short_name` 不合格，因为 Android 启动器会把它截断，这就是实际的长度上限。

应用处于 `standalone` 或 `fullscreen` 模式时，Chromium 在窗口切换界面中把 `short_name`（缺省时为 `name`）加到文档 `<title>` 之前，使页面无法把自己伪装成操作
系统或另一个应用（web.dev）。在 iOS 上，存在 `<meta name="apple-mobile-web-app-title">` 时，
它优先于 `short_name` 成为主屏幕标签。

浏览器会丢弃非字符串值，并记录 `property 'name' ignored, type string expected.`（`short_name` 同理）。这可能使清单失去可安装性所需的唯一名称字段。

:::caution
Chromium 的 [`installable_logging.cc`](https://chromium.googlesource.com/chromium/src/+/main/components/webapps/browser/installable/installable_logging.cc) 定义了可安装性错误 `Manifest does not contain a 'name' or 'short_name' field`。按标准清单条件，`name` 与 `short_name` 都不是非空字符串时，可安装性评估器会抛出它（见 [`installable_evaluator.cc`](https://chromium.googlesource.com/chromium/src/+/main/components/webapps/browser/installable/installable_evaluator.cc) 中的 `IsManifestNameValid`），所以任意一个保留非空值的清单不会触发。DevTools 在 **Installability** 一节用自己的措辞显示同一个错误："Manifest doesn’t contain a 'name' or 'short_name' field"（[`AppManifestView.ts`](https://github.com/ChromeDevTools/devtools-frontend/blob/main/front_end/panels/application/AppManifestView.ts)）。
:::

## 示例

选择符合启动器 12 字符限制的标签，并在运行时检查已部署的清单。

### 完整名称与放得进启动器的短标签

品牌名放在 `name`；`short_name` 是用户在 48 px 图标下方能认出的那个词。两者都别写"App"或
"PWA"，操作系统已经知道它是应用。

```json
{
  "name": "Acme Invoice Tracker",
  "short_name": "Invoices",
  "start_url": "/",
  "display": "standalone",
  "icons": [{ "src": "/icon-512.png", "sizes": "512x512", "type": "image/png" }]
}
```

`Invoices` 有 8 个字符，在 Android 启动器预算之内；`Acme Invoice Tracker` 则显示在安装对话框、
Windows 开始菜单与窗口标题中。

### 在 standalone 模式下保持窗口标题可读

由于 Chromium 会在 standalone 模式下把 `short_name` 加到窗口标题前面，重复应用名的页面标题会
显示成 `Invoices - Invoices - March`。把文档标题只设为内容本身，让浏览器自己加应用标签。

```js
function setWindowTitle(section) {
  const standalone = matchMedia('(display-mode: standalone)').matches;
  // 在 standalone 窗口里，Chromium 会自行加上清单的 short_name。
  document.title = standalone ? section : `${section} · Acme Invoice Tracker`;
}

setWindowTitle('March');
```

在浏览器标签页里，该分支把品牌加回来，书签与标签栏仍然能识别站点。

### 在部署测试里检查上线的标签

运行时没有任何东西能报告操作系统画出的标签，但测试可以断言已部署的清单带有 `name`、
`short_name` 在启动器预算之内、两者都不含标记。回退分支返回问题而不是抛出，这样测试输出会
点名具体字段。

```js
export async function checkLabels(manifestUrl = '/manifest.webmanifest') {
  const m = await fetch(manifestUrl).then((r) => r.json());
  const problems = [];
  if (typeof m.name !== 'string' || !m.name.trim()) problems.push('name missing');
  if (typeof m.short_name === 'string' && m.short_name.length > 12) problems.push('short_name over 12 chars');
  for (const key of ['name', 'short_name']) {
    if (typeof m[key] === 'string' && /<[a-z]/i.test(m[key])) problems.push(`${key} contains markup`);
  }
  return problems;
}
```

每次部署后对生产源跑一遍，与可安装性标准条目里描述的图标和启动 URL 检查放在一起。

## 另请参阅

- [lang、dir 与 *_localized 清单成员](/zh/reference/manifest/localization/)
- [icons 清单成员](/zh/reference/manifest/icons/)
- [可安装性标准：什么让 PWA 可安装](/zh/reference/installation/installability-criteria/)
- [iOS 添加到主屏幕：iOS 上的 PWA 安装](/zh/reference/installation/ios-add-to-home-screen/)
- [Web Application Manifest: name member](https://www.w3.org/TR/appmanifest/#name-member)（w3.org）
- [Add a web app manifest](https://web.dev/articles/add-manifest)（web.dev）