Early Hints (HTTP 103)
This recipe demonstrates how to send HTTP 103 Early Hints in Fiber v3 using c.SendEarlyHints().
Early Hints (RFC 8297) allows the server to send preliminary response headers containing Link preload hints before the full response body is generated. Browsers supporting Early Hints can start fetching critical stylesheets, scripts, and fonts in parallel while the server executes database queries or business logic.
Prerequisites
Ensure you have the following installed:
- Go 1.26.0 or newer
- Fiber v3 package (
github.com/gofiber/fiber/v3)
Note: Fiber sends the
HTTP 103 Early Hintsresponse on HTTP/1.1 requests. However, most modern web browsers only process Early Hints when served over HTTP/2 or HTTP/3. In production, the preloading benefit typically requires a reverse proxy or CDN that terminates HTTP/2 or HTTP/3 to the browser and forwards the upstream 103 status. Check that yours does before relying on it. For local testing, runningcurl -v http://127.0.0.1:3000/demonstrates the interim 103 response followed by the final 200 OK. Older or non-browser HTTP/1.1 clients that do not support interim 1xx responses may fail to process the stream correctly.
Setup
-
Navigate to the recipe directory:
cd recipes/early-hints -
Download and verify dependencies:
go mod tidy
Running the Application
-
In your terminal, start the Fiber server:
go run main.go -
In a separate terminal, test the endpoints using
curl:# Request root pagecurl -i http://127.0.0.1:3000/# Check health endpointcurl -i http://127.0.0.1:3000/healthz
Example Code
package main
import (
"log"
"time"
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
// Assets for the client to preload
hints := []string{
"</css/main.css>; rel=preload; as=style",
"</js/bundle.js>; rel=preload; as=script",
}
// Dispatch HTTP 103 Early Hints
if err := c.SendEarlyHints(hints); err != nil {
log.Printf("SendEarlyHints warning: %v", err)
}
// Simulate server work
time.Sleep(50 * time.Millisecond)
c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8)
return c.SendString("<!DOCTYPE html><html>...</html>")
})
log.Fatal(app.Listen(":3000"))
}