Broken digested asset URLs on Heroku

I want to deploy my Phoenix application to 2 different Heroku apps (production and staging). I’m treating both deployments as “production”, so I’m using the same prod.exs config file, with environment variables for the database configuration.

Since the URL host will be different depending on the app, I tried using an environment variable for the host URL:

# prod.exs
config :testapp, TestApp.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [scheme: "https", host: {:system, "HOST"}, port: 443],
  cache_static_manifest: "priv/static/manifest.json",
  force_ssl: [rewrite_on: [:x_forwarded_proto]],
  secret_key_base: System.get_env("SECRET_KEY_BASE")

On Heroku, I added HOST with the URL (e.g. someapp-staging.herokuapp.com) respectively.

However, when I deployed the app, I lost the digested hash in my asset URLs, so instead of /js/app-3a4848cba008b5940de449d92db3ff33.js?vsn=d it started pointing to /js/app.js. I’m using the elixir buildpack, followed by the phoenix static assets buildpack.

I’m new to Elixir so I’m not sure if this is a bug or I’m missing something in the configuration? Or is there a better way to achieve what I’m trying to do? Thanks a lot!

2 Likes

I forgot to mention that I’m also exposing the HOST variable in my buildpack.config files, like so

config_vars_to_export=(DATABASE_URL HOST)

Although that’s for adding the Env var during compile time; my understanding is that the prod.exs file is used at run time, so all Heroku environment variables should be accessible?

2 Likes

@svilen I assume you’re using the Hashnuke buildpack and following the directions in the readme?

The trick is that compile step and finishing it off with mix phoenix.digest which will add the digest on your assets for you. I’m doing something slightly different from what you are in that I’m having to consume the digested asset from another app entirely, so I basically have one action that sends a js file that then loads the digested js, but in your case, you should be able to just follow the instructions in the buildpack and get there.

2 Likes

@karmajunkie Thank you for the taking the time to reply! Yes, I’m using hashnuke’s and gjaldon’s buildpacks. I do have a compile file as per instructions, which is only slightly different for me (I have an npm script that runs Sass and webpack):

info "Building Phoenix static assets"
npm run build
mix phoenix.digest

So I do get hashed URLs for my assets, but they disappear when I use the environment variable HOST which I set myself. I’m not touching MIX_ENV, so I can confirm that this is still set to prod when I deploy.

1 Like

yeah, that’s a bit of a headscratcher… I’m essentially set up the same way you are, and using static_path gets me the digested url. One thing you might try is making sure you don’t have any of these static assets in priv/static checked into source code so that they are definitely not part of the problem.

Hey @svilen I am having the same issue. Did you ever find out what the reason was?

Hi @ospaarmann ! I think I did, but honestly, I cannot remember what it was :sweat_smile: I can see I asked this question in May 2016 and my memory is not so great… Sorry I can’t help much :frowning_face: