-
-
Notifications
You must be signed in to change notification settings - Fork 660
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
fix(middleware/cache): avoid caching according to RFC7231 #3943
base: main
Are you sure you want to change the base?
fix(middleware/cache): avoid caching according to RFC7231 #3943
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3943 +/- ##
=======================================
Coverage 91.32% 91.33%
=======================================
Files 168 168
Lines 10688 10698 +10
Branches 3070 3077 +7
=======================================
+ Hits 9761 9771 +10
Misses 926 926
Partials 1 1 ☔ View full report in Codecov by Sentry. |
Hi, @miyamo2. Thank you for creating PR! cacheable status404When we apply this change, the cache middleware will cache the 404 status code by default. I think it depends on the application whether or not you want to cache 404. I think it's a good idea to refer to the RFC, but the RFC talks about ‘whether or not it is cacheable?’ and not ‘whether or not it should be cached?’ , so I think it's up to the ‘application (or framework)’ to decide whether or not middleware should cache 404s. In my experience, when caching 404s, you often want to set a shorter TTL than for other status codes. Therefore, if 404s are to be cached, I think a system that allows you to set a TTL for each status code is necessary. If such a system does not exist, a simple system that only caches 2xx is fine, as it is now. 201, 202, 205, etc201 and 202 are currently cached, and I think it would be more appropriate not to cache them. (I don't think Hono users would apply cache middleware to APIs that return 201 or 202, though. cacheable methodI think that for almost all apps, we only want to cache GET and HEAD requests, so if we make this change, I think the default should be only GET and HEAD. (POST and PATCH should be excluded.) If we do that, I think it will be easier to specify using Depending on the application, you might want to cache DELETE method like this (this is a deviation from the standard, but in some cases it is a reasonable design to deviate from the standard depending on the application's requirements). In such cases, the existing code will no longer work, but in that case I think it would be fine to specify the method explicitly. app.delete(
'/api/*',
cache()
) |
That said, I don't think there are any security issues with the current implementation. I don't think there is anything particularly problematic about the DELETE method also being cached, if you think of it as ‘middleware for a low-level cache layer’. Therefore, although it is a very thought-provoking pull request, I think it is also possible to leave the current simple implementation as it is. (It might be a good idea to exclude 201, 202, 205, etc.) |
In terms of implementation, we should use the combination of |
I'd also like to hear the opinions of other committers. |
This pull request updates the cache middleware to avoid caching when it is defined as uncacheable in RFC 7231.
Caching will no longer occur under the following conditions.
Conditions for Avoiding Caching
1. Status codes defined as uncacheable by default
Additionally, It introducing an optional argument to allow caching of arbitrary status codes.
2. Request methods defined as uncacheable
Only GET, HEAD, POST, and PATCH will be cached, otherwise it is no longer cached.
https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.3
https://datatracker.ietf.org/doc/html/rfc5789#section-2
See also
Thank you.
The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code