Romper
Romper

Reputation: 2257

HTTP Cache-Control and params order

Why browsers cache data, don't send request to server, only if query parameters in url are in same order?

For example if request to

/product?q=phone&cat=1

send in response header Cache-Control:max-age=60

the next request

/product?cat=1&q=phone

request is send to server, but should retrieve data from cache

Also some frameworks recommend

If you are caching requests that use a large number of query string parameters, consider sorting them to ensure that the request is properly cached.

By ordering your params, you can be sure the cache key will be consistent across requests and you are caching effectively.

Upvotes: 5

Views: 7812

Answers (3)

goose_lake
goose_lake

Reputation: 1480

The No-Vary-Search header, which is part of this HTTP standards draft, is meant exactly to address this. It is currently party-supported (as of early 2025 Firefox and Safari out of major browsers are missing support), but it seems likely to be adopted more widely as time passes.

For the problem in the question, No-Vary-Search: key-order should suffice.

For cases where some query parameters should be ignored entirely, and variations in them not taken into account when differentiating a resource (i.e. cached resource for URI https://www.example.com/something?irrelevantQP=1 should be re-used for a subsequent request to https://www.example.com/something?irrelevantQP=2), it's possible to include No-Vary-Search: params=("irrelevantQP").

Upvotes: 0

shaochuancs
shaochuancs

Reputation: 16246

According to the HTTP RFC, the primary key for cache is URI:

The primary cache key consists of the request method and target URI. However, since HTTP caches in common use today are typically limited to caching responses to GET, many caches simply decline other methods and use only the URI as the primary cache key.

Thus, all cache related headers: Cache-Control, Expires, ETag etc. are all based on one precondition: resource of the same URL.

Upvotes: 4

Julian Reschke
Julian Reschke

Reputation: 42045

HTTP doesn't define the syntax of query parameters. So from the cache's point of view, these URLs might identify different content.

Upvotes: 8

Related Questions