kiru

kiru

We are using Phoenix with Plug.Static to serve static files (e.g. images and js files).

Some images were not cached in the borwser, and it turned out we were not using Routes.static_path(@conn, "images/bla.png") instead, we just added the path like this: <img src="images/bla.png" />.

I see in the browser, that the server replies with the following response headers:

cache-control: public
Connection: keep-alive
Date: Mon, 16 Jan 2023 13:15:03 GMT
etag: “52BBD1A”

This is correct, since public is the default value for cache_control_for_etags. I tried to understand what the default (without explicit expiration date) exactly means, but the online resources are a bit sparse.

This SO answers recommends not to use “Cache-control: public” at all, while this article recommends not to put a max-age at all:

97.3% of responses containing it [Cach-control: public] have max-age or s-maxage too, making it redundant

So far, what I understood is that by omitting the expiration date, the browser (or the CDN in between) guesses the expiration value using some heuristics (source).

My question is the one stated in the topic, why does “public” without expiration makes sense as the default value?

I am not sure if this is the correct place to ask. Thank you in advance for answering.

Kiru

Showing Posts 1 to 3

BartOtten

BartOtten

The SO answer you link to is not advising against ‘public’ for static assets, but setting it as default for all responses. Even responses with private data could end up in a cache of you and all parties in between with such default.

The article is about using a value for max-age, which means you know better than heuristics. Sometimes a developer does, most of the times they don’t.

Now for Plug…

For static assets, the etag is used. This is a check-value based on the content or metadata of a response. When the browser needs something it already cached once it will communicate with the webserver if that check-value is still current. If it is, the file is not returned over the wire and the browser uses the cache. So this method always requires a call, but server software is extremely fast at answering those (they already know the checksum of the file as they cache the checksum!).

As soon as the check-value changes the cache is cleared for that item.

So the default of Plug says: you (and everyone in between) can cache this asset as long as you like as we trust your heuristics. However, if the content changed you’ll need a fresh copy.

LostKobrakai

LostKobrakai

If the request query string starts with “?vsn=”, Plug.Static assumes the application is versioning assets and does not set the ETag header, meaning the cache behaviour will be specified solely by the cache_control_for_vsn_requests config, which defaults to "public, max-age=31536000".

That’s the default for phoenix when using static_path though, which is explicitly not using etags. Though I’m not really sure as to why exactly.

BartOtten

BartOtten

Because now the file can have other properties on disk without having a ‘uncached’ etag status.

Common implementations use the ‘file modified’ property for etag generation as it’s cheap to get by. As a result a re-compile of assets might make all assets uncached even when the content of only one has changes.

So it’s about taking control of the cache-status from the webserver to the (asset generating) application.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews