Early Hints 允许浏览器在源服务器编译完整响应的同时开始加载资源。这可提升最终用户的网页加载速度。作为 SaaS 提供商,你可能希望为部分自定义主机名优先考虑速度。使用自定义元数据,你可以按自定义主机名启用 Early Hints。
在为 SaaS 使用 Early Hints 之前,你需要创建自定义主机名。如果尚未完成,请参阅 Cloudflare for SaaS 快速入门。
-
查找你的 zone ID,可在 Cloudflare 仪表板中获取。
-
在 API Tokens(API 令牌) ↗ 页面的 Global API Key(全局 API 密钥) 下查找你的身份验证密钥。
-
如果要创建新的自定义主机名,请发出类似下方示例的 API 调用,并指定
"early_hints": "on":
Required API token permissions
At least one of the following token permissions is required:SSL and Certificates Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"hostname": "<CUSTOM_HOSTNAME>",
"ssl": {
"method": "http",
"type": "dv",
"settings": {
"http2": "on",
"min_tls_version": "1.2",
"tls_1_3": "on",
"early_hints": "on"
},
"bundle_method": "ubiquitous",
"wildcard": false
}
}'- 对于现有自定义主机名,通过
GET调用查找该主机名的id:
Required API token permissions
At least one of the following token permissions is required:SSL and Certificates WriteSSL and Certificates Read
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames?hostname=%7Bhostname%7D" \
--request GET \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"- 然后发出类似下方示例的 API 调用,并指定
"early_hints": "on":
Required API token permissions
At least one of the following token permissions is required:SSL and Certificates Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/custom_hostnames/$CUSTOM_HOSTNAME_ID" \
--request PATCH \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"ssl": {
"method": "http",
"type": "dv",
"settings": {
"http2": "on",
"min_tls_version": "1.2",
"tls_1_3": "on",
"early_hints": "on"
}
}
}'目前,settings 中的所有选项都是必需的,以防止这些选项被重置为默认值。你可以在更新 Early Hints 之前,利用返回主机名 id 的输出来获取当前设置状态。