Cloudflare Docs
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

103 Early Hints

Allow a client to request static assets while waiting for the HTML response.

To ensure Early Hints are enabled:

  1. Log in to the Cloudflare Dashboard and select your account and website.
  2. Go to Speed -> Optimization.
  3. Enable the Early Hints toggle to on.
const CSS = "body { color: red; }";
const HTML = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Early Hints test</title>
<link rel="stylesheet" href="/test.css">
</head>
<body>
<h1>Early Hints test page</h1>
</body>
</html>
`;
export default {
async fetch(req) {
// If request is for test.css, serve the raw CSS
if (/test\.css$/.test(req.url)) {
return new Response(CSS, {
headers: {
"content-type": "text/css",
},
});
} else {
// Serve raw HTML using Early Hints for the CSS file
return new Response(HTML, {
headers: {
"content-type": "text/html",
link: "</test.css>; rel=preload; as=style",
},
});
}
}
}
const CSS = "body { color: red; }";
const HTML = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Early Hints test</title>
<link rel="stylesheet" href="/test.css">
</head>
<body>
<h1>Early Hints test page</h1>
</body>
</html>
`;
const handler: ExportedHandler = {
async fetch(req) {
// If request is for test.css, serve the raw CSS
if (/test\.css$/.test(req.url)) {
return new Response(CSS, {
headers: {
"content-type": "text/css",
},
});
} else {
// Serve raw HTML using Early Hints for the CSS file
return new Response(HTML, {
headers: {
"content-type": "text/html",
link: "</test.css>; rel=preload; as=style",
},
});
}
}
};
export default handler;