简体   繁体   中英

Phoenix framework - assets don't update without running mix phx.digest

After changing an asset (a css or js) file I see in the logs that the change was noticed and compiled, and the browser also auto-reloads.

[debug] Live reload: priv/static/js/app.js
10:53:15 - info: compiled MyComponent.jsx and 2095 cached files into 2 
files in 2.3 sec

However, it doesn't appear that the assets in /priv/static were actually updated. I can only see my change in the browser once I run mix phx.digest , and hard refresh the browser.

Any ideas on how to troubleshoot this?


Using:

  • Phoenix 1.3
  • brunch 2.10.7

config/dev.exs:

config :my_app, MyApp.Web.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
                cd: Path.expand("../assets", __DIR__)]]

# Watch static and templates for browser reloading.
config :my_app, MyApp.Web.Endpoint,
  live_reload: [
    patterns: [
      ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
      ~r{priv/gettext/.*(po)$},
      ~r{lib/my_app/web/views/.*(ex)$},
      ~r{lib/my_app/web/templates/.*(eex)$}
    ]
  ]

TL;DR — If you don't set the cache_static_manifest setting on your endpoint, it won't generate versioned URLs.

So, I know I'm about three years late here, but I recently figured this out. I discovered that merely setting the cache_static_manifest value in the Endpoint config will cause it to use the digest in any mode. (This is documented, but not in a way that seemed particularly clear to me.)

Now, you might be thinking "But I didn't set that in dev mode." I thought that, too, until I realized that I had written a naive config/runtime.exs .

At the time, I had been focused on configuring things a runtime when running a release, but completely forgot that it configures things even when not running in a release. Once I made it conditional, everything was fine.

Example:

if Config.config_env == :production do
  config :my_app, MyAppWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
end

I ran into the same issue and for me it helped to manually remove the static folder with rm -rf priv/static and to restart the server with mix phx.server . Afterwards the hot reloading worked without having to manually run mix phx.digest all the time.

Another possible cause is that the Endpoint module ( lib/my_app_web/endpoint.ex ) is setting Plug.Static to use compressed assets:

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app_web

  plug Plug.Static,
    # ...
    gzip: true,

Then, if a release has been built from within the project directory and the gzipped assets are still present when developing, they will be served instead of the newly-saved, non-compressed assets.

To avoid this:

config/dev.exs :

config :my_app, :environment, :dev

config/test.exs :

config :my_app, :environment, :test

config/prod.exs :

config :my_app, :environment, :prod

lib/my_app_web/endpoint.ex :

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app_web

  in_prod = Application.get_env(:my_app, :environment) == :prod

  plug Plug.Static,
    # ...
    gzip: in_prod,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM