View Source PlugLiveReload (plug_live_reload v0.2.0)

Plug for live-reload detection in development. Specifically, this plug injects some Javascript at the bottom of each HTML page that listens to websocket events from PlugLiveReload.Socket. That Javascript will update the page.

Usage

Add the PlugLiveReload plug to your router. E.g.,

defmodule MyApp.Router do
  use Plug.Router

  if Mix.env() == :dev do
    plug PlugLiveReload
  end

  plug :match
  plug :dispatch

  get "/" do
    conn
    |> put_resp_content_type("text/html")
    |> send_resp(200, "<html><body><h1>Plug</h1></body></html>")
  end
end

This plug will only inject the Javascript if the content type of the response is text/html. This can be done with Plug.Conn.put_resp_content_type/3, as shown above.

Additionally, this plug will only inject Javascript if the HTML response has a <body> tag.

Configuration

This plug is configured via opts passed to the plug. E.g.,

plug PlugLiveReload, target_window: :top

The following options are supported:

  • :iframe_attrs - attrs to be given to the iframe injected by live reload. Expects a keyword list of atom keys and string values.

  • :target_window - the window that will be reloaded, as an atom. Valid values are :top and :parent. An invalid value will default to :top.

Additionally, one can disable the plug via an application config. This is useful for disabling it dynamically when running a Mix task, for example. This is typically not needed, as it is preferred to disable it via a Mix.env() guard check in your router, instead.

Application.put_env(:plug_live_reload, :disable_plug, true)

Summary

Functions

Callback implementation for Plug.call/2.

Callback implementation for Plug.init/1.

Functions

Callback implementation for Plug.call/2.

Callback implementation for Plug.init/1.