Skip to content
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

allow forcing refresh of cache using x-api-cache-force-fetch #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ import apicache from 'apicache-plus'
apicache.options({ debug: true })
```

## Client-Side Headers
Passing header `x-apicache-force-refresh` will not only bypass the cache but it will refresh the cache with the newly pulled data. Whereas, `x-apicache-bypass` (with option `isBypassable`) will skip the cache to return fresh data set but doesn't change what was cached.

## Official framework support

- Express
Expand Down
7 changes: 5 additions & 2 deletions src/apicache.js
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,7 @@ function ApiCache() {
if (
opt.isBypassable &&
(req.headers['cache-control'] === 'no-store' ||
['1', 'true'].indexOf(req.headers['x-apicache-bypass']) !== -1 ||
['1', 'true'].indexOf(req.headers['x-apicache-force-fetch']) !== -1)
['1', 'true'].indexOf(req.headers['x-apicache-bypass']) !== -1)
) {
return bypass()
}
Expand Down Expand Up @@ -1356,6 +1355,10 @@ function ApiCache() {
})
}

if (['1', 'true'].indexOf(req.headers['x-apicache-force-fetch']) !== -1) {
debug('force fetch detected, refreshing cache.')
return maybeMakeResponseCacheable()
}
return attemptCacheHit()
}

Expand Down
24 changes: 1 addition & 23 deletions test/apicache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,7 @@ describe('.middleware {MIDDLEWARE}', function() {
})
})

it('skips cache when using header "x-apicache-force-fetch (legacy)"', function() {
it('updates cache when using header "x-apicache-force-fetch"', function() {
var app = mockAPI.create('10 seconds', { isBypassable: true })

return request(app)
Expand Down Expand Up @@ -1550,28 +1550,6 @@ describe('.middleware {MIDDLEWARE}', function() {
})
})

it('prevent cache skipping when using header "x-apicache-force-fetch (legacy)" with isBypassable "false"', function() {
var app = mockAPI.create('10 seconds', { isBypassable: false })

return request(app)
.get('/api/movies')
.expect(200, movies)
.then(assertNumRequestsProcessed(app, 1))
.then(function() {
return request(app)
.get('/api/movies')
.set('x-apicache-force-fetch', true)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect('apicache-store', 'memory')
.expect('apicache-version', pkg.version)
.expect(200, movies)
.then(function(res) {
expect(app.requestsProcessed).to.equal(1)
})
})
})

it('does not cache header in headerBlacklist', function() {
var app = mockAPI.create('10 seconds', { headerBlacklist: ['x-blacklisted'] })

Expand Down