Next.js Discord

Discord Forum

Revalidation pattern?

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
Is there a way to revalidate a layout page route from a client side component that is nested under that route? For example, I have a layout that holds a table of records and a dynamic route to view each of those records. The dynamic record route has a form that when submitted should cause the layout page to revalidate so when we navigate back to that route the table will have the updated record data. What I'm experiencing is that 'no-store' on the layout page request does not seem to work (perhaps it is due to soft/hard navigation) and revalidatePath does not do anything when invoked in the form's onSubmit, which I thought could be due to being invoked in a client side component.

Ex:
/Layout.tsx
export async function Layout() {
  const tableData = await fetch('table-endpoint', cache: 'no-store')

  return <Table data={tableData} /> // click on an item in the table to navigate to /[id] route
}

/[id].tsx
'use client'

export function Record({ data }) {
 const [form, setForm] = React.useState(data)

 const onSubmit = async () => {
  const newData = await fetch('update-endpoint') // PUT request
  setData(form) 
 }  

  return <Form onSubmit={onSubmit} />
}


After submitting the form, navigating back to Layout I expect to see the new data in the Table, but it will still display the data from before the update request was made. So, how do I revalidate the data in the table in Layout?

11 Replies

Layouts should have a lowercase name so change Layout.tsx to layout.tsx
Also, if you need to revalidate a path on-demand you can do it by making a fetch request to a revalidation endpoint as described here: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation
The problem is that you cannot really revalidate a specific dynamic route
For now, my solution consists in also tracking the state of the data locally (what we sometimes refer as optimistic updates)
So this avoids the need to completely refresh the page
This use case is just no covered so far a lot of people hit similar issues
The short solution is to do a hard refresh
check that too Tim Neutkens gave a few clarifications: https://github.com/vercel/next.js/discussions/54075
@not-milo.tsx Layouts should have a lowercase name so change `Layout.tsx` to `layout.tsx`
Spectacled bearOP
lol my bad that was a typo, it's actually lowercase in my codebase
Thanks for the suggestions, I'll read more on these