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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews