Skip to content

Instantly share code, notes, and snippets.

@dejurin
Created May 5, 2025 11:14
Show Gist options
  • Save dejurin/4d28c6ecf2eecb5793536234937ee938 to your computer and use it in GitHub Desktop.
Save dejurin/4d28c6ecf2eecb5793536234937ee938 to your computer and use it in GitHub Desktop.
Early Hints
// Early Hints
app.Use(func(c *fiber.Ctx) error {
if c.Method() != fiber.MethodGet ||
!strings.Contains(c.Get("Accept", ""), "text/html") {
return c.Next()
}
for _, entry := range Manifest {
switch ext := strings.ToLower(filepath.Ext(entry.File)); ext {
case ".js":
c.Append("Link", fmt.Sprintf("</%s>; rel=preload; as=script", entry.File))
case ".css":
c.Append("Link", fmt.Sprintf("</%s>; rel=preload; as=style", entry.File))
}
for _, css := range entry.Css {
c.Append("Link", fmt.Sprintf("</%s>; rel=preload; as=style", css))
}
}
reqCtx := c.Context()
if err := reqCtx.EarlyHints(); err != nil {
zap.L().Warn("Early Hints failed", zap.Error(err))
}
return c.Next()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment