-
Notifications
You must be signed in to change notification settings - Fork 3
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
v2.6.0 Add cache control string to the router #101
base: master
Are you sure you want to change the base?
Conversation
@@ -70,6 +70,7 @@ const routerInstance = router(server, { | |||
appEnv: process.ENV.APP_ENV, // optional, used to specify the env in Sentry, defaults to "unknown" | |||
logger: logger, // pass in the luxuryescapes logger and the error handler will use this, resulting in single line log messages in new relic with stack traces. | |||
sanitizeKeys: [/_token$/, /password/i, "someHideousKey", "path.with.dot"], // array of keys or paths to sanitize from the request body, query and params on error | |||
cacheControlString: 'public, max-age=600', // you can pass cache directives in a ',' separated string string. Reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#cache_directives |
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.
Can you add a test for this? I think theres a test file with some examples. Or I can add one for you.
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 will add the test later todat
* Cache-Control header value | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control | ||
*/ | ||
cacheControlString?: string; |
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.
Would it be better to abstract away the syntax and make it easier to use, e.g.
cacheControlString?: string; | |
cacheControl?: { | |
type: 'public', // More options can be added when required | |
TTLInSeconds: number, | |
} |
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 don't think we should, this would limit it too much, the spec is very wide and has many options that can be combined in different ways.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
return function responseCacheControl (req, res, next) { | ||
const originalFunc = res.json | ||
res.json = (responseBody) => { | ||
res.set('Cache-Control', cacheControlString) |
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 wonder if we should do something to check the res.status?
e.g. only set it if it's 2xx
This pull request introduces a new feature to support cache control headers in responses and updates.
Cache Control Feature:
README.md
: AddedcacheControlString
option to the router configuration to allow passing cache directives.lib/middleware/cache-control.js
: Implemented new middleware to set theCache-Control
header in responses.lib/router.js
:responseCacheControl
middleware.cacheControlString
to the handler configuration and included the middleware in the handler stack ifcacheControlString
is provided.Version Update:
package.json
: Updated the package version from2.5.32
to2.6.0
.Usage
before
after