Phoenix + Elixir: outdated static file is served

55 Views Asked by At

I have a sitemap file that is located in priv/static/sitemaps/sitemap1.xml

▾ priv/
  ▸ gettext/
  ▸ repo/
  ▾ static/
    ▸ assets/
    ▸ images/
    ▾ sitemaps/
        sitemap.xml
        sitemap1.xml   // <=================
      favicon.ico
      robots.txt

In addition, I have added the sitemaps folder to static_paths in lib/MyApp.ex

  def static_paths, do: ~w(assets fonts images favicon.ico robots.txt sitemaps)

However, it continues to serve an outdated version of my sitemap when I visit https://www.myapp.com/sitemaps/sitemap1.xml even after multiple deploys / restarts. I even sshed into the server and opened up the file to confirm that the file was different from the one being served.

Am I missing a step? Or is there another way of serving static files?

1

There are 1 best solutions below

0
Adam Millerchip On

You can generate the file to a known location, and put a new Plug.Static call in your Endpoint:

plug Plug.Static, from: "my/custom/relative/dir"

If your worker is only specifying the relative priv directory, it is probably saving to a different directory than the one the release uses. Releases use the application's directory inside the release, rather than the working directory. You can ensure you save to the application's directory by using Application.app_dir/1:

:myapp
|> Application.app_dir()
|> Path.join("priv/static/sitemaps/sitemap1.xml")
|> File.write!(xml)

However, this is not a good idea because you're generating dynamic content at runtime, and injecting it into what is supposed to be a pre-compiled release, so I think the extra plug Plug.Static suggestion above is better. Or generate the content at compile-time, and include it in the release.