Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
akama-aka opened this issue Feb 4, 2025 · 6 comments
Open
2 tasks done

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

akama-aka opened this issue Feb 4, 2025 · 6 comments

Comments

@akama-aka
Copy link

akama-aka commented Feb 4, 2025

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.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

akama-aka/cdn-cgi#36

Expected Behavior

No response

@mcollina
Copy link
Member

mcollina commented Feb 4, 2025

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

@akama-aka
Copy link
Author

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

Oh lmao forgot to translate it TwT

@climba03003
Copy link
Member

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)

@markrzen
Copy link

markrzen commented Feb 7, 2025

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.

@akama-aka
Copy link
Author

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants