fastify-static icon indicating copy to clipboard operation
fastify-static copied to clipboard

Cache-Control Header keeps showing "public, max-age=0"

Open akama-aka opened this issue 10 months ago • 6 comments

Prerequisites

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

Fastify version

5.2.1

Plugin version

8.1.0

Node.js version

20.18.2

Operating system

Linux

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

rolling

Description

Hello,

as already described on the Discord server of Fastify, the documentation regarding the setHeader is extremely poor, at least I can't find any way to include the Cache-Control header functionally without getting back Cache-Control: public, max-age=0. Unfortunately there are also no tests or templates that rely on the setHeaders function despite the fact that Fastify, in my opinion, places an extremely high value on tests.

Anyway, back to the problem. I have already tried the following versions:

...
.register(require('@fastify/static'), {         // For all Static files like Styling, JavaScript Cde
        prefix: `/${process.env.PATH_IDENTIFIER}/assets/`,
        setHeaders: (res) => {
            res.setHeader('Cache-Control', 'public, immutable, max-age=2592000');
        },
        root: join(__dirname + '/public/assets/')
    })
...
...
.register(require('@fastify/static'), {         // For all Static files like Styling, JavaScript Cde
        prefix: `/${process.env.PATH_IDENTIFIER}/assets/`,
        setHeaders: (res) => {
            cacheControl: true,
            maxAge: 2592000
        },
        root: join(__dirname + '/public/assets/')
    })
...
...
.register(require('@fastify/static'), {         // For all Static files like Styling, JavaScript Cde
        prefix: `/${process.env.PATH_IDENTIFIER}/assets/`,
        setHeaders: (res) => {
            res.setHeaders({'Cache-Control': 'public, immutable, max-age=2592000'});
        },
        root: join(__dirname + '/public/assets/')
    })
....

and more.

I've seen some similar issues but they all use a Third Party Package which I don't want for something like that, that HAS TO BE easy to understand and implement

Link to code that reproduces the bug

https://github.com/akama-aka/cdn-cgi/pull/36

Expected Behavior

No response

akama-aka avatar Feb 04 '25 07:02 akama-aka

can you write it in English? I don't speak german.

mcollina avatar Feb 04 '25 09:02 mcollina

can you write it in English? I don't speak german.

Oh lmao forgot to translate it TwT

akama-aka avatar Feb 04 '25 09:02 akama-aka

Change the execution order may fix the issue, since the send package no longer inspect the response object. https://github.com/fastify/fastify-static/blob/bcf294fcbb26f8529ad50a1ae52639047a6bd440/index.js#L360-L363

climba03003 avatar Feb 04 '25 17:02 climba03003

To what @climba03003 is saying, the headers from send are overwriting the headers that are being set in #setHeaders.

As a work around, you can set cacheControl: false in the @fastify/static plugin, which will disable cache control headers in send. Then your setHeaders won't get overwritten.

markrzen avatar Feb 07 '25 18:02 markrzen

To what @climba03003 is saying, the headers from send are overwriting the headers that are being set in #setHeaders.

As a work around, you can set cacheControl: false in the @fastify/static plugin, which will disable cache control headers in send. Then your setHeaders won't get overwritten.

Thats exactly what I want. Static Files should be Cacheable because its kinda dumb to always load the same file over and over again

akama-aka avatar Feb 08 '25 10:02 akama-aka

Change the execution order may fix the issue, since the send package no longer inspect the response object.

fastify-static/index.js

Lines 360 to 363 in bcf294f if (setHeaders !== undefined) { setHeaders(reply.raw, metadata.path, metadata.stat) } reply.headers(headers)

What do you mean with Change the execution order exactly?

akama-aka avatar Feb 10 '25 07:02 akama-aka