-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Added cache control utilities #45
Conversation
🤔 I think maybe |
@jsumners good point, I forgot about We could make a new plugin just for them, but I'm not sure it would be worth it. |
Why not making |
This module is mainly a toolbelt module, it does not alter the workings of Fastify. Both |
I would rename I'm happy to have these in here or in a new |
(I'm also ok keep them here, they are just decorators) |
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
The only concern in keeping those accessors here is that this plugin is not built to be reused by other plugins, because it adds a error handler. |
We could cut a major and disable the error handler by default, or advise better that if you use it in a nested plugin, it should be disabled. |
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.
I've updated the helpers and added a few utilities for common use cases:
.preventCache()
// tells to browsers and CDN to not cache anything.revalidate()
// tells to the browser that the content must be revalidated asap.staticCache(time)
// tells to the browser that the content is static and immutable, CDNs can cache it.
There are a .stale()
and .maxAge
helpers as well to help build a stale strategy.
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.
I agree to disable the error handler by default too
Co-authored-by: Matteo Collina <hello@matteocollina.com>
…nsible into suggest-cache
The more I think about this, the more I dislike having the caching stuff in this module. I agree that the functionality in As for this PR, I can easily see someone wanting to use the HTTP spec cache control features added by it but not wanting things like So I think we should really create some new module to handle the HTTP cache control spec. I already mentioned a possible name of |
I likely prefer the approach of a separate module as well. |
Oks, I'll work on a separate module in the upcoming days :) |
I've been thinking about this and I don't think having a separate module is worth it. I think this repository is still the best place to have them, as we already have the |
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
I agree. But that's why I suggested it be a module for the whole cache control spec.
It should be moved to the module that is specifically for the cache control spec.
This is a bit of a straw man. There was a clear line proposed: a module is specific to a domain instead of a grab bag of things. |
A module for http caching should include not only the header helpers we are shipping here,but at least etag handling as well (and probably request headers caching as well). |
I am not blocking this. |
then land? |
I am leaving that decision up to the team. |
I'll merge and release this as soon as I have time. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@delvedor could you fix this PR? |
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
} | ||
|
||
function preventCache () { | ||
this.header('Cache-Control', 'no-store, max-age=0, private') |
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.
It would be better to set pragma
and expires
headers to give compatibility support for HTTP/1.0? @delvedor
this
.header('Cache-Control', 'no-store, max-age=0, private')
.header('pragma', 'no-cache')
.header('expires', 0)
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.
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.
please open a new issue or propose a PR, this landed.
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.
@mcollina I just wanted to get your opinions in the first place.
Caching is very important for enabling a performant web. This pr adds a small utility to help users configure cache control headers.
Ther is a generic API for configure the header and two specialized apis, one for no caching and one for stale content. Is there something else we should have?
Useful reading:
cc @fastify/core
Checklist
npm run test
andnpm run benchmark
and the Code of conduct