Skip to content

Commit 1d7cf83

Browse files
committed
fix: handle more cache directives
1 parent 7c2f987 commit 1d7cf83

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

lib/interceptor/cache.js

+32-23
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,26 @@ class CacheHandler extends DecoratorHandler {
4242
return super.onHeaders(statusCode, headers, resume)
4343
}
4444

45-
const cacheControl = parseCacheControl(headers['cache-control'])
45+
const cacheControl = parseCacheControl(headers['cache-control']) ?? {}
46+
47+
if (this.#key.headers.authorization && !cacheControl.public) {
48+
return super.onHeaders(statusCode, headers, resume)
49+
}
50+
51+
if (cacheControl.private || cacheControl['no-store']) {
52+
return super.onHeaders(statusCode, headers, resume)
53+
}
54+
4655
if (
47-
!cacheControl ||
48-
!cacheControl.public ||
49-
cacheControl.private ||
50-
cacheControl['no-store'] ||
51-
// TODO (fix): Support all cache control directives...
52-
// cacheControl['no-transform'] ||
53-
cacheControl['no-cache'] ||
54-
cacheControl['must-understand'] ||
56+
cacheControl['no-transform'] ||
5557
cacheControl['must-revalidate'] ||
56-
cacheControl['proxy-revalidate']
58+
cacheControl['proxy-revalidate'] ||
59+
cacheControl['must-understand'] ||
60+
cacheControl['stale-while-revalidate'] ||
61+
cacheControl['stale-if-error'] ||
62+
cacheControl['no-cache']
5763
) {
58-
// Not cacheble...
64+
// TODO (fix): Support all cache control directives...
5965
return super.onHeaders(statusCode, headers, resume)
6066
}
6167

@@ -134,16 +140,17 @@ export default () => (dispatch) => (opts, handler) => {
134140
return dispatch(opts, handler)
135141
}
136142

137-
if (opts.headers?.['cache-control'] || opts.headers?.authorization) {
143+
const cacheControl = parseCacheControl(opts?.headers['cache-control']) ?? {}
144+
145+
if (
146+
cacheControl['max-age'] ||
147+
cacheControl['max-stale'] ||
148+
cacheControl['min-fresh'] ||
149+
cacheControl['no-cache'] ||
150+
cacheControl['no-transform'] ||
151+
cacheControl['stale-if-error']
152+
) {
138153
// TODO (fix): Support all cache control directives...
139-
// const cacheControl = cacheControlParser.parse(opts.headers['cache-control'])
140-
// cacheControl['no-cache']
141-
// cacheControl['no-store']
142-
// cacheControl['max-age']
143-
// cacheControl['max-stale']
144-
// cacheControl['min-fresh']
145-
// cacheControl['no-transform']
146-
// cacheControl['only-if-cached']
147154
return dispatch(opts, handler)
148155
}
149156

@@ -152,10 +159,12 @@ export default () => (dispatch) => (opts, handler) => {
152159

153160
const store = opts.cache.store ?? DEFAULT_STORE
154161
const entry = store.get(opts)
155-
if (!entry) {
162+
if (!entry && !cacheControl['only-if-cached']) {
156163
return dispatch(
157164
opts,
158-
new CacheHandler(undici.util.cache.makeCacheKey(opts), { store, handler }),
165+
cacheControl['no-store']
166+
? handler
167+
: new CacheHandler(undici.util.cache.makeCacheKey(opts), { store, handler }),
159168
)
160169
}
161170

@@ -174,7 +183,7 @@ export default () => (dispatch) => (opts, handler) => {
174183
}
175184
}
176185

177-
const { statusCode, headers } = entry
186+
const { statusCode, headers } = entry ?? { statusCode: 504, headers: {} }
178187
try {
179188
handler.onConnect(abort)
180189
if (aborted) {

0 commit comments

Comments
 (0)