Created
May 5, 2025 11:14
-
-
Save dejurin/4d28c6ecf2eecb5793536234937ee938 to your computer and use it in GitHub Desktop.
Early Hints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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