Rafael Marques
Rafael Marques

Reputation: 1445

Cache-Control headers - ignore query parameters

I am setting up a Cloudfront distribution for my companies website. We would like to set the caching time by using the Cache-Control headers on the server-side (Node.Js with Express), like this:

if (req.url.startsWith('/static')) {
   res.setHeader('Cache-Control', 'public,max-age=500');
}

At first, this seems to work well, but one of the criteria for the cache is failing, and that is, to ignore query string parameters.

For example, the request "domain.com/static/logo" and "domain.com/static/logo?foo=bar" should be interpreted as the same resource, and cached as one.

I wonder if it is possible to cache a resource while ignoring its query string parameters, using only the Cache-Control headers.

Thank you.

Upvotes: 1

Views: 978

Answers (1)

James Dean
James Dean

Reputation: 4421

Bydefault CloudFront does remove the query string and also doesn't consider it into the cache , this is a default behaviour of CloudFront so that there are not multiple cache copies based on different query string parameter. If you don't seem this behaviour, you may have "Query string" set to Forward all and cache based on call in CloudFront's cache behaviour.

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/QueryStringParameters.html

Upvotes: 1

Related Questions