Skip to content

Cache prioritises cacheByDefault/max-age over no-cache response directive #4173

Description

@alxndrsn

Bug Description

When cacheByDefault is set, no revalidation occurs for a response which had the no-cache directive.

Reproducible By

const http = require('node:http');

const server = http.createServer((req, res) => {
  console.log('request received');

  req.on('data', () => {});

  req.on('end', () => {
    res.setHeader('cache-control', 'no-cache');
    res.end('OK');

    console.log('Closing server...');
    server.close(() => console.log('Server closed.'));
  });
});

const port = 1234;
server.listen(port, async () => {
  console.log('server listening on port', port);

  const undici = require('undici');

  const dispatcher = new undici.Agent().compose(undici.interceptors.cache({
    cacheByDefault: 1000,
  }));
  
  const res1 = await undici.fetch(`http://localhost:${port}/`, { dispatcher });
  console.log('res1.status:', res1.status);

  await new Promise(resolve => setTimeout(resolve, 200));

  const res2 = await undici.fetch(`http://localhost:${port}/`, { dispatcher });
  console.log('res2.status:', res2.status);
});

Expected Behavior

https://datatracker.ietf.org/doc/html/rfc9111#name-no-cache-2:

The no-cache response directive, in its unqualified form (without an argument), indicates that the response MUST NOT be used to satisfy any other request without forwarding it for validation and receiving a successful response

Logs & Screenshots

server listening on port 1234
request received
Closing server...
Server closed.
res1.status: 200
res2.status: 200

Environment

node: v22.12.0
undici: 7.8.0

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions