← All series

Series contents

Log in to track your progress

  1. 1. Simple Phoenix LiveView App: Setup Episode
  2. 2. Simple Phoenix LiveView App: Planning DB Schemas Episode
  3. 3. Simple Phoenix LiveView App: Upgrade to LiveView 0.5.1 Episode
  4. 4. Simple Phoenix LiveView App: Socket Session info, CSRF & .leex Episode
  5. 5. Simple Phoenix LiveView App: Scaffolding Users & Content Episode
  6. 6. Simple Phoenix LiveView App: PubSub & Index Episode
  7. 7. Simple Phoenix LIveView App: Show & Edit Pages Episode
  8. 8. Simple Phoenix LiveView App: Upgrade LiveView to v0.8.1 Episode
  9. 9. Simple Phoenix LiveView App: Fonts, logos and sass Episode
  10. 10. Simple Phoenix LiveView App: Markdown show notes Episode
  11. 11. Simple Phoenix LiveView App: The Podcast Listing Page Episode
  12. 12. Simple Phoenix LiveView App: Upgrade LiveView to v0.12.1 Episode
  13. 13. How I add auth to Phoenix LiveView apps Episode
  14. 14. Simple Phoenix LiveView App: planning comments Episode
  15. 15. Showing comment counts on the index page (Simple Phoenix LiveView App) Episode
  16. 16. Simple Phoenix LiveView app: Embedded comments form Episode
  17. 17. Passing redirect URLs + fragments Episode
  18. 18. Simple Phoenix LiveView App: Series Complete! Episode

Simple Phoenix LiveView App: Upgrade LiveView to v0.12.1

This is episode #13 of a Phoenix LiveView series.

This time we're upgrading LiveView from version 0.8.1 to 0.12.1, and getting both the main app and the throwaway /foo route working again.

Upgrading libraries

We'll start by updating our mix.exs file with the following two changes to our deps:

defp deps do
  [
    {:phoenix_live_view, "~> 0.12.1"}
  ]
end

Fix errors

After upgrading our dependencies, we can start up the server, but all LiveView functionality is broken and styles are missing! That's because app.html.eex now needs to be refactored into three files—root.html.eex which all templates will use, app.html.eex which traditional templates will use and live.html.eex which will be used by live templates.

The new layout templates

Begin by copying all of app.html.eex into a new file in the same directory called root.html.eex. Then in root.html.eex, replace the <main> tag with:

<%= @inner_content %>

Next delete everything except the main tag from app.html.eex, leaving only:

<main role="main" class="container">
  <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
  <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
  <%= render @view_module, @view_template, assigns %>
</main>

Finally copy this same code into live.html.leex, updating the <%= %> tags as follows:

<main role="main" class="container">
  <p class="alert alert-info" role="alert"><%= live_flash(@flash, :notice) %></p>
  <p class="alert alert-danger" role="alert"><%= live_flash(@flash, :error) %></p>
  <%= @inner_content %>
</main>

Note: in the video, I named this as a eex template and used a render instead of an @inner_content. It still worked, but the above is the recommended syntax.

In order to use the new root layout, we'll have to update the router:

pipe_through :browser
live "/users", UserLive.Index, layout: {ReactorWeb.LayoutView, :root}
live "/users/new", UserLive.New, layout: {ReactorWeb.LayoutView, :root}
live "/users/:id", UserLive.Show, layout: {ReactorWeb.LayoutView, :root}
live "/users/:id/edit", UserLive.Edit, layout: {ReactorWeb.LayoutView, :root}
# ...
live "/foo", FooLive, layout: {ReactorWeb.LayoutView, :root

Update the error_tag helper (optional)

In reactor_web/views/error_helpers.ex, rename the function "field_id" "input_id" to eliminate the deprecation warning.

  def error_tag(form, field) do
    Enum.map(Keyword.get_values(form.errors, field), fn error ->
      content_tag(:span, translate_error(error),
        class: "help-block",
        data: [phx_error_for: input_id(form, field)]
      )
    end)
  end

Fix the broken stylesheet link

Styling will now show up in the app but the LiveView functionality still won't work, due to a stylesheet import, of all things!

The live_view.css file we were including at the top of app.scss is now missing from Phoenix LiveView. It was handing the very thin blue bar at the top of the page that showed loading progress. We could just delete the dep, or in order to maintain the same functionality, we can install nprogress as follows:

yarn add -D assets nprogress

Then import it in app.scss:

@import "../node_modules/nprogress/nprogress.css"

Now all is working again!

(Source code available for premium members)

Open standalone page ↗

No Comments