Yes, you can often determine whether a request was served from a CDN cache or whether it had to go to the origin server by inspecting the HTTP headers in the response. Most CDNs insert specific headers to indicate the cache status. Here are some steps and common headers you can check using the browser's developer tools:
Using Browser Developer Tools
-
Open Developer Tools:
- Right-click on the webpage and select “Inspect” or press
Ctrl+Shift+I (Windows/Linux) or Cmd+Opt+I (Mac) to open Developer Tools.
- Navigate to the Network tab.
-
Reload the Page:
- Refresh the page to capture all network requests.
-
Inspect Network Requests:
- Click on individual requests in the list to view details.
-
Check the Response Headers:
- Look for specific headers that CDNs use to indicate cache status.
Common CDN-Specific Headers
Here are some headers you might see and what they typically indicate:
-
X-Cache or CF-Cache-Status (for Cloudflare):
- HIT: The content was served from the CDN cache.
- MISS: The content was not in the CDN cache, so it was fetched from the origin server.
- EXPIRED: The cached content was expired and had to be refreshed from the origin server.
- BYPASS: The CDN did not cache the content (this can happen for various reasons).
-
X-Cache (for AWS CloudFront):
- Hit from cloudfront: The content was served from the CloudFront cache.
- Miss from cloudfront: The content was fetched from the origin server because it was not in the cache.
- RefreshHit from cloudfront: The content was refreshed in the cache.
-
X-Cache-Status (for various CDNs like Fastly or Akamai):
- HIT: The content was served from the CDN cache.
- MISS: The content was fetched from the origin server.
- REVALIDATED: The content was revalidated with the origin server and served from the cache.
- STALE: The cached content was served but it is outdated.
Example
Here’s an example of what the headers might look like for a Cloudflare-cached response:
HTTP/1.1 200 OK
Date: Mon, 23 Jan 2023 12:34:56 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
Connection: keep-alive
CF-Cache-Status: HIT
...
If you see CF-Cache-Status: HIT, the content was served from Cloudflare's cache.
Conversely, if the status is MISS, then the content was fetched from the origin server as it was not present in the cache.
Summary
To determine if a request was served from a CDN cache:
- Open the browser's Developer Tools.
- Go to the Network tab.
- Inspect the response headers of the requested resources.
- Look for CDN-specific headers that indicate whether the request was a cache hit or miss.
This method allows you to understand and verify the behavior of the CDN in your content delivery strategy.