Skip to content

Commit 3603e4d

Browse files
committed
feat: expose .getKey method
closes #11 closes #22
1 parent 1c7fd38 commit 3603e4d

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

Diff for: README.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ curl https://myserver.dev/user?force=true # MISS (forcing invalidation)
122122

123123
##### cache
124124

125-
Type: `boolean`<br>
125+
Type: `boolean`<br/>
126126
Default: `new Keyv({ namespace: 'ssr' })`
127127

128128
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.
133133

134134
##### ttl
135135

136-
Type: `number`<br>
136+
Type: `number`<br/>
137137
Default: `7200000`
138138

139139
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
146146

147147
##### serialize
148148

149-
Type: `function`<br>
149+
Type: `function`<br/>
150150
Default: `JSON.stringify`
151151

152152
Set the serializer method to be used before compress.
153153

154154
##### deserialize
155155

156-
Type: `function`<br>
156+
Type: `function`<br/>
157157
Default: `JSON.parse`
158158

159159
Set the deserialize method to be used after decompress.
160160

161161
##### compress
162162

163-
Type: `boolean`<br>
163+
Type: `boolean`<br/>
164164
Default: `false`
165165

166166
Enable compress/decompress data using brotli compression format.
@@ -173,7 +173,7 @@ npm install iltorb
173173

174174
##### revalidate
175175

176-
Type: `function`|`number`<br>
176+
Type: `function`|`number`<br/>
177177
Default: `ttl => ttl / 24`
178178

179179
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
182182

183183
The value will be associated with [`stale-while-revalidate`](https://www.mnot.net/blog/2014/06/01/chrome_and_stale-while-revalidate) directive.
184184

185+
##### getKey
186+
187+
Type: `function`<br/>
188+
Default: `req => normalizeUrl(req.url)`
189+
190+
It determinates how the cache key should be computed using `req` as input.
191+
185192
##### get
186193

187-
_Required_<br>
188-
Type: `function`<br>
194+
_Required_<br/>
195+
Type: `function`<br/>
189196

190197
The method to be called for creating a fresh cacheable response associated with the current route path.
191198

@@ -207,8 +214,8 @@ Any other property can be specified and will passed to `.send`.
207214

208215
##### send
209216

210-
_Required_<br>
211-
Type: `function`<br>
217+
_Required_<br/>
218+
Type: `function`<br/>
212219

213220
The method used to determinate how the content should be rendered.
214221

@@ -289,7 +296,7 @@ You can have a better overview of the percentage of success by looking your Clou
289296

290297
## License
291298

292-
**cacheable-response** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/cacheable-response/blob/master/LICENSE.md) License.<br>
299+
**cacheable-response** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/cacheable-response/blob/master/LICENSE.md) License.<br/>
293300
Authored and maintained by Kiko Beats with help from [contributors](https://github.com/Kikobeats/cacheable-response/contributors).
294301

295302
> [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)

Diff for: index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const getEtag = require('etag')
1010
const { URL } = require('url')
1111
const Keyv = require('keyv')
1212

13-
const getKey = url => {
13+
const _getKey = req => {
14+
const url = urlResolve('http://localhost', req.url)
1415
const { origin } = new URL(url)
1516
const baseKey = normalizeUrl(url, {
1617
removeQueryParameters: [/^utm_\w+/i, 'force', 'filter', 'ref']
@@ -43,6 +44,7 @@ const createSetHeaders = ({ revalidate }) => {
4344
module.exports = ({
4445
cache = new Keyv({ namespace: 'ssr' }),
4546
compress: enableCompression = false,
47+
getKey = _getKey,
4648
get,
4749
send,
4850
revalidate = ttl => ttl / 24,
@@ -65,7 +67,7 @@ module.exports = ({
6567
const hasForce = Boolean(
6668
req.query ? req.query.force : parse(req.url.split('?')[1]).force
6769
)
68-
const key = getKey(urlResolve('http://localhost', req.url))
70+
const key = getKey(req)
6971
const cachedResult = await decompress(await cache.get(key))
7072
const isHit = !hasForce && cachedResult !== undefined
7173

@@ -98,4 +100,4 @@ module.exports = ({
98100
}
99101
}
100102

101-
module.exports.getKey = getKey
103+
module.exports.getKey = _getKey

0 commit comments

Comments
 (0)