Phoenix: Make static assets directory location configurable through config.exs

Created on 7 Oct 2014  路  7Comments  路  Source: phoenixframework/phoenix

At present all javascript, css etc. are kept in priv/static directory relative to root folder of the Phoenix app. This location should be configurable to any absolute location of the filesystem.
The rationale is:
1) It will make having common javascript and css etc. librarieris to be kept at common place, instead of copying them separately for each project.
2) If the app is converted to escript, priv directory access is lost. Having an absolute directory location for static assets will solve this issue.

For reference, please check the "Serve all files from a directory" section of http://ninenines.eu/docs/en/cowboy/HEAD/guide/static_files/

Most helpful comment

@vitalis Found it today. The configuration can be found at the endpoint.ex with the Static plug.

plug Plug.Static, 
   at: "/", from: "path/to/your/external-static-folder", gzip: false,
   only: ~w(css fonts images js favicon.ico robots.txt)

https://hexdocs.pm/plug/Plug.Static.html

All 7 comments

On master it is already configurable. You just do config :phoenix, MyApp.Router, static: [from: "/path/to/files"]

@josevalim this config is still relevant? I'm trying to change it in my project without success

@vitalis Found it today. The configuration can be found at the endpoint.ex with the Static plug.

plug Plug.Static, 
   at: "/", from: "path/to/your/external-static-folder", gzip: false,
   only: ~w(css fonts images js favicon.ico robots.txt)

https://hexdocs.pm/plug/Plug.Static.html

It still does not work for me. I have my JS file in /assets/js/index.js and configured Plug.Static to be

plug Plug.Static,
    at: "/", from: {:api_test, "assets"}, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

While debugging the static.ex in plug deps I found out that it's looking for the path

/_build/dev/lib/api_test/assets/js/index.js

But, my _build folder does not have the assets folder. I can see that the default priv folder is in there.

image

So I am assuming the cowboy build did not copy my custom assets folder? How to get around this?

@taher435 you can only serve directories from priv because that's the only directory that is copied to builds and used on releases, etc, etc. I would recommend to keep the pattern of putting them on priv/static.

@josevalim if that is the only folder we should keep our assets in then having the option of adding a custom path in from: in Plug.Static is useless?

@taher435 it can still be used for single file examples, where you want to quickly run a webserver with mix run. But for any project, you need to stick to priv.

Was this page helpful?
0 / 5 - 0 ratings