#How to import custom css and javascript in phoenix
27 messages · Page 1 of 1 (latest)
I think it's best to keep the assets folder as just "phoenix managed assets" that go through the asset build process (.e.g esbuild + tailwind). For other static assets, maybe it's best to put them into priv/static directly instead
yeah I put the images and fonts in priv/static
put the js and css in there too and put <script> and <link> tags in your header to directly reference them
works in dev but not in production
I extended static_paths to contain js, css and img but still doesn't work in prod
def static_paths, do: ~w(assets fonts js css img images favicon.ico robots.txt)
what do you mean by "doesn't work"?
Sorry for being unclear, the file is not found.
e.g.
mydomain.com/assets/css/flaticon.css 404
in root.html.heex: <link rel="stylesheet" href={~p"/assets/css/flaticon.css"} />
but it works in dev locally?
Yes
are you sure the priv/static stuff is being included in your release? it should be, I don't see why not...
have you checked your Plug.Static in your endpoint to make sure it looks right?
I don't really see what could cause a difference between dev and prod for this...
other than the files not being included in the release
priv/static/assets is in the generated gitignore when setting up a phoenix project so maybe it's wiped by esbuild
how are you deploying / releasing to prod?
docker image
have you checked the contents of your docker image to check that the files are actually there?
Yes, they are getting wiped after esbuild. I just confirmed it 🍻
👍
So, the answer to my question is:
Don't put static files under priv/static/assets, priv/static/assets is reserved for the output from esbuild.
maybe put them in priv/static/vendor instead and leave assets alone?
yeah maybe, but you have control over this so you could change that if you want
That is exactly how I solved it 🙂
but I suggest maybe not, and using a different folder