# @fastify/early-hints 中文文档

规范地址：https://npmcn.com/package/%40fastify/early-hints/

版本：1\.0\.1
npm 元数据更新时间：2026-08-02T10:02:08.380Z
中文译文更新时间：2026-11-14T11:24:50.099Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fastify/early-hints
原文对照：https://npmcn.com/package/%40fastify/early-hints/?lang=original
项目主页：https://github.com/fastify/fastify-early-hints#readme
源码仓库：https://github.com/fastify/fastify-early-hints

弃用说明：This package is no longer needed\. Upgrade to fastify v5, which includes this functionality\.

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：43.2 kB，21 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2022-11-03T11:04:42.314Z
- 最新版本发布：2023-01-25T19:45:37.288Z
- 近 30 天下载量：43727（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

快捷早期提示

## 安装

```sh
npm install @fastify/early-hints
```

也可以使用：`pnpm add @fastify/early-hints`、`yarn add @fastify/early-hints`、`bun add @fastify/early-hints`

## README

# @ fastify/ early- shits (中文( 台灣 ) )

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![Continuous Integration](https://github.com/fastify/fastify-early-hints/workflows/CI%20workflow/badge.svg)

关于处理HTTP 103代码的插件的建议草案. 依据: [https://github.com/fastify/fastify/issues/2683 (中文(简体) )](https://github.com/fastify/fastify/issues/2683)

## 安装

```shell
npm i @fastify/early-hints
```

## 选项

在插件注册期间, 您可以通过以下选项 :

```js
await fastify.register(import('@fastify/early-hints'), {
  warn: true // default: false
})
```

-   `warn` : 显示插件是否应该登录警告, 如果提供无效值作为早期提示

## 使用量

### 回复. write Early 提示

这种方法用于用需要的任何头来写出早期的提示. 它接受 无论是 `object` 或者说 `Array` 标题并返回 `Promise`。 。 。 .

```javascript
const Fastify = require("fastify");
const eh = require("@fastify/early-hints");

const fastify = Fastify({ logger: true });
fastify.register(eh);

fastify.get("/", async (request, reply) => {
  // object
  await reply.writeEarlyHints({
    'Content-Security-Policy': 'style-src: self;',
    Link: ['</style.css>; rel=preload; as=style', '</script.js>; rel=preload; as=script']
  })
  // array
  await reply.writeEarlyHints([
    { name: 'Content-Security-Policy', value: 'style-src: self;' },
    { name: 'Link', value: '</style.css>; rel=preload; as=style' },
    { name: 'Link', value: '</script.js>; rel=preload; as=script' },
  ])
  return { hello: "world" };
});

const start = async () => {
  try {
    await fastify.listen({ port: 3000 });
    fastify.log.info(`server listening on ${fastify.server.address().port}`);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();
```

结果

```shell
$ curl -D - http://localhost:3000    
HTTP/1.1 103 Early Hints
Content-Security-Policy: style-src: self;
Link: </style.css>; rel=preload; as=style
Link: </script.js>; rel=preload; as=script

HTTP/1.1 103 Early Hints
Content-Security-Policy: style-src: self;
Link: </style.css>; rel=preload; as=style
Link: </script.js>; rel=preload; as=script

HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
content-length: 17
Date: Thu, 12 Nov 2020 22:45:54 GMT
Connection: keep-alive

{"hello":"world"}
```

### 回复. write EarlyHintsLinks (英语)

此方法只用于写入 `Link` 头曰. 它接受一个 `Array` 财务报告和审定财务报表 返回时 `Promise`。 。 。 .

```javascript
const Fastify = require("fastify");
const eh = require("@fastify/early-hints");

const fastify = Fastify({ logger: true });
fastify.register(eh);

fastify.get("/", async (request, reply) => {
  await reply.writeEarlyHintsLinks([
    "Link: </style.css>; rel=preload; as=style",
    "Link: </script.js>; rel=preload; as=script",
  ])
  await reply.writeEarlyHintsLinks([
    { href: "//example.com", rel: "preload", as: "style" },
    { href: "//example.com", rel: "preload", as: "style", cors: true },
    { href: "//example.com", rel: "preconnect" },
    { href: "//example2.com", rel: "preconnect", cors: true },
    { href: "//example3.com", rel: "preconnect", cors: "use-credentials" },
  ])
  return { hello: "world" };
});

const start = async () => {
  try {
    await fastify.listen({ port: 3000 });
    fastify.log.info(`server listening on ${fastify.server.address().port}`);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};
start();
```

结果

```shell
$ curl -D - http://localhost:3000    
HTTP/1.1 103 Early Hints
Link: </style.css>; rel=preload; as=style
Link: </script.js>; rel=preload; as=script

HTTP/1.1 103 Early Hints
Link: </style.css>; rel=preload; as=style
Link: </script.js>; rel=preload; as=script
Link: <//example.com>; rel=preload; as=style
Link: <//example.com>; rel=preload; as=style; crossorigin
Link: <//example.com>; rel=preconnect
Link: <//example2.com>; rel=preconnect; crossorigin
Link: <//example3.com>; rel=preconnect; crossorigin=use-credentials

HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
content-length: 17
Date: Thu, 12 Nov 2020 22:45:54 GMT
Connection: keep-alive

{"hello":"world"}
```

## 浏览器限制

目前(2022-09-29),仅Chrome 103正在支持 `103 Early Hints` 财务报告和审定财务报表 克罗姆会忽略的 `103 Early Hints` 在下列情况下:.

-   根据子资源请求发出的早期提示
-   Iframe 导航时发送的早期提示
-   在 HTTP/1.1 上或更早发送的早期提示
-   第二次和随后的早期提示

更多信息 [https://chromium.googlesource.com/chromium/src/+/master/docs/ear-hints.md#103-ear-hints](https://chromium.googlesource.com/chromium/src/+/master/docs/early-hints.md#103-early-hints)

## 参考资料

-   [https://httpwg.org/specs/rfc8297.html (中文(简体) )](https://httpwg.org/specs/rfc8297.html)
-   [https://www.w3.org/TR/resource-hints/ (中文(简体) )](https://www.w3.org/TR/resource-hints/)

## 许可证

许可证根据 [麻省理工学院](https://npmcn.com/package/%40fastify/early-hints/LICENSE)。 。 。 .

## 常见问题

### @fastify/early-hints 是什么？

快捷早期提示注意：该包已被作者标记为弃用——This package is no longer needed\. Upgrade to fastify v5, which includes this functionality\.

### 如何安装 @fastify/early-hints？

在项目目录运行 npm install @fastify/early-hints，或使用 pnpm add / yarn add / bun add。

### @fastify/early-hints 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @fastify/early-hints 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @fastify/early-hints 可以商用吗？

@fastify/early-hints 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @fastify/early-hints 的下载量有多少？

根据 npm 官方下载统计，@fastify/early-hints 在 2026/10/15 – 2026/11/13 这 30 天内被下载 43,727 次，日均约 1,458 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @fastify/early-hints 还在维护吗？

最新版本 1\.0\.1 发布于 2023/01/26（3 年前）；至今共发布 2 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/11/03。作者已将其标记为弃用，新项目建议寻找替代方案。

