kiru

kiru

Why does Plug.Static not define an cache expiration date by default?

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

First Post!

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.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

We're in Beta

About us Mission Statement