#How to set cache-control headers for static assets using SSR?

7 messages · Page 1 of 1 (latest)

vestal scarab
#

I'm using output: "server" and the deno adapter. Images in /public are served without any cache control headers.
I'm new to Astro, but from a stroll through the docs it looks like http headers can be set for Astro components (though I'm not sure if that works for SSR). But I can't see how to set them for static assets, notably images. Anyone know where to do this?

vestal scarab
#

Hmm I had a squizz at the Deno adapter, and it seems that Astro doesn't intervene with the static asset response at all. The handler just reads and serves the file: https://github.com/withastro/astro/blob/c596c6c995ebfb2b005ca5f902fa498fffbcb50a/packages/integrations/deno/src/server.ts#L40
It would be possible to adapt the handler to add cache control headers to the response, though that's all-or-nothing.
Is a more granular way possible (other than by a substantial rewrite of the adapter)?

GitHub

Build faster websites with Astro's next-gen island architecture 🏝✨ - astro/server.ts at c596c6c995ebfb2b005ca5f902fa498fffbcb50a · withastro/astro

ornate kernel
#

If an asset requires changes in headers dynamically, it's then probably better served by an api endpoint no ? You can then set any header you want. You can also consider astro in middleware mode, like that you can serve the static assets yourself, the way you want, without rewriting the astro part.

vestal scarab
#

I'm not sure what you mean 'dynamically' here - it's typically a default for images to be served with expires or cache control headers set. I haven't found a way to do that for statically-served images in the /public directory using Astro in SSR mode, and am asking if there is a way.

I'll look into the API endpoint thing - as I mentioned, I'm new to Astro. I don't know what middleware mode is - do you have a docs link handy?

#

Ah right just found middleware mode in the docs. Thanks but I'd rather do without that. The simplicity of SSR + serverless-style hosting is what I'm after in this case.

ornate kernel
#

by dynamically, I meant surely updating after build with a certain update rate, e.g. likely to be 1 day or 1 week, that way you set your browser cached asset to become invalid after that period of time.
It is necessary to check your host where will you deploy the static assets ?
If you consider writing your own adapter, I expected middleware to be easier, but you know better what's easier for you.