fastify-caching icon indicating copy to clipboard operation
fastify-caching copied to clipboard

Only set cache for 200, 301 and 302 response

Open adityapatadia opened this issue 7 years ago • 5 comments

Currently cache-control header is sent for even 404 response. Can we avoid it?

adityapatadia avatar Sep 18 '18 10:09 adityapatadia

Thanks for reporting this!

It definitely should, would you like to send a PR?

You should add a check for the status codes in https://github.com/fastify/fastify-caching/blob/master/plugin.js#L43-L52.

mcollina avatar Sep 18 '18 10:09 mcollina

I am talking about this line: https://github.com/fastify/fastify-caching/blob/master/plugin.js#L72

adityapatadia avatar Sep 18 '18 11:09 adityapatadia

I think that should move to a onSend hook instead.

mcollina avatar Sep 18 '18 11:09 mcollina

Well I thought about it and current one seems fine to me. I set cache control to private by default and when I need the response to be cached, I set the header with reply.header. If we move it there, there must be a check that it should not set cache-control header if something in the app has already set it.

adityapatadia avatar Sep 18 '18 11:09 adityapatadia

Yes exactly.

mcollina avatar Sep 18 '18 11:09 mcollina