Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revalidatePath in server action is not revalidating the path(s) #50356

Closed
1 task done
Abyrd9 opened this issue May 25, 2023 · 10 comments
Closed
1 task done

revalidatePath in server action is not revalidating the path(s) #50356

Abyrd9 opened this issue May 25, 2023 · 10 comments
Labels
bug Issue was opened via the bug report template. locked

Comments

@Abyrd9
Copy link

Abyrd9 commented May 25, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Home
    Binaries:
      Node: 18.16.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.4.4-canary.13
      eslint-config-next: 13.4.3
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which area(s) of Next.js are affected? (leave empty if unsure)

app dir
server actions
revalidatePath

Link to the code that reproduces this issue or a replay of the bug

https://github.com/Abyrd9/nextjs-revalidate-path-bug

To Reproduce

Sorry that there's db setups involved but I wasn't sure how else to get the reproduction I wanted.

  1. Create new supabase project
  2. Create new table called "posts" and a new columns called "name" with type "text"
  3. In project settings copy the "Project URL" and the "Project API keys" (anon public)
  4. create a local.env file and add the url and the api key:
SUPABASE_URL=https://{supabase-db-url}.supabase.co
SUPABASE_KEY={supabase-key}

npm run dev and navigate to http://localhost:3000/

Click the "Add New" button and then type in any sort of text and submit it.

Describe the Bug

When calling a server action that is running revalidatePath on a specific path, I would expect that path to re-run it's queries and return with fresh data. Instead if stays cached.

When submitting the form (located in /src/components/dialog.tsx) we call a server action (located in /src/components/dialog.actions.ts) in which we run the revalidatePath function on the paths / (base path) and /?create=true (dialog form path), but the neither the base path nor the dialog path revalidate, they both keep the stale data unless the default cache time runs out or you hard refresh.

Expected Behavior

When Submitting the form on the /?create=true url, the base url / and the dialog url /?create=true should revalidate and run their queries again to get the newly created post.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@Abyrd9 Abyrd9 added the bug Issue was opened via the bug report template. label May 25, 2023
@Comi9
Copy link

Comi9 commented May 31, 2023

To benefit from the path revalidation, I believe you have to use a regular function and not an arrow function for your server actions.

export async function submit(data: FormData) {
  await supabase.from("posts").insert([{ name: data?.get("note") }])

  revalidatePath('/')
}

@Abyrd9
Copy link
Author

Abyrd9 commented Jun 2, 2023

To benefit from the path revalidation, I believe you have to use a regular function and not an arrow function for your server actions.

export async function submit(data: FormData) {
  await supabase.from("posts").insert([{ name: data?.get("note") }])

  revalidatePath('/')
}

Even after changing to a regular function, the page(s) still will not revalidate.

@TranscendentalRide
Copy link

loading.tsx will not display on path passed to revalidatePath in 13.4.4, 13.4.3, or 13.4.2. But it did work in 13.4.1 and 13.4.0

@latinrev
Copy link

latinrev commented Jun 8, 2023

I just figured out my issue with revalidate path using MongoDB, it was rather silly but perhaps it may help you
I had
Action

  async function saveNewAnime(data: FormData) {
    "use server";
    let newAnime = Object.fromEntries(data.entries()) as anime;
    createAnime(newAnime);
    revalidatePath("/");
  }

Database action

export const createAnime = async (animeData: anime) => {
    const newAnime = deleteActionField(animeData) as anime
    await client.anime.create({ data: { ...newAnime } })
}

my mistake was that I forgot to await my
createAnime(newAnime);
changing it to
await createAnime(newAnime);
made it work

So, I'm guessing perhaps revalidatePath("/") is being triggered before awaiting for
await supabase.from("posts").insert([{ name: data?.get("note") }])

@Abyrd9
Copy link
Author

Abyrd9 commented Jun 8, 2023

@latinrev In the repo I shared above I'm awaiting on line 10

I think the problem is different because it seems like revalidatePath is revalidating the path it's currently on rather than the path that I'm passing it.

So when I run the server action on the /?create=true path, I want to revalidate the / path, but only the /?create=true path is being invalidated even when I'm passing in a different path.

@shuding
Copy link
Member

shuding commented Jun 8, 2023

#50848 should fix this issue, you can test again once a new canary release is out, thanks!

@shuding
Copy link
Member

shuding commented Jun 8, 2023

Should be included in next@13.4.5-canary.8, let me know if it's fixed!

@Abyrd9
Copy link
Author

Abyrd9 commented Jun 11, 2023

@shuding Sorry for not getting back, I didn't see the "let me know if it's fixed".

It is fixed and working great 👍🏻

@shuding shuding closed this as completed Jun 11, 2023
@havgry
Copy link

havgry commented Jun 14, 2023

Just confirming it now works with revalidateTag as well.

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

No branches or pull requests

6 participants