Liveview - rerendering nonstop (keeps making api request)

Trying to load user data from the reddit api which is working. However, on mount it appears to be stuck in a rendering loop repeatedly making requests to the api. This causes it to error out and redirect to ‘/’. This is what I have below so far… running latest versions of phoenix and elixir 1.10.0. using Tesla for the api requests. i have also tried moving this logic to handle_params instead but does same thing. Any ideas why this is happening? I thought maybe it was making the call twice possibly since it connects to socket twice on initial mount but doesn’t seem to be it. Is it possible this might be due to the way I implemented my api requests?

Other weird thing is that temporary_assigns will not work either…i get an @saves not available in eex template error. I need this to work b/c plan is to append additional items to list w/ “load more” button using phx-update.

Any help appreciated. been digging at this all day and getting nowhere. Also, if there’s a better/cleaner way do this please let me know. a bit green.

def mount(params, _session, socket) do
      case Reddit.request_token(params["code"]) do
        "invalid_grant" ->
          {:ok,  
            socket
            |> put_flash(:error, "Token Invalid!")
            |> redirect(to: "/")
          }

        token ->
          {:ok, response} = Reddit.get_user_saves(token)
          {:ok, assign(socket, :saves, response.body)}
      end
end

Hi, have you tried using connected? function since liveview is mounted twice.

def mount(params, _session, socket) do
  if connected?(socket) do
    case Reddit.request_token(params["code"]) do
     "invalid_grant" ->
       {:ok,  
         socket
         |> put_flash(:error, "Token Invalid!")
         |> redirect(to: "/")
       }

      token ->
       {:ok, response} = Reddit.get_user_saves(token)
       {:ok, assign(socket, :saves, response.body)}
    end
  end
end

I have but, still doesn’t work correctly.

Could you maybe share what kind of logging messages are shown in the console that is running Phoenix while someone connects to it?

That would make it maybe easier to help with debugging. Even better would be if you could create a minimal example application that has the same problem, but of course that might take some more effort.

The logging is just reddit returning a json object w/ token: “invalid_grant” instead of the auth code. This is happening b/c the initial request for the token is sent w/ a one-time use code. so on the repeat request it’s returning “invalid grant” for the token.

behavior of the app is basically, initial request occurs and i see the data load on the page as it should and then within a second it redirects to my homepage based on the first case b/c it’s trying to request a token again w/ the expired code.

i am going to try this w/ another api that just needs api key and not oauth authorization and see if it causes the same issue or not.

I have implemented previously not using liveview and had no issues.

Your mount code will always run twice. See the docs about LiveView lifecycle. The advice about is_connected? was on the right track I think.

Tried running the reddit api code inside connected? again. it will run the request only once but it does not update the page w/ data from the request. IO.inspect confirms the request data being grabbed though.

I also built a quick liveview using another api(one that doesn’t require a key or auth and I do not have a problem at all. makes one request, loads the data on the page and that’s it. did not need to check if connected first or anything. HOWEVER, if i run this inside connected? conditional it does the same behavior as reddit api above. does not update page w/ data from request…

Looking at the docs it seems like all my request logic should be in handle_info and that would get triggered by the connected? conditional and then should update the page… Tried this too and technically it does update the data but, it also loops requests. maybe i am triggering it incorrectly though.

I’m wondering if reddit api request is doing weird stuff since it’s doing a token request then an auth/data request and then redirecting back to my uri. and this is maybe triggering renders? Maybe I need to bury the call for the token request inside the api wrapper?

Wanted to give an update on this:

I moved the case for the token validation into the api wrapper so there was only one call inside mount/3. Still caused the same problems. So I ended up just putting the api request in a regular controller then, called live_render/3 and passed my data through to the liveview w/ session%{}.