@fastify/early-hints

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

快捷早期提示

此软件包已被标记为弃用

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

中文使用文档
文档目录8 节
  1. 安装
  2. 选项
  3. 使用量
  4. 回复. write Early 提示
  5. 回复. write EarlyHintsLinks (英语)
  6. 浏览器限制
  7. 参考资料
  8. 许可证

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

js-standard-style Continuous Integration

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

安装

npm i @fastify/early-hints

选项

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

await fastify.register(import('@fastify/early-hints'), {
  warn: true // default: false
})
  • warn : 显示插件是否应该登录警告, 如果提供无效值作为早期提示

使用量

回复. write Early 提示

这种方法用于用需要的任何头来写出早期的提示. 它接受 无论是 object 或者说 Array 标题并返回 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();

结果

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

此方法只用于写入 Link 头曰. 它接受一个 Array 财务报告和审定财务报表 返回时 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();

结果

$ 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

参考资料

许可证

许可证根据 麻省理工学院。 。 。 .

版本与发布记录

@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 是什么?

快捷早期提示注意:该包已被作者标记为弃用——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。作者已将其标记为弃用,新项目建议寻找替代方案。