diff --git a/README.md b/README.md index 1964c18..59b7a51 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ curl https://myserver.dev/user?force=true # MISS (forcing invalidation) ##### cache -Type: `boolean`
+Type: `boolean`
Default: `new Keyv({ namespace: 'ssr' })` The cache instance used for backed your pre-calculated server side response copies. @@ -133,7 +133,7 @@ If you don't specify it, a memory cache will be used. ##### ttl -Type: `number`
+Type: `number`
Default: `7200000` Number of milliseconds a cache response is considered valid. @@ -146,21 +146,21 @@ If you don't provide one, this be used as fallback for avoid keep things into ca ##### serialize -Type: `function`
+Type: `function`
Default: `JSON.stringify` Set the serializer method to be used before compress. ##### deserialize -Type: `function`
+Type: `function`
Default: `JSON.parse` Set the deserialize method to be used after decompress. ##### compress -Type: `boolean`
+Type: `boolean`
Default: `false` Enable compress/decompress data using brotli compression format. @@ -173,7 +173,7 @@ npm install iltorb ##### revalidate -Type: `function`|`number`
+Type: `function`|`number`
Default: `ttl => ttl / 24` Number of milliseconds that indicates grace period after response cache expiration for refreshing it in the background. the latency of the refresh is hidden from the user. @@ -182,10 +182,17 @@ You can provide a function, it will receive [`ttl`](#ttl) as first parameter or The value will be associated with [`stale-while-revalidate`](https://www.mnot.net/blog/2014/06/01/chrome_and_stale-while-revalidate) directive. +##### getKey + +Type: `function`
+Default: `req => normalizeUrl(req.url)` + +It determinates how the cache key should be computed using `req` as input. + ##### get -_Required_
-Type: `function`
+_Required_
+Type: `function`
The method to be called for creating a fresh cacheable response associated with the current route path. @@ -207,8 +214,8 @@ Any other property can be specified and will passed to `.send`. ##### send -_Required_
-Type: `function`
+_Required_
+Type: `function`
The method used to determinate how the content should be rendered. @@ -289,7 +296,7 @@ You can have a better overview of the percentage of success by looking your Clou ## License -**cacheable-response** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/cacheable-response/blob/master/LICENSE.md) License.
+**cacheable-response** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/cacheable-response/blob/master/LICENSE.md) License.
Authored and maintained by Kiko Beats with help from [contributors](https://github.com/Kikobeats/cacheable-response/contributors). > [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats) diff --git a/index.js b/index.js index d82840b..2f6020e 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,8 @@ const getEtag = require('etag') const { URL } = require('url') const Keyv = require('keyv') -const getKey = url => { +const _getKey = req => { + const url = urlResolve('http://localhost', req.url) const { origin } = new URL(url) const baseKey = normalizeUrl(url, { removeQueryParameters: [/^utm_\w+/i, 'force', 'filter', 'ref'] @@ -43,6 +44,7 @@ const createSetHeaders = ({ revalidate }) => { module.exports = ({ cache = new Keyv({ namespace: 'ssr' }), compress: enableCompression = false, + getKey = _getKey, get, send, revalidate = ttl => ttl / 24, @@ -65,7 +67,7 @@ module.exports = ({ const hasForce = Boolean( req.query ? req.query.force : parse(req.url.split('?')[1]).force ) - const key = getKey(urlResolve('http://localhost', req.url)) + const key = getKey(req) const cachedResult = await decompress(await cache.get(key)) const isHit = !hasForce && cachedResult !== undefined @@ -98,4 +100,4 @@ module.exports = ({ } } -module.exports.getKey = getKey +module.exports.getKey = _getKey