简体   繁体   中英

@Header('Cache-Control') in NestJs

I have an NestJS REST api and noticed that Cache-Control header is not sent. I am not sure if i miss something with nestjs but asking google how to cache REST API get response with nestjs only leads me to NestJs cache manager which is on server in memory cachig. So I have couple of questions:

  1. Is it not common to cache REST API data responses
  2. Should I set @Header('Cache-Control', '..?') explicitly
  3. Maybe it is better to cache on the client? (I know about data validity - data is not changing I am talking about articles)

Thanks for the answers

Yes you can set it with the header annotation:

https://docs.nestjs.com/controllers#headers

@Get()
@Header('Cache-Control', 'max-age=3600')
get() {
  return 'This action adds a new cat';
}

btw. its in the rest definition that the should be cachable, just not every method: https://en.wikipedia.org/wiki/Representational_state_transfer#Cacheability

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM