souin icon indicating copy to clipboard operation
souin copied to clipboard

Provide a way to cache early hints

Open punkpeye opened this issue 7 months ago • 4 comments

Not sure if this is even within the scope of this library, but what would be nice is a way to cache early hints.

For context, I am discovering that caching whole responses is a lot more complicated than I anticipated (mostly issues with things like accidentally caching set-cookie and such)

However, a quick win that should be safe to have enabled for most URLs is to simply cache early hints.

punkpeye avatar May 23 '25 21:05 punkpeye

If you can add the HTTP response Cache-Control header with the value Set-Cookie, it won't store the cookie.

e.g. With the Caddyfile

{
	admin localhost:2999
	http_port     9080
	cache {
		ttl 5s
	}
}
localhost:9080 {
	route /cache-set-cookie {
		cache
		reverse_proxy localhost:9087 {
			header_down +Cache-Control no-cache=Set-Cookie
		}
	}
}

darkweak avatar May 25 '25 09:05 darkweak

This might be a response to a different thread.

punkpeye avatar May 25 '25 11:05 punkpeye

You're right, I opened your issue in another tab, sorry. It can cache the early hints as-is but it will probably wait for the whole content and override the stored response with the final HTTP code and the full response body. What we could add is a key that match the following pattern {generated_key}_status103_{iteration} and serve all iterations as 103 early hint responses then serve the matched response in cache after.

darkweak avatar May 25 '25 13:05 darkweak

What we could add is a key that match the following pattern {generated_key}status103{iteration} and serve all iterations as 103 early hint responses then serve the matched response in cache after.

That would be an awesome feature.

I currently built a hacky solution using Fastify, but it would be nice to have as part of the Caddy cache layer.

punkpeye avatar May 25 '25 14:05 punkpeye