Should `phx.new --umbrella` add live-reloading to 'data' app?

I’ve started an app with phx.new --umbrella and it took me a while to find out what was happening but live-reloading wasn’t working with the ‘data’ app. In hindsight this makes sense, as live-reloading is provided by phoenix. Is there a way for live-reloading to ‘watch’ the non web app for changes? As in initial development lots of the changes come from there, for instance - I migrate my schema, update it, then update a view which reloads the _web app but doesn’t have the change in Schema loaded to it crashes.

Thoughts on if this seems like something that should exist?

1 Like

Sure! Just update the paths you want it to watch in the config/dev.exs file. :slight_smile:

1 Like

Thanks! I’m looking at the GitHub page: https://github.com/phoenixframework/phoenix_live_reload and not seeing any info in configuring it. Can you point me to a page I can find that at? If you do, I’ll even create a pull request for the repo to add it to GitHub :smiley:

Rather post your generated config/dev.exs file. :slight_smile:

Got it! Here is the live-reload part:

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

Yep, just add whatever paths you want to watch more to the patterns in standard regex format just like the default 4 listed there. :slight_smile:

Is there an equivalent setting for the Phoenix CodeReloader?

Also I’m wondering if what you’re really interested in is the CodeReloader (which recompiles elixir code), vs the Phoenix LiveReload which causes a browser refresh on changes to templates and views.

1 Like

Hey @OvermindDL1,

I can’t seem to get this to actually work, I’m trying to add something as simple as ../ to it but it’s not compiling because of (probably) and invalid regex… but it seems valid to me.

      ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
      ~r{priv/gettext/.*(po)$},
      ~r{lib/myapp_web/views/.*(ex)$},
      ~r{lib/myapp_web/templates/.*(eex)$},
      ~r{\\.\\./myapp/lib/**/.*(ex)$} # When I add this line

I tried the below first, not thinking about the fact that periods have a specific meaning In regex:

      ~r{../myapp/lib/**/.*(ex)$} 

But it won’t even compile:

** (Mix.Config.LoadError) could not load config apps/myapp_web/config/dev.exs
** (Regex.CompileError) nothing to repeat at position 18

I’m pretty sure it’s valid regex. Any ideas, I’m sure it’s something simple.

Uh, I’m unsure what you are trying to find here…

Let’s see, you are trying to look in the main project directory for a directory named \.\. where the .'s can be anything at all, so something like \a\b would be a valid directory name that it is looking for, then inside that directory it is looking for a myapp directory, then inside that it is looking for a lib directory, then inside that it is looking for uh, I’m not even sure how ** would parse in regex? Pretty sure it wouldn’t? ^.^;

Remember that configs are run from the project ‘root’ directory, so are you wanting something like ~r{lib/myapp/.*(ex)} or so? Hard to tell without you giving specific paths of your setup or where things are located or so… ^.^;

Except ** is not valid regex, I’m not sure what you are trying to repeat there?

So I think @Jono might be confusing / merging two separate functionalities. Let’s get things straight:

Live Reload is the ability to auto-refresh the current page, opened in the browser, whenever it’s assets or HTML or view changes. It boils down to you making change to say HTML template, and the Phoenix.LiveReload module picks up this change, pushes the update to client via websocket and the client web site refreshes. This is handled the way you want and configured within dev.exs. Most likely you don’t want to change this setting as it only affects changes to HTML/JavaScript/assets and not other code like migrations, your models etc.

Code Reloading provided by Phoenix.CodeReloader Plug works differently. Whenever you make a HTTP request to the web site, it checks if some modules need re-compilation and if they do, it re-compiles and loads them before the request is handled. From what I understand, code reloading does not work for you.

Phoenix code reloading does support umbrella projects. So, provided you do have correct umbrella set up, changes to your data app should be picked up by the front-end during next request. If this is not happening there is some problem.

From what I recall, the code reloading only works if you start your web server from the umbrella root. So you have to go to project root directory and run mix phx.server or iex -S mix phx.server there. If you are running it from your apps/web, it might not work, and I think it is likely your case. Try that and let me know if this works.

2 Likes

That’s helpful, you are right about my trying to use live-reload for non-html. It was not codeReloading either, for instance I can refresh the page manually and any changes effected by the non _web app are not seen. Watching the logs I can confirm this wasn’t reloading.

After checking (as I use Docker), I am running phx.server from the _web directory. As soon as a switched it, the code would presumably reload as refreshing the webpage would show the changes. Thank you for that! I run it via script so I don’t often check it anymore.

Is there any way for CodeReloading to also trigger a reload of the HTML?

For that you might want to update the paths with config / live_reload/patterns as you are trying above, but I never tried it and uncertain if this will work.

1 Like

I do it, though not in an umbrella, it works fine. ^.^;

@OvermindDL1 do you ever have to traverse up a directory to get it to work?

Say I have the following applications in a non-umbrella situation

root_dir
  web_interface
  domain_app

So the web_interface has a path dependency on domain_app. If I wanted live reloading when an Elixir file in the domain_app changes what configuration do I need to set in the web_interface?

You should never need to, even in an umbrella as the configs are relative to the project root from my understanding (I don’t use umbrella’s though, rather I just make each thing it’s own distinct testable library/application).

Although that also depends on how Phoenix’s reloader is programmed too. ^.^;

I appreciate your help! As for testable library/application - the reason I use an umbrella is because to do any sort of ‘microservice’ the data needs to be owned by the app, also separation of concerns. My web layer shouldn’t be coupled to postgres.
It allows me to write other ‘apps’ that have their own data, that can use my App (non-web) through it’s API and I know that I’ve written it in a way where that is possible (since the web layer also has to do that).

2ndFearlessLeader Chris said, “If your context manages it’s own data, use an app (umbrella) if it doesn’t use a context”.

Religious war started! Jk, it’s all good either way.

:relaxed::sunglasses:

1 Like

Exactly, hence why I make everything in my setups application libraries instead of umbrella’s. No chance of accidental mixing then and they become even easier to test as I can mock any interface by just passing each set a different module name in the configs. :slight_smile:

Umbrella’s, on the other hand, make the git repo larger with a mixing of concerns that really shouldn’t be mixed on top of making it easier to accidentally cross-call.