I want to redirect the Phoenix router to a specific page so “/” renders the first navigation item.
Currently I do this using a root page:
# router.ex
defmodule AppWeb.Router do
...
scope "/", AppWeb do
pipe_through :browser
live_session :navigation, on_mount: [AppWeb.Navigation] do
live "/", RootLive.Index
live "/comics", ComicLive.Index
live "/comics/new", ComicLive.New, :new
live "/comics/:id", ComicLive.Show, :edit
...
In RootLive.index
this happens:
defmodule StripeljeeWeb.RootLive.Index do
use StripeljeeWeb, :live_view
def handle_params(_unsigned_params, _uri, socket) do
{:noreply, socket |> redirect(to: "/comics")}
end
def render(assigns) do
~H"""
...
"""
end
end
This does work, but Dializer complains about this:
Type mismatch for @callback mount/3 in Phoenix.LiveView behaviour.
Expected type:
{:ok,
%Phoenix.LiveView.Socket{
:assigns => Phoenix.LiveView.Socket.assigns_not_in_socket() | map(),
:endpoint => atom(),
:fingerprints => {nil | binary(), map()},
:host_uri =>
:not_mounted_at_router
| %URI{
:authority => URI.authority(),
:fragment => nil | binary(),
:host => nil | binary(),
:path => nil | binary(),
:port => nil | char(),
:query => nil | binary(),
:scheme => nil | binary(),
:userinfo => nil | binary()
},
:id => binary(),
:parent_pid => nil | pid(),
:private => map(),
:redirected => nil | tuple(),
:root_pid => pid(),
:router => atom(),
:transport_pid => nil | pid(),
:view => atom()
}}
| {:ok,
%Phoenix.LiveView.Socket{
:assigns => Phoenix.LiveView.Socket.assigns_not_in_socket() | map(),
:endpoint => atom(),
:fingerprints => {nil | binary(), map()},
:host_uri =>
:not_mounted_at_router
| %URI{
:authority => URI.authority(),
:fragment => nil | binary(),
:host => nil | binary(),
:path => nil | binary(),
:port => nil | char(),
:query => nil | binary(),
:scheme => nil | binary(),
:userinfo => nil | binary()
},
:id => binary(),
:parent_pid => nil | pid(),
:private => map(),
:redirected => nil | tuple(),
:root_pid => pid(),
:router => atom(),
:transport_pid => nil | pid(),
:view => atom()
}, Keyword.t()}
Actual type:
{:noreply,
%Phoenix.LiveView.Socket{
:redirected => {:redirect, map()} | {:live, :patch | :redirect, map()},
_ => _
}}