pub async fn send_early_hints(
req: &mut Request<impl Body>,
headers: HeaderMap,
) -> Result<(), Error>Expand description
Sends a 103 Early Hints response for the given request.
Early hints allow the server to push Link (or other) headers to the
client before the final response is ready, enabling the browser to begin
preloading resources (stylesheets, scripts, fonts, etc.) while the handler
is still computing the response.
§Requirements
The connection must be HTTP/2 or HTTP/3, or if HTTP/1.1, the server must
have been created with
Http1Options::enable_early_hints
set to true. When that option is enabled the handler inserts an
EarlyHints token into each request’s extensions. If the token is absent
(i.e. early hints were not enabled) this function returns an error.
§Errors
Returns Err if:
- Early hints are not enabled on the connection (
"early hints not supported"). - The connection handler has already shut down and can no longer accept interim responses.
- Writing the
103response to the underlying I/O stream fails.
§Example
ⓘ
use http::HeaderMap;
let mut link_headers = HeaderMap::new();
link_headers.insert(
http::header::LINK,
"</style.css>; rel=preload; as=style".parse().unwrap(),
);
send_early_hints(&mut request, link_headers).await?;