Skip to content

Commit

Permalink
Set cache header in creator node CID route (#959)
Browse files Browse the repository at this point in the history
* Set cache header in creator node CID route

* Move remove cache headers in getCID

* Move remove header cache
  • Loading branch information
jowlee authored Oct 21, 2020
1 parent a9447b9 commit 1d70307
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions creator-node/src/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ const getCID = async (req, res) => {
res.setHeader('Content-Disposition', contentDisposition(req.query.filename))
}

// Set the CID cache-control so that client cache the response for 30 days
res.setHeader('cache-control', 'public, max-age=2592000, immutable')

try {
// Add a rehydration task to the queue to be processed in the background
RehydrateIpfsQueue.addRehydrateIpfsFromFsIfNecessaryTask(CID, queryResults.storagePath, { logContext: req.logContext })
Expand Down Expand Up @@ -187,6 +190,9 @@ const getCID = async (req, res) => {
.on('error', e => { reject(e) })
})
} catch (e) {
// Unset the cache-control header so that a bad response is not cached
res.removeHeader('cache-control')

// If the file cannot be retrieved through IPFS, return 500 without attempting to stream file.
return sendResponse(req, res, errorResponseServerError(e.message))
}
Expand Down Expand Up @@ -229,6 +235,9 @@ const getDirCID = async (req, res) => {
req.logger.info(`IPFS Standalone Request - ${ipfsPath}`)
req.logger.info(`IPFS Stats - Standalone Requests: ${totalStandaloneIpfsReqs}`)

// Set the CID cache-control so that client cache the response for 30 days
res.setHeader('cache-control', 'public, max-age=2592000, immutable')

try {
// Add rehydrate task to queue to be processed in background
RehydrateIpfsQueue.addRehydrateIpfsFromFsIfNecessaryTask(dirCID, parentStoragePath, { logContext: req.logContext }, filename)
Expand Down Expand Up @@ -264,6 +273,8 @@ const getDirCID = async (req, res) => {
.on('error', e => { reject(e) })
})
} catch (e) {
// Unset the cache-control header so that a bad response is not cached
res.removeHeader('cache-control')
return sendResponse(req, res, errorResponseServerError(e.message))
}
}
Expand Down

0 comments on commit 1d70307

Please sign in to comment.