fireproofsocks

fireproofsocks

Serving only specific static assets

I’m wading through my app’s endpoint.ex – I want to only expose several hand-selected static assets in my Phoenix app, but I can’t seem to find the magic way to do it.

This doesn’t seem to work:

plug Plug.Static,
    at: "/",
    from: {:myapp, "priv/static"},
    only: ["xyz/some_file.json"]

http://localhost:4000/xyz/some_file.json - 404

Nor does this:

plug Plug.Static,
    at: "/",
    from: {:myapp, "priv/static/xyz"},
    only: ["some_file.json"]

http://localhost:4000/xyz/some_file.json - 404

If I open up the restrictions a bit, it does, e.g.

plug Plug.Static,
    at: "/",
    from: {:myapp, "priv/static"},
    only: ~w(xyz)

http://localhost:4000/xyz/some_file.json - OK!

Most Liked

konstantine

konstantine

My understanding is that there are 2 options:

:only, it will match exact file and folder names at the :at location (“/” in your case)

:only_matching, which works similarly, but allows for random filename ‘tails’ (this is useful when serving digested files)

With reference to your 3 examples, the first :only specifies a path rather than a name, the second a name that is not at the :at location, the third one works because xyz is right at the :at location (and the filename does not matter after that).

Assuming that my understanding is correct, you have two possibilities. Either you place your hand-selected assets in specific folders, whose names you specify, together with any files located at the root, in a single :only or :only_matching word list. Or, if you want to hand-select from various folders, you will need multiple plugs and multiple :at locations, each with its very own word list.

Last Post!

fireproofsocks

fireproofsocks

That sounds correct. I think my struggle stems from my attempts to include different files in different directories… the examples don’t have that… and I’m not even sure how to include static assets from 2 different directories…

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement