Search documentation of Phoenix

# Phoenix.CodeReloader (Phoenix v1.8.7)

View Source

A plug and module to handle automatic code reloading.

To avoid race conditions, all code reloads are funneled through a sequential call operation.

## Summary

### Functions

call(conn, opts)

API used by Plug to invoke the code reloader on every request.

init(opts)

API used by Plug to start the code reloader.

reload(endpoint, opts \\ [])

Reloads code for the current Mix project by invoking the `:reloadable_compilers` on the list of `:reloadable_apps`.

reload!(endpoint, opts)

Same as `reload/1` but it will raise if Mix is not available.

sync()

Synchronizes with the code server if it is alive.

## Functions

# call(conn, opts)

API used by Plug to invoke the code reloader on every request.

# init(opts)

API used by Plug to start the code reloader.

# reload(endpoint, opts \\ [])

    @spec reload(
      module(),
      keyword()
    ) :: :ok | {:error, binary()}

Reloads code for the current Mix project by invoking the `:reloadable_compilers` on the list of `:reloadable_apps`.

This is configured in your application environment like:

    config :your_app, YourAppWeb.Endpoint,
      reloadable_compilers: [:gettext, :elixir],
      reloadable_apps: [:ui, :backend]

Keep in mind `:reloadable_compilers` must be a subset of the `:compilers` specified in `project/0` in your `mix.exs`.

The `:reloadable_apps` defaults to `nil`. In such case default behaviour is to reload the current project if it consists of a single app, or all applications within an umbrella project. You can set `:reloadable_apps` to a subset of default applications to reload only some of them, an empty list - to effectively disable the code reloader, or include external applications from library dependencies.

This function is a no-op and returns `:ok` if Mix is not available.

The reloader should also be configured as a Mix listener in project's mix.exs file (since Elixir v1.18):

    def project do
      [
        ...,
        listeners: [Phoenix.CodeReloader]
      ]
    end

This way the reloader can notice whenever the project is compiled concurrently.

## Options

- `:reloadable_args` - additional CLI args to pass to the compiler tasks. Defaults to `["--no-all-warnings"]` so only warnings related to the files being compiled are printed

# reload!(endpoint, opts)

    @spec reload!(
      module(),
      keyword()
    ) :: :ok | {:error, binary()}

Same as `reload/1` but it will raise if Mix is not available.

# sync()

    @spec sync() :: :ok

Synchronizes with the code server if it is alive.

It returns `:ok`. If it is not running, it also returns `:ok`.