Je Suis Un Ami
Explore posts from serversHow to Execute Server Action on Each Route Visit?
So, for clarification, suppose this scenario. The user visits the home route where that loader is called (initially). The effect runs, the user isn’t authenticated. So, it redirects the user to the login route. After authenticating, the login route redirects the user back to the home route via
navigate(“/“)
(meaning, the home route is visited a second time). Will the loader run again on that second visit?
My initial solution was to use a data loader. However, it wasn’t running in subsequent visits to the home route. So, I switched to an action.16 replies
How to Execute Server Action on Each Route Visit?
From the Data Loading section of the docs. Under caveats. “The load function is called once per route, which is the first time the user comes to that route. Following that, the fine-grained resources that remain alive synchronize with state/url changes to refetch data when needed. If the data needs a refresh, the refetch function returned in the createResource can be used.”.
I need the loader to run every single time the route is visited, not just on the first load.
useResource()
is client-side only, meaning I cannot access the server session to get auth tokens to check if the user is authenticated (or do anything requiring access to the session for that matter).
This is why I opted to use an Action. With action, my thinking was I can call it so it loads every time. At least, that was the idea.16 replies
How to Execute Server Action on Each Route Visit?
These examples use data loaders. The reason I am using
action()
is because Data loaders only load on the server once then rely on useResource()
to refresh subsequent data. Since I am using a server session, I opted to use Actions instead.
In short: No access to useSession() if I were to do this on a createResource()16 replies
How to Execute Server Action on Each Route Visit?
For clarification: The action is being executed on the initial page load. However, it isn’t being executed in subsequent visits to the route. Like if you directly enter “my site.com” this route would load and the action would execute. However, if you
navigate(“/“)
from some other component, the action doesn’t execute.
What I am trying to accomplish is to get the action to execute event when the route is loaded via a navigate()
call from some other part of my app.16 replies
Server Actions and Cookies sending error: Cannot set headers after they are sent to the clien
Just in case someone else is reading this with the same issue. It seems this issue only arises in actions involving SSR.
For server actions invoked via form action, this solution is not necessary.
At least as far as I can tell. My authentication form action seems to work just fine
29 replies
Server Actions and Cookies sending error: Cannot set headers after they are sent to the clien
Yes. This is using SSR. I scattered some logs around the action. And it seems the error occurs during the
getUserSession()
call, which indicates I am experiencing the same issue as you linked.
So, I guess the solution to this error would be to do the checks from within a middleware instead of an action.
Let me try that out really quick.29 replies
Server Actions and Cookies sending error: Cannot set headers after they are sent to the clien
Okay. So it looks like the error is only being thrown whenever the session is being updated. So, my guess is the session is what is causing the error…
29 replies
Server Actions and Cookies sending error: Cannot set headers after they are sent to the clien
Let me test it if it with an action that just returns immediately. If this works, my best guess is the error is somehow in how I set up the getUserSession() function?
29 replies
Server Actions and Cookies sending error: Cannot set headers after they are sent to the clien
Nope. They just make a
fetch()
call and return the respective data (the authenticated user and the auth tokens respectively). Updating the session is happening in the call to user session.update() in the action function itself.
The only un-await-ed promise I’m seeing is the call to loadAuthenticatedUser()
action in the main component.
However, that’s the same thing that the example in the actions section in the docs is doing…29 replies