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

Possibility to set option to static serving (via express.static) #1221

Closed
changke opened this issue Aug 20, 2023 · 3 comments
Closed

Possibility to set option to static serving (via express.static) #1221

changke opened this issue Aug 20, 2023 · 3 comments

Comments

@changke
Copy link
Contributor

changke commented Aug 20, 2023

Hi,

my use case could actually be not so significant, but anyway :-)

I do use FoalTS to write traditional SSR apps, where also static files like CSS and JS are served by Foal, default to put them in the public folder.

Recently while debugging a CDN caching issue, I noticed those static resources all have a cache-control: public, max-age=0, max-age=<some other number> being set <- Yes, two different "max-age" values.

Multiple "max-age" values could be considered invalid, and it will depend on client to choose the actual number, my CDN in this case chose 0 -> so always missing cache.

On my VPS, I use Apache as reverse proxy server and the H5BP apache config to optimize caching, it will e.g. add some 1 year cache time to CSS files, it will merge the existing "cache-control" rather than replacing it, that was the cause of two "max-age" values.

But where does the first "max-age=0" come from? Turns out it was set by express.static() [1] which Foal uses to serve static files.

One can of course set some options to disable or overwrite this default value, but in Foal currently is not possible, since express.static() is called directly from create-app.ts [2]

My current workaround is

  1. Not use the default public folder for static files, create another one e.g. static
  2. Use the afterPreMiddlewares to manually call express.static(), as documented [3]

Code changes in index.ts

import * as express from 'express';

const app = await createApp(AppController, {
    afterPreMiddlewares: [express.static('static', {cacheControl: false})]
  });

Now all static files in the static folder are served by the new middleware without cache-control set, and I've got correct header with only one "max-age" value.

It is obviously verbose and not so elegant, so I wish there could be a way to set options at least to static serving middleware somehow.

Thanks for reading :-)

[1] https://expressjs.com/en/4x/api.html#express.static
[2]

express.static(Config.get('settings.staticPath', 'string', 'public'))

[3] https://foalts.org/docs/common/expressjs#pre-and-post-express-middlewares

@LoicPoullain
Copy link
Member

We could add the possibility to pass an option to the createApp static middleware.

Are you sure the { cacheControl: false } works? When looking at the documentation and the implementation of the middleware, I don't see a mention of this property:

@changke
Copy link
Contributor Author

changke commented Oct 29, 2023

@LoicPoullain
Copy link
Member

Thank you for your reply. The feature has been added in v4.2. 👍

@LoicPoullain LoicPoullain moved this to Done / Closed This Release in Issue tracking Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

2 participants