-
Notifications
You must be signed in to change notification settings - Fork 1
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
Cache control #71
Comments
One idea would be to implement this as a handler: new CacheControl('public, max-age=31557600', function($req, $res) {
// ...
}) |
See https://ktor.io/docs/caching.html: install(CachingHeaders) {
options { outgoingContent ->
when (outgoingContent.contentType?.withoutParameters()) {
ContentType.Text.CSS -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 3600))
else -> null
}
}
} |
Take-aways from https://csswizardry.com/2019/03/cache-control-for-civilians/: AssetsFor immutable files including a fingerprint or version, e.g. style.cb381a32b229.css, we should use For other static assets, PagesFor pages with very sensitive data, e.g. login forms, For dynamic pages, most commonly For dynamic pages which are unfrequently updated (say, a blog), we can cache a little bit more using |
Idea for a caching DSL instead of simply passing seconds (because of its general use, the // Cache-Control: no-cache
new AssetsFrom(..., CacheControl::noCache())
// Cache-Control: max-age=31536000, immutable
new AssetsFrom(..., CacheControl::maxAge(seconds: 31536000)->immutable())
// Cache-Control: max-age=2419200, must-revalidate
new AssetsFrom(..., CacheControl::maxAge(seconds: 2419200)->mustRevalidate()) ...and for fingerprinting using PHP 8 new AssetsFrom(..., fn($path, $mime) => match (true) {
preg_match('/[a-z]+\.[a-f0-9]+\.[a-z]+$/', $path) => CacheControl::maxAge(seconds: 31536000)->immutable(),
default => CacheControl::maxAge(seconds: 2419200)->mustRevalidate()
}); ...or using an array: new AssetsFrom(..., [
'/^[a-z]+\.[a-f0-9]+\.[a-z]+/' => CacheControl::maxAge(seconds: 31536000)->immutable(),
null => CacheControl::maxAge(seconds: 2419200)->mustRevalidate()
]); |
Alternative use as filter: // Cache-Control: no-cache
new Filters([CacheControl::noCache()], new AssetsFrom(...))
// Cache-Control: max-age=31536000, immutable
new Filters([CacheControl::maxAge(seconds: 31536000)->immutable()], new AssetsFrom(...))
// Cache-Control: max-age=2419200, must-revalidate
new Filters([CacheControl::maxAge(seconds: 2419200)->mustRevalidate()], new AssetsFrom(...)) We cannot attach headers inside a filter after the invocation because the content may have already been sent. The following doesn't work: class CacheControl implements Filter {
// ...shortened for brevity
public function filter($request, $response, $invocation) {
$result= $invocation->proceed($request, $response);
if (200 === $response->status()) {
$response->header('Cache-Control', $this->header());
}
return $result;
}
} So, we need to rewrite this to the following: class CacheControl implements Filter {
// ...shortened for brevity
public function filter($request, $response, $invocation) {
$response->header('Cache-Control', $this->header());
return $invocation->proceed($request, $response);
}
} However, this would attach to all responses and not just There are two possibilities here:
|
PR #74 is released in 2.9.0 |
http-cache verifies that the page and all its resources follow a good, sustainable caching strategy.
The right caching strategy can help improve site performance through:
See https://webhint.io/docs/user-guide/hints/hint-http-cache/
The text was updated successfully, but these errors were encountered: