server static file + caching

Hey guys, I am serving a static folder called public and it's all working well, I can access my files, eg styles.css. Here is the code for it:
app
.use("*", validateRequest)
.use("/public/*", serveStatic({ root: "./" }))
app
.use("*", validateRequest)
.use("/public/*", serveStatic({ root: "./" }))
However I didn't realize that hono wasn't caching the assets in /public by default. Is there any way to tell hono to cache that directory ?
12 Replies
Aditya Mathur
Aditya Mathur3mo ago
This does depends on the deployment solution you are using. Most of them automatically caches these assets for you like cloudflare, vercel
Augustin Sorel
Augustin SorelOP3mo ago
Sorry I should have mentioned that in the question, I was talking about the built in browser cache.
Aditya Mathur
Aditya Mathur3mo ago
I think browser already does it for the client, except if you have enabled disabled cache in your network tab (usually for testing)
Augustin Sorel
Augustin SorelOP3mo ago
that's why I am a bit confused as well as the browser isn't caching it. I don't have disabled cache enabled. Caching does work as it can cache other assets. But not the one from /public/*.
No description
Aditya Mathur
Aditya Mathur3mo ago
Someone told me about this message, is this helpful in your case? - https://discord.com/channels/1011308539819597844/1012485912409690122/1265753791274156233
Augustin Sorel
Augustin SorelOP3mo ago
not really as I am not using cloudflare. I am just running a basic vps
Aditya Mathur
Aditya Mathur3mo ago
what is your setup looks like and why is JS getting cached and not your HTML and CSS. Can you share a reproduction of this? I will try to figure this out tomorrow
Augustin Sorel
Augustin SorelOP3mo ago
Thanks for taking your time and looking into it 🙏 here is the github repo: https://github.com/AugustinSorel/ungrading-portfolio
GitHub
GitHub - AugustinSorel/ungrading-portfolio
Contribute to AugustinSorel/ungrading-portfolio development by creating an account on GitHub.
Aditya Mathur
Aditya Mathur3mo ago
Can you point me to the files were you are serving those files
Augustin Sorel
Augustin SorelOP3mo ago
My bad I should have explained that, sorry. if you run bun styles:build this should generate the css file in public/styles.css. Then in my /src/index.ts I am serving the folder public statically. if you then run the app with bun dev or bun server:dev and open up the dev tool you should see that the css file isn't being cached by the browser.
Aditya Mathur
Aditya Mathur3mo ago
Okay. Also, check out this issue https://discord.com/channels/1011308539819597844/1011308539819597847/1281919444775342162 which is adding headers to the static files, maybe once this is merged your problem will be resolved!
Augustin Sorel
Augustin SorelOP3mo ago
that sounds good I will keep an eye on it. Thanks for the help 🫶
Want results from more Discord servers?
Add your server