You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is known that awaiting for revalidatePath requires some trickery and that you need to use client components if you want to provide the user a loading indicator when server actions and revalidation is occurring. What if page.js|ts default export Components would have a loading prop passed to them when a server action is executing or a revalidation is in progress? This would greatly improve the amount of code needed to achieve a very common pattern: Mutate data on the server and showing loading information to the user until new data is available.
exportdefaultasyncfunctionPage({loading}){// <-- loading is true while any server action/revalidation is running constitems=awaitgetItemsFromSomewhere();constaction=async()=>{"use server";awaitmutateItems();revalidatePath("/path/of/this/page");};return<div><ulclassName={loading ? "loading" : ""}>{items.map(item=><likey={item.id}>{item.description}</li>)}</ul><formaction={action}><button>Mutate items</button></form></div>}
Going even further, server actions could be assigned ids and loading prop would be an array of ids with the current running actions, which applied to the above snippet would become
constmutateItemsActionId="some-custom-id";exportdefaultasyncfunctionPage({loading}){// <-- loading is an array of stringsconstitems=awaitgetItemsFromSomewhere();constaction=async(data,context)=>{"use server";context.setId(mutateItemsActionId);awaitmutateItems();revalidatePath("/path/of/this/page");};return<div><ulclassName={loading.includes(mutateItemsActionId) ? "loading" : ""}>{items.map(item=><likey={item.id}>{item.description}</li>)}</ul><formaction={action}><button>Mutate items</button></form></div>}
This would allow for very fine grained control over server actions feedback, and reduce the need for client components while providing a great DX. I have not enough knowledge of nextjs's source code to tell if this is even feasible though.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
It is known that awaiting for
revalidatePath
requires some trickery and that you need to use client components if you want to provide the user a loading indicator when server actions and revalidation is occurring. What ifpage.js|ts
default export Components would have aloading
prop passed to them when a server action is executing or a revalidation is in progress? This would greatly improve the amount of code needed to achieve a very common pattern: Mutate data on the server and showing loading information to the user until new data is available.Going even further, server actions could be assigned ids and
loading
prop would be an array of ids with the current running actions, which applied to the above snippet would becomeThis would allow for very fine grained control over server actions feedback, and reduce the need for client components while providing a great DX. I have not enough knowledge of nextjs's source code to tell if this is even feasible though.
Beta Was this translation helpful? Give feedback.
All reactions