Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for informational headers #47

Merged
merged 7 commits into from
Nov 12, 2024

Conversation

tigerwill90
Copy link
Owner

@tigerwill90 tigerwill90 commented Oct 29, 2024

This PR introduces support for informational headers (such as 103 Early Hints), by allowing preliminary headers to be sent before the final response. This feature enables applications to send hints to clients (e.g., preload assets) that improve page load performance and responsiveness. This feature is supported in the http standard library but was not handle correctly by Fox.

Example

package main

import (
	"net/http"
        "github.com/tigerwill90/fox"
)

func main() {
	f := fox.New()

	f.MustHandle(http.MethodGet, "/foo", func(c Context) {
		// Send 103 Early Hints with a Link header to prompt the client to preload the CSS file
		c.SetHeader("Link", "</style.css>; rel=preload; as=style")
		c.Writer().WriteHeader(http.StatusEarlyHints)

		// Send the final HTML response after the early hint
		c.SetHeader("Content-Type", "text/html; charset=utf-8")
		c.Writer().WriteHeader(http.StatusOK)
		c.Writer().WriteString(`
			<!DOCTYPE html>
			<html lang="en">
			<head>
				<meta charset="UTF-8">
				<meta name="viewport" content="width=device-width, initial-scale=1.0">
				<link rel="stylesheet" href="/style.css">
				<title>Early Hints Example</title>
			</head>
			<body>
				<h1>Welcome to the Early Hints Example Page</h1>
				<p>This page preloads the CSS stylesheet using the 103 Early Hints feature.</p>
			</body>
			</html>
		`)
	})

	http.ListenAndServe(":8080", f)
}

Copy link

codecov bot commented Oct 29, 2024

Codecov Report

Attention: Patch coverage is 79.48718% with 8 lines in your changes missing coverage. Please review.

Project coverage is 90.86%. Comparing base (c8befa2) to head (13f8446).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
response_writer.go 79.48% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #47      +/-   ##
==========================================
+ Coverage   90.46%   90.86%   +0.39%     
==========================================
  Files          18       18              
  Lines        2832     2779      -53     
==========================================
- Hits         2562     2525      -37     
+ Misses        210      194      -16     
  Partials       60       60              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tigerwill90 tigerwill90 changed the title feat: feat add support for information http code Add support for informational headers Oct 29, 2024
@tigerwill90 tigerwill90 marked this pull request as ready for review November 11, 2024 20:04
@tigerwill90 tigerwill90 self-assigned this Nov 11, 2024
pawndev
pawndev previously approved these changes Nov 11, 2024
Copy link
Collaborator

@pawndev pawndev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn ! So much deletion 😂 but seems good !

@tigerwill90 tigerwill90 merged commit 161ea5b into master Nov 12, 2024
6 of 8 checks passed
@tigerwill90 tigerwill90 deleted the feat/support-informational-header branch November 12, 2024 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants