kokolegorille

kokolegorille

Hello everyone,

I am trying to implement live reload on a static page, for an API.

I build the project with --no-html --no-webpack, and added a bucklescript pipeline instead.

There is only one page, a static index.html, located in priv/static. I render with the following action

  def index(conn, _params) do
    conn
    |> put_resp_header("content-type", "text/html; charset=utf-8")
    |> IO.inspect()
    |> send_file(200, "priv/static/index.html")
  end

I added phoenix-live-reload, configured the api to use it… and now code is reloading when frontend files change. I can see bsb and webpack reloading in the background.

I have configured my Endpoint with live/code reload, and I can see in my connection when in development.

  before_send: [#Function<1.112466771/1 in Plug.Logger.call/2>, 
   #Function<0.66982185/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>],

But as it is an api only, there is no phoenix_html, I need to inject the live reload iframe manually into the static index.html.

Can somebody point me where (and when) Phoenix inject the live reload iframe? I find it hard to find in the source code.

Thanks for taking time

Showing Posts 1 to 3

kokolegorille

kokolegorille OP

Thank You for the response, but I read this and configured accordingly…

It does set the before_send in my connection correctly.

But I would like to know where this function is applied in reality (the plug just add a function in the before_send). As it is a static file, not an eex template, I will need to inject manually.

kokolegorille

kokolegorille OP

The code is in the live_reloader, as said by @dom, in the private functions…

At the very end, we can find

  defp before_send_inject_reloader(conn, endpoint) do
    register_before_send(conn, fn conn ->
      if conn.resp_body != nil && html?(conn) do
        resp_body = IO.iodata_to_binary(conn.resp_body)
        if has_body?(resp_body) and :code.is_loaded(endpoint) do
          [page | rest] = String.split(resp_body, "</body>")
          body = page <> reload_assets_tag(conn) <> Enum.join(["</body>" | rest], "")
          put_in conn.resp_body, body
        else
          conn
        end
      else
        conn
      end
    end)
  end

  defp reload_assets_tag(conn) do
    path = conn.private.phoenix_endpoint.path("/phoenix/live_reload/frame")
    """
    <iframe src="#{path}" style="display: none;"></iframe>
    """
  end

The solution is dead simple (once You get how it works), Not even the need to parse the static file, just render with.

  def index(conn, _params) do
    file = "priv/static/index.html"
    {:ok, binary} = File.read(file)
    html(conn, binary)
  end

The html helper will take care of this. Sorry for the question…

Now I have a slim api, with live reload enable on the static file.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews