-
-
Notifications
You must be signed in to change notification settings - Fork 670
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
feat(middleware/cache): add cacheableStatusCodes
option
#3943
base: main
Are you sure you want to change the base?
feat(middleware/cache): add cacheableStatusCodes
option
#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.29% -0.04%
==========================================
Files 168 168
Lines 10688 10775 +87
Branches 3070 3105 +35
==========================================
+ Hits 9761 9837 +76
- Misses 926 937 +11
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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. |
Hi, @usualoma. Thanks for checking and pointing it out.
My Apologies, I misunderstood what they meant.
After reading your comments, I am beginning to feel that it is not reasonable to exclude DELETE, OPTIONS, etc. from the cache.
I corrected to use |
Co-authored-by: Taku Amano <[email protected]>
@usualoma Given this, how about allowing users to configure the status code and method at the userland? |
Hi, @miyamo2. I appreciate your consideration. methodAs mentioned above, hono can specify the method to apply middleware in the framework semantics. ( statusRegarding the status code, currently, For example, 206 is cacheable, as you know from the HTTP semantics, but to cache it, we must include the Range request header in the cache key, etc., and Hono's cache middleware does not support this by default. From the perspective of preventing accidents (or certain types of vulnerability) caused by careless caching, the default should be 200 only. In addition, as you say, it might be a good idea to allow users to specify the status code to be cached explicitly. |
Hi @miyamo2 Thank you for the PR. @usualoma Thank you for the comments. Regarding handling the method, I also think the middleware should not specify the method, but for example, you can use Regarding the status code, it's a good idea not to use 200 - 299 defined in Either way, making the default status code not 200 - 299 will cause a breaking change. We can't merge this immediately. |
Hi @yusukebe, @usualoma. I modified the default cached response to only
Sure. In my correspondence with @usualoma , I understood that this PR should have been kept as a proposal, not a fix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Co-authored-by: Taku Amano <[email protected]>
cacheableStatusCodes
option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Hey @miyamo2 Looks good! I fixed the title to "add I appreciate your great work! |
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