Skip to content

[BUG] Empty response body is not cached #293

@peterjanes

Description

@peterjanes

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

A response that returns an empty body (for example, a HEAD request) does not write a cache entry.

Expected Behavior

An empty response body should be written to the cache. (HEAD is useful when looking for redirected responses, for example, but I think it's legitimate to cache empty responses to GET and potentially other verbs.)

Steps To Reproduce

  1. In Node.js
  2. With a default make-fetch-happen instance (i.e. no .defaults())
  3. Run this:
import mfh from 'make-fetch-happen';
const res = await mfh('https://www.google.com/', { method: 'HEAD', cachePath: './cache' });
await res.text(); // "Requests will not be cached unless their response bodies are consumed."
console.log(res.headers.get('x-local-cache-status'));
  1. Observe that the x-local-cache-status header is set to skip, and ./cache/ hasn't been created.
  2. Change HEAD to GET and observe that x-local-cache-status is miss and ./cache is created and populated.

Environment

  • npm: 10.5.0
  • Node: v20.12.2
  • OS: Fedora 39
  • platform: Linux x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions