Next.js Discord

Discord Forum

Server actions (revalidateTag/revalidatePath) vs traditional SPA data fetching

Unanswered
Palomino posted this in #help-forum
Open in Discord
Avatar
PalominoOP
I might be wrong but since revalidatePath/revalidateTag causes a rerender of the whole route on the server, isn't slower than the traditional spa where it only fetches the json data and nothing else?

139 Replies

Avatar
DirtyCajunRice | AppDir
revalidate tag does not rerender the entire route
if it did there wouldnt be much point of the the separation of those functions 😉
Avatar
Cimarrón Uruguayo
revalidate path does
They both invalidate the caches so the routes have to be rerendered after they refetch the data
I believe
Avatar
DirtyCajunRice | AppDir
you arent thinking deep enough. The docs cannot explain every possible scenario, and use top level fetches in pages as the example
additionally, ISR addresses a significant portion.
so how much it re-renders is fully dependent on how well you create your branch/leaf structure
Avatar
PalominoOP
ISR involves rerendering the routes at some interval or when the caches are invalidated which revalidateTag/Path does
Avatar
DirtyCajunRice | AppDir
ISR has nothing to do with intervals
you may choose to trigger ISR at an interval
but it itself has no concept of an interval
Avatar
PalominoOP
Yeah thats what I mean
Avatar
DirtyCajunRice | AppDir
then… what is the question lol
Avatar
PalominoOP
ISR doesn't help make revalidateTag/path more efficient
Avatar
DirtyCajunRice | AppDir
if you fetch in a server component that is nested, and you invalidate that tag, your page is only going to rerender that component
i do it in many components across many sites.
Avatar
PalominoOP
Wouldn't it need to rerender the whole route on the server to make a new entry for the Full Route Cache? (for static routes)
Avatar
DirtyCajunRice | AppDir
If i was building a static site i wouldnt be using nextjs
the primary benefit of nextjs is SSR.
Avatar
PalominoOP
SSR involves static routes
and static rendering
Avatar
DirtyCajunRice | AppDir
sigh
ok ill let you argue with someone else. i came in here to answer your question not debate nonsense
Avatar
PalominoOP
You didn't answer my question of why it is not slower than just getting pure json data
For example, it would need to rerender the whole route because it needs to be sent to the client no?
revalidateTag/Path invalidates the Router cache so all the RSC Payload is gone
Avatar
DirtyCajunRice | AppDir
client = call for everyone every time. cache is scoped to user.
server = call for everyone 1 time. cache is scoped to application.
if SPA stood for single person app yes it would be faster. But as a single page app, it is slower by scale.
Avatar
PalominoOP
RevalidateTag/Path would invalidate the cache
Avatar
DirtyCajunRice | AppDir
jesus. christ.
Avatar
PalominoOP
I'm listening but I think you are giving me the wrong info tbh
Avatar
DirtyCajunRice | AppDir
you arent listening at all
you have already decided what you think is right
and are dismissing literally everything else that doesnt fit what you have decided
Avatar
PalominoOP
Tell me where I'm wrong
Avatar
DirtyCajunRice | AppDir
at the beginning
if you have a page. and that page has data. and that data is cached for 1 day.
and 100 people go to it
it renders very quickly
except for the first person
then revalidate is called
the first person it is slow again
then it is fast for everyone again
because the cache is server side
revalidating has nothing to do with the client
the client gets a changed result, but is not the target
Avatar
PalominoOP
What if the data is personalised?
Avatar
DirtyCajunRice | AppDir
what does that matter
Avatar
PalominoOP
There is only the first person and no other person
Avatar
DirtyCajunRice | AppDir
the point of the cache is for subsequent calls
if you are going to show them new data every time why are you caching to begin with
Avatar
PalominoOP
Right you wouldn't even need to call revalidatePath/Tag
But it is still slower than the tradtional spa?
Avatar
DirtyCajunRice | AppDir
what are you talking about
how is it slower if you arent using it
are you conflating server fetching with server actions? they are not the same thing
Avatar
PalominoOP
So if you aren't caching data, the route uses dynamic rendering. Every time a user wants to create a new item, you would use a server action to create the item and then send back the new data.
Avatar
DirtyCajunRice | AppDir
no
well
depends
you can do it multiple ways.
the benefit of a server action in that specific case is the privacy of access to the data
creating a new todo means storing something in a database which the client shouldnt have access to.
Avatar
PalominoOP
So you would use a server action then?
Avatar
DirtyCajunRice | AppDir
to click create? yep. i wouldnt cache tho
not if the entire premise is live data
Avatar
PalominoOP
For example, I'm thinking about a user's todo. It is static data until they create/delete/update a todo
Avatar
DirtyCajunRice | AppDir
it is not faster to be pure json.
Avatar
PalominoOP
Why not?
Avatar
DirtyCajunRice | AppDir
because the user may look at their todos more than once ever in their life without modifying their todos
Avatar
PalominoOP
Yes that makes sense
Avatar
DirtyCajunRice | AppDir
this has literally nothing to do with the conversation.
and you are going to immediately make it veer off track
Avatar
Cimarrón Uruguayo
ok
Avatar
DirtyCajunRice | AppDir
to open and shut that irrelevant door, that would still be server side. He is trying to grasp why calling server at all is better than client.
Avatar
Cimarrón Uruguayo
i was under the assumption that we were thinking about server side calls
from here
Avatar
DirtyCajunRice | AppDir
:facepalm:
this thread alone may have used all the patience i have for the day
Avatar
PalominoOP
Server actions by itself would be slower than just getting json data but I guess the caching nextjs does makes everything else faster
Avatar
DirtyCajunRice | AppDir
you shouldnt be using server actions just to get json data in the first place
thats not what they are for
Avatar
PalominoOP
I know, they receive RSC payload when they renreder the server components but if it could just get json data while keeping the benefit of the caching on the serverside, it would be faster
Avatar
DirtyCajunRice | AppDir
what
Avatar
Cimarrón Uruguayo
RSC payload is irrelevant here
@Palomino traditionally, what you do is fetch your JSON data from the frontend and have an api handler that returns this data
With a server action, you basically mitigate this, and return the data directly in the same step
there is no separation of "fetching" and "providing" if that makes sense
Avatar
DirtyCajunRice | AppDir
still no
Avatar
Cimarrón Uruguayo
damn 💔
Avatar
DirtyCajunRice | AppDir
if you are just fetching data it should be done directly in a server compnent
no server action needed
at all
Avatar
Cimarrón Uruguayo
OK
let me clarify
sorry
include a database in there
Avatar
DirtyCajunRice | AppDir
still no
Avatar
PalominoOP
What don't you understand?
Avatar
DirtyCajunRice | AppDir
you still fetch it in a server component,
How that sentence had any relevance or accuracy at all
Avatar
Cimarrón Uruguayo
no as in, in an SPA you'd have the api handler GET() call db.get(query) and return (json data), and on the client side return the fetched json
whereas with server action you don't need fetch
just call db.get directly
and use that data
at least that's my understanding of use case
Avatar
DirtyCajunRice | AppDir
thats not true
you are calling post
just like with an api route
Avatar
Cimarrón Uruguayo
ok i give up i should actually leave
my bad
Avatar
DirtyCajunRice | AppDir
server actions are simply purpose built slimmed down and isolated route handlers.
Avatar
Cimarrón Uruguayo
yeah sorry my bad
Avatar
DirtyCajunRice | AppDir
with the caviat that they are meant for MUTATION based api calls
Avatar
PalominoOP
Server actions (revalidatePath):
1. client makes request to server
2. update DB
3. invalidate cache
4. Turn server components to RSC Payload
5. Respond with RSC to client
yes?
Avatar
DirtyCajunRice | AppDir
no
4 and 5 are wrong
and for extreme clarity, 2 and 3 arent server actions either
they are just things you may call in one
Avatar
PalominoOP
Ok what is your version?
Avatar
DirtyCajunRice | AppDir
its not my version
its just how they work
Avatar
PalominoOP
Like give me your step by step of how server actions work
you need to read the docs
Avatar
PalominoOP
I have read it
It doesn't say
Can you specify?
section
Avatar
DirtyCajunRice | AppDir
Image
massive main section
not 20% of the way down
i think you skimmed it
Avatar
PalominoOP
This isn't relevant?
Avatar
DirtyCajunRice | AppDir
re word this or my patience is gone and someone else can baby step you through this.
Avatar
PalominoOP
This isn't relevant.
Avatar
DirtyCajunRice | AppDir
Ill leave you with a thought. When you want to learn something, its smarter ask questions than make statements, as argumentative, stubborn, and ignorant of the topic all at once wont get many people to help you.
Avatar
PalominoOP
Thanks for explaning how it is relevant