1
- export enum ServedBy {
1
+ export enum ServedBySource {
2
2
CDN = "CDN" ,
3
3
DurableCache = "Durable Cache" ,
4
4
Function = "Function" ,
@@ -77,10 +77,10 @@ export const parseCacheStatus = (
77
77
) ;
78
78
} ;
79
79
80
- const getServedBy = (
80
+ const getServedBySource = (
81
81
cacheHeaders : Headers ,
82
82
cacheStatus : ParsedCacheStatusEntry [ ] ,
83
- ) : ServedBy => {
83
+ ) : ServedBySource => {
84
84
// Per the spec, these are sorted from "the cache closest to the origin server" to "the cache closest to the user".
85
85
// So, the first cache hit (starting from the user) is the one that served the request.
86
86
// 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 = (
91
91
} of cacheStatus ) {
92
92
if ( ! hit ) continue ;
93
93
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 ;
96
96
}
97
97
98
98
// NOTE: the order is important here, since a response can be served by a Function even
99
99
// 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 ;
101
101
102
- if ( cacheHeaders . has ( "X-NF-Edge-Functions" ) ) return ServedBy . EdgeFunction ;
102
+ if ( cacheHeaders . has ( "X-NF-Edge-Functions" ) )
103
+ return ServedBySource . EdgeFunction ;
103
104
104
105
throw new Error (
105
106
`Could not determine who served the request. Cache status: ${ cacheStatus } ` ,
106
107
) ;
107
108
} ;
108
109
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
+
109
125
export interface CacheAnalysis {
110
126
servedBy : ServedBy ;
111
127
cacheStatus : ParsedCacheStatusEntry [ ] ;
0 commit comments