@fastify/early-hints

中文文档
1.0.1发布于 2023/01/26MITTypeScriptCommonJS近 30 天下载 4.4万

Fastify Early Hints

此软件包已被标记为弃用

This package is no longer needed. Upgrade to fastify v5, which includes this functionality.

原始使用文档
文档目录8 节
  1. Install
  2. Options
  3. Usage
  4. Reply.writeEarlyHints
  5. Reply.writeEarlyHintsLinks
  6. Browser Limitation
  7. References
  8. License

@fastify/early-hints

js-standard-style Continuous Integration

Draft proposal of plugin handling the HTTP 103 code. Based on : https://github.com/fastify/fastify/issues/2683

Install

npm i @fastify/early-hints

Options

You can pass the following options during the plugin registration:

await fastify.register(import('@fastify/early-hints'), {
  warn: true // default: false
})
  • warn : indicates if the plugin should log warnings if invalid values are supplied as early hints

Usage

Reply.writeEarlyHints

This method is used to write early hints with any header you need. It accepts either object or Array of headers and return Promise.

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();

Result

$ 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"}

This method used to write only the Link header. It accepts an Array and return Promise.

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();

Result

$ 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"}

Browser Limitation

Currently (2022-09-29), only Chrome 103 is supporting 103 Early Hints and Chrome will ignore 103 Early Hints in the following situations.

  • Early Hints sent on subresource requests
  • Early Hints sent on iframe navigation
  • Early Hints sent on HTTP/1.1 or earlier
  • Second and following Early Hints

Read more on https://chromium.googlesource.com/chromium/src/+/master/docs/early-hints.md#103-early-hints

References

License

Licensed under MIT.

版本与发布记录

@fastify/early-hints 共发布 2 个版本,首次发布于 2022/11/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2023/01/263 年前
1.0.02022/11/034 年前

关于 @fastify/early-hints 的常见问题

@fastify/early-hints 是什么?

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。作者已将其标记为弃用,新项目建议寻找替代方案。