You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Called right before the response is cached. Allows you to control caching behavior by modifying response properties or preventing caching entirely.
247
+
248
+
This is especially useful when you want to prevent caching of specific responses or modify cache headers.
249
+
250
+
**Return value:**
251
+
> -`false` - Prevent caching (remaining hooks are skipped)
252
+
> -`void`/`undefined` - Use default caching behavior (mutations take effect)
253
+
254
+
**Modifying the response:**
255
+
> - Hooks can directly mutate response properties like `headers`, `statusCode`, and `statusMessage`
256
+
> - Mutations to `response.headers` affect how the caching layer decides whether to cache the response and for how long
257
+
> - Changes are applied to what gets cached, not to the response the user receives (they are separate objects)
258
+
259
+
**Note:**
260
+
> - This hook is only called when the `cache` option is enabled.
261
+
262
+
**Note:**
263
+
> - This hook must be synchronous. It cannot return a Promise. If you need async logic to determine caching behavior, use a `beforeRequest` hook instead.
264
+
265
+
**Note:**
266
+
> - When returning `false`, remaining hooks are skipped. The response headers the user receives are NOT modified - only the caching layer sees modified headers.
267
+
268
+
**Note:**
269
+
> - If a hook throws an error, it will be propagated and the request will fail. This is consistent with how other hooks in Got handle errors.
270
+
271
+
**Note:**
272
+
> - At this stage, the response body has not been read yet - it's still a stream. Properties like `response.body` and `response.rawBody` are not available. You can only inspect/modify response headers and status code.
// Don't call remaining hooks - we've decided not to cache
1249
+
return;
1241
1250
}
1242
1251
1243
-
returnresult;
1244
-
};
1252
+
if(is.promise(result)){
1253
+
// BeforeCache hooks must be synchronous because cacheable-request's handler is synchronous
1254
+
thrownewTypeError('beforeCache hooks must be synchronous. The hook returned a Promise, but this hook must return synchronously. If you need async logic, use beforeRequest hook instead.');
1255
+
}
1256
+
1257
+
if(result!==undefined){
1258
+
// Hooks should return false or undefined only
1259
+
// Mutations work directly - no need to return the response
1260
+
thrownewTypeError('beforeCache hook must return false or undefined. To modify the response, mutate it directly.');
1261
+
}
1262
+
// Else: void/undefined = continue
1263
+
}
1264
+
}catch(error: any){
1265
+
// Convert hook errors to RequestError and propagate
1266
+
// This is consistent with how other hooks handle errors
Called right before the response is cached. Allows you to control caching behavior by directly modifying the response or preventing caching.
362
+
363
+
This is especially useful when you want to prevent caching of specific responses or modify cache headers.
364
+
365
+
@default []
366
+
367
+
**Return value:**
368
+
> - `false` - Prevent caching (remaining hooks are skipped)
369
+
> - `void`/`undefined` - Use default caching behavior (mutations take effect)
370
+
371
+
**Modifying the response:**
372
+
> - Hooks can directly mutate response properties like `headers`, `statusCode`, and `statusMessage`
373
+
> - Mutations to `response.headers` affect how the caching layer decides whether to cache the response and for how long
374
+
> - Changes are applied to what gets cached
375
+
376
+
**Note:**
377
+
> - This hook is only called when the `cache` option is enabled.
378
+
379
+
**Note:**
380
+
> - This hook must be synchronous. It cannot return a Promise. If you need async logic to determine caching behavior, use a `beforeRequest` hook instead.
381
+
382
+
**Note:**
383
+
> - When returning `false`, remaining hooks are skipped and the response will not be cached.
384
+
385
+
**Note:**
386
+
> - Returning anything other than `false` or `undefined` will throw a TypeError.
387
+
388
+
**Note:**
389
+
> - If a hook throws an error, it will be propagated and the request will fail. This is consistent with how other hooks in Got handle errors.
390
+
391
+
**Note:**
392
+
> - At this stage, the response body has not been read yet - it's still a stream. Properties like `response.body` and `response.rawBody` are not available. You can only inspect/modify response headers and status code.
0 commit comments