Browser Caching Mechanism
Chrome browser uses multiple caching mechanisms to improve page load speed and reduce network requests.
Cache Types
-
Strong Cache
- Controlled by HTTP headers: Cache-Control and Expires
- Cache-Control: max-age=3600 means cache for 1 hour
- Higher priority than negotiated cache
-
Negotiated Cache
- Controlled by HTTP headers: ETag and Last-Modified
- ETag: Unique identifier of resource content
- Last-Modified: Last modification time of resource
- Needs to verify with server if resource has changed
-
Service Worker Cache
- Offline caching capability
- Can intercept network requests
- Provides more granular cache control
-
Memory Cache
- Stored in memory, invalidates after page closes
- Fast access speed but limited capacity
-
Disk Cache
- Stored on disk, can persist long-term
- Large capacity but relatively slower access speed
Caching Strategy
- Static Resources: Use strong cache with long expiration time
- HTML Files: Use negotiated cache to ensure latest content
- API Responses: Set appropriate caching strategy based on business needs
Cache Priority
- Service Worker Cache
- Memory Cache
- Disk Cache
- Network Request
Practical Applications
- Use version numbers or hash values to update static resources
- Set Cache-Control headers appropriately
- Use Service Worker for offline access