Static resources added version number still hit the old cache, how should the cache header be written?
After adding version numbers to static resources, the browser still hits the old cache. The problem is usually not with the version number itself, but with the lack of coordination between the cache response header and the filename strategy. These are two separate issues:Freshness is determined by max-age, and whether the old copy can be used after expiration is determined by the validation requirements.If only a long freshness period is set without requiring a source validation, the browser may still reuse an outdated copy after expiration; and when the filename version and path remain unchanged, the old response may still be in the cache.
The cache of static resources directly determines the page loading speed, and also determines how many repeated requests the source site needs to handle under high concurrency, so this level of configuration is worth calculating separately by object, rather than using a uniform duration for the entire site.
Why is the browser still using the old file?
A point that is easy to misunderstand:max-ageIt refers to how fresh the response remains within a certain period after it is generated on the source site.The starting point is the generation time, not the receipt time.. Resources that have been cached or not accessed for a long time actually have a shorter available time than it appears on the surface. When it expires, the subsequent behavior depends on whether there are validation requirements.
The version number solves the issue of 'the same URL corresponds to different content.' When the file content changes, the address also changes, and the copy of the old address in the cache naturally no longer referenced; conversely, if the version number is only added to the query parameters while the path is reused, it may still encounter the old response.
Which field manages the freshness period and validation requirements
max-ageSet the freshness period, and it can be reused within the period;must-revalidateThe requirement is to verify the source after expiration, if it cannot be verified, return 504 instead of continuing to send out outdated copies.no-storeIt is completely not allowed to store, it is two different things from 'store but require verification'.immutableIt indicates that the response will not be updated during the fresh period, thus avoiding unnecessary conditional requests, it is suitable for use with version numbers - the filename has determined the corresponding content version.
There is a common misconfiguration: setting for resources with version numbers at the same time.no-storeIt is equivalent to offsetting the benefits of versioning, and sourcing every time.
Why should shared cache and private cache be written separately.
s-maxageIt only takes effect on shared cache (CDN, reverse proxy) and will override.max-agewithExpires; Ignore it in private cache. So when the site is in front of a reverse proxy or CDN, only writemax-ageIt will make the shared cache follow the same duration, changing the style once will take a long time to refresh the entire network. The purpose of writing separately is to give the proxy layer a longer freshness period and the browser a shorter update freshness period.
Where should the version number be added to be effective
The version number added to the filename or path is more reliable than just adding query parameters: some caches and acceleration services handle addresses with parameters differently. At the same time, it must be confirmed that the publishing process really produces new files and not just pushes the old files again.
This is related to the deployment method of the site. For those deployed using Docker images, the static files are usually in the mounted volume, and when publishing, you need to confirm that the volume content is updated with the version; for those deployed using Baota panel or aaPanel, after the revision, you should also confirm whether the reverse proxy cache has been refreshed. For page URLs that depend on pseudo-static rules, do not affect the existing addresses when changing the file name strategy; when it comes to domain name or directory migration, use 301 redirect to point the old address to the new address so that the cache and search spiders can follow up.
The main page and static resources are separated
| object | Freshness orientation | Complementary fields | Reason |
|---|---|---|---|
| Static resources with version numbers | Long | immutableCompared with versioned filenames |
The address has been determined and the content does not need frequent verification |
| Static resources without version numbers | Short | Maintain verification requirements | The address does not change when the content is modified, and long caching will hinder updates |
| Page HTML content | Very short or not cached | Verification on demand | The content may be updated at any time, and readers will not see new articles if the cache is too long |
| Interface response class | As the data varies | Use shared cache cautiously | Responses with user identity are not suitable for reuse in shared cache |
The caching strategy of the page entity has the most direct impact: if the list page and article page are cached for a long time, readers may not see the new content they publish first. The refresh granularity at the proxy or CDN level also needs to be supplemented - if it is possible to refresh by path, do not use a full site clearance.
Common questions
Question: If a version number is added, do we still need must-revalidate?Answer: It can be optional, but don't write it as allowing outdated reuse. Versioned resources are more suitable for long freshness periods with immutable, while the page entity still needs to retain verification requirements.
How do you confirm which cache the browser is using?Look at the actual source and headers of the response, compare with the incognito window, it is more reliable than just looking at the local file time.
Do you need to change the version number every time you change the style?As long as the publishing process can automatically generate new filenames, there is no need to manually record them, the key point is to keep the content and address consistent.