Skip to content

Not properly setting cache-control headers #477

Closed
@bhouston

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5

Plugin version

8.0.2

Node.js version

23

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

15.1

Description

This simple example does not set cache headers on any content correct:

import Fastify from 'fastify';
import { fastifyStatic } from '@fastify/static';

import { cacheHeader } from 'pretty-cache-header';

import path from 'path';

const app = Fastify({});

await app.register(fastifyStatic,{
  root: path.resolve(`./public` ),
  prefix: '/',
  cacheControl: true,
  setHeaders(res) {
    const cacheHeaderText = cacheHeader({
      public: true, 
      maxAge: '1 year',
      immutable: true });
    res.setHeader('cache-control', cacheHeaderText);
  },
});

// Run the server!
try {
  const address = await app.listen({
    port: 3000,
    host: 'localhost',
  });
  console.log(`Server is running on port ${address}`);
} catch (err) {
  app.log.error(err);
  process.exit(1);
}

Instead all results have Cache-Control: public, max-age=0 for some reason:

Screenshot 2024-10-28 at 1 24 55 PM

This is affecting the whole Fastify ecosystem that has moved to 5.x. The issue is especially problematic because this plugin doesn't explicitly fail with 5.x, rather it just doesn't do the correct thing.

Link to code that reproduces the bug

https://github.com/bhouston/fastify-basic-server

Expected Behavior

The cache control headers should be set correctly when using fastify 5.0 with @fasfify/static

This is breaking a number of tools right now including much of the Fastify-Remix integration.

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions