Where do I add a javascript source file and how do I call it from html - without brunch?

I wont detail why I wont use the javascript ecosystem. I downloaded a js file that I have to host myself because its not always possible to link to external sources.

I’m in development now which was created with brunch included and thats why I have these folders:
/assets/js
and
/priv/static/js

But it is not clear to me where I’d put the file when I run without replying on brunch. I tried to put a copy of the js file on many paths then called in html:
<script src="<%= static_path(@conn, "/js/myfile.js") %>"></script>
but it wont work.

Check the Plug.Static configuration in your endpoint. It might be configured to serve only certain files or folders (e.g. only: ~w(images robots.txt).

1 Like

That is, given a Phoenix app “rlp”:

# rlp/lib/rlp_web/endpoint.ex

defmodule RlpWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :rlp

  socket "/socket", RlpWeb.UserSocket

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phoenix.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/", from: :rlp, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)
...

will only load from (under)

  • rlp/priv/static/css as /css
  • rlp/priv/static/fonts as /fonts
  • rlp/priv/static/images as /images
  • rlp/priv/static/js as /js
  • rlp/priv/static/favicon.ico as /favicon.ico
  • rlp/priv/static/robots.txt as /robots.txt
2 Likes

Solved it. Many, many thanks guys. I dont know what was wrong, the generated URL for js file was ok in html source , but I remember the logger told me that it could not find the route, only for that file. I started to access in the browser all the files in static until I got it right.