Skip to content

Commit 2cd3c8d

Browse files
committed
feat: show CDN node that served the request
1 parent 6097bf4 commit 2cd3c8d

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

app/components/CacheAnalysis.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ const { servedBy, cacheStatus } = getCacheAnalysis(props.cacheHeaders);
77
</script>
88

99
<template>
10-
<div>
11-
<dl>
12-
<dt>Served by</dt>
13-
<dd>{{ servedBy }}</dd>
14-
</dl>
10+
<div class="container">
11+
<div>
12+
Served by: <strong>{{ servedBy.source }}</strong>
13+
</div>
14+
<div>
15+
CDN node: <code>{{ servedBy.cdnNode }}</code>
16+
</div>
1517

1618
<hr />
1719

app/utils/getCacheAnalysis.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export enum ServedBy {
1+
export enum ServedBySource {
22
CDN = "CDN",
33
DurableCache = "Durable Cache",
44
Function = "Function",
@@ -77,10 +77,10 @@ export const parseCacheStatus = (
7777
);
7878
};
7979

80-
const getServedBy = (
80+
const getServedBySource = (
8181
cacheHeaders: Headers,
8282
cacheStatus: ParsedCacheStatusEntry[],
83-
): ServedBy => {
83+
): ServedBySource => {
8484
// Per the spec, these are sorted from "the cache closest to the origin server" to "the cache closest to the user".
8585
// So, the first cache hit (starting from the user) is the one that served the request.
8686
// But we don't quite want to return exactly the same concept of "caches" as in `Cache-Status`, so
@@ -91,21 +91,37 @@ const getServedBy = (
9191
} of cacheStatus) {
9292
if (!hit) continue;
9393

94-
if (cacheName === "Netlify Edge") return ServedBy.CDN;
95-
if (cacheName === "Netlify Durable") return ServedBy.DurableCache;
94+
if (cacheName === "Netlify Edge") return ServedBySource.CDN;
95+
if (cacheName === "Netlify Durable") return ServedBySource.DurableCache;
9696
}
9797

9898
// NOTE: the order is important here, since a response can be served by a Function even
9999
// though one or more Edge Functions are also invoked (as middleware).
100-
if (cacheHeaders.has("X-NF-Function-Type")) return ServedBy.Function;
100+
if (cacheHeaders.has("X-NF-Function-Type")) return ServedBySource.Function;
101101

102-
if (cacheHeaders.has("X-NF-Edge-Functions")) return ServedBy.EdgeFunction;
102+
if (cacheHeaders.has("X-NF-Edge-Functions"))
103+
return ServedBySource.EdgeFunction;
103104

104105
throw new Error(
105106
`Could not determine who served the request. Cache status: ${cacheStatus}`,
106107
);
107108
};
108109

110+
interface ServedBy {
111+
source: ServedBySource;
112+
cdnNode: string;
113+
}
114+
115+
const getServedBy = (
116+
cacheHeaders: Headers,
117+
cacheStatus: ParsedCacheStatusEntry[],
118+
): ServedBy => {
119+
return {
120+
source: getServedBySource(cacheHeaders, cacheStatus),
121+
cdnNode: cacheHeaders.get("X-BB-Host-Id") ?? "unknown CDN node",
122+
};
123+
};
124+
109125
export interface CacheAnalysis {
110126
servedBy: ServedBy;
111127
cacheStatus: ParsedCacheStatusEntry[];

app/utils/getCacheHeaders.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const CACHE_HEADER_NAMES = [
1515
"Vary",
1616
"X-BB-Deploy-Id",
1717
"X-BB-Gen",
18+
"X-BB-Host-Id",
1819
"X-NF-Cache-Info",
1920
"X-NF-Cache-Result",
2021
// TODO(serhalp) These two probably shouldn't be here but I use it to determine who served the

0 commit comments

Comments
 (0)