- Cache the web resources like JS, css, images, fonts, etc.
- Reduce the traffic load.
- Improve the speed and performance of the app.
To achieve this, we need to leverage the multiple HTTP headers.
Headers:
- Cache-control: do you want to cache it, how long you want to cache.
- Expire :
- Last-Modified: When was this item last modified, so that next time when you come and we need to verify what is there in the data, when the data was last modified and present what you have.
- The client has some data, and the Client makes a request for data. If the server also has the same data(not updated), then so server will give acknowledgement to the client, then the client uses the local data; else the server will give the latest data to the client.
- Etag: it talks about some hash which generated and stored. Depending on your content, the hash is generated, so we verify based on the ETag, which is a hash. I have this etag, and if I have a different etag, then it means the data has been changed, and then give me the new data; otherwise, you don't give me data because I have data and I use it from cache.
Cache-control:
app.use((req, res, next)=>{
res.setHeader('Cache-Contro', 'public, max-age=86400')
})
Note: try different methods.
Expire:
app.use((req, res, next)=>{
res.setHeader('Expire', 'Sat, 30 July 11:20:39 GMT')
next()
})