Next.js Discord

Discord Forum

revalidate and redirect not working in startTransition server actions

Unanswered
Ghost ant posted this in #help-forum
Open in Discord
Ghost antOP
Hi, i have a big pet project. I'm created an mtg card recognizer client and categorizing app. I. like to migrate the project into the nextjs but i'm hitting walls constantly with this app router. Most of the part of the app is on client so the rsc in not necessary but sometimes is felt so natural with solid start. But with next i'm basically struggle to create a reload after a mutation.

My start ocr button is like this:

const UploadedImage = async ({ image }: { image: ImageType }) => {
  const lines = await trpc.getImageLines({ id: image.id });

  return (
    <div className="card card-compact bg-neutral m-4 relative">
      ...
      <div className="card-body">
        <div className="flex flex-row justify-center gap-2">
          <OcrSubmitBtn imageId={image.id} />
        </div>
        <RecLines imageId={image.id} lines={lines} />
      </div>
    </div>
  );
};


The server action looks like this:
"use server";

import { trpc } from "@/trcp/trpcServer";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";

export default async function ocr(id: number) {
  await trpc.requestImageOcr({ id });
  revalidatePath("/digitalize");
  redirect("/digitalize");
}


And the caller:
"use client";

import { useTransition } from "react";
import startOcr from "../_actions/startOcr";

export default function OcrSubmitBtn({ imageId }: { imageId: number }) {
  const [pending, startTransition] = useTransition();

  const handleOcr = () => {
    startTransition(async () => {
      await startOcr(imageId);
    });
  };

  return (
    <button disabled={pending} onClick={handleOcr} className="btn btn-outline">
      ...
    </button>
  );
}


I'm using startTransition becuase even the documentation said i can use useFormState but i got a big error form webpack. How can i reload my component after finished the ocr process?

19 Replies

@Ghost ant Hi, i have a big pet project. I'm created an mtg card recognizer client and categorizing app. I. like to migrate the project into the nextjs but i'm hitting walls constantly with this app router. Most of the part of the app is on client so the rsc in not necessary but sometimes is felt so natural with solid start. But with next i'm basically struggle to create a reload after a mutation. My start ocr button is like this: TypeScript const UploadedImage = async ({ image }: { image: ImageType }) => { const lines = await trpc.getImageLines({ id: image.id }); return ( <div className="card card-compact bg-neutral m-4 relative"> ... <div className="card-body"> <div className="flex flex-row justify-center gap-2"> <OcrSubmitBtn imageId={image.id} /> </div> <RecLines imageId={image.id} lines={lines} /> </div> </div> ); }; The server action looks like this: TypeScript "use server"; import { trpc } from "@/trcp/trpcServer"; import { revalidatePath } from "next/cache"; import { redirect } from "next/navigation"; export default async function ocr(id: number) { await trpc.requestImageOcr({ id }); revalidatePath("/digitalize"); redirect("/digitalize"); } And the caller: TypeScript "use client"; import { useTransition } from "react"; import startOcr from "../_actions/startOcr"; export default function OcrSubmitBtn({ imageId }: { imageId: number }) { const [pending, startTransition] = useTransition(); const handleOcr = () => { startTransition(async () => { await startOcr(imageId); }); }; return ( <button disabled={pending} onClick={handleOcr} className="btn btn-outline"> ... </button> ); } I'm using startTransition becuase even the documentation said i can use useFormState but i got a big error form webpack. How can i reload my component after finished the ocr process?
The code looks okay other than the “big error from webpack”, what is this error
Ghost antOP
error - TypeError: (0 , react_domWEBPACK_IMPORTED_MODULE_1.experimental_useFormStatus) is not a function
Sorry, yeah it's the useFormStatus hook
Ghost antOP
13.5.6
I actually don’t even know what hook is available on what version anymore, they renamed experimental_useFormStatus to useFormStatus recently and it’s been messy ever since
Now try this: either of the two will work. Try both. Ignore typescript if it complains, just give a ts-expect-error if it does complain that the import doesn’t exist
And build and see which of the two webpack is happy with
Ghost antOP
Thanks, i will try but still. I just tried to get rid of the startTransition because i only need it because of the loading state. But even if i call directly from a <form action>. Still not refreshing or revalidating any of my data.
Ghost antOP
Yeah its run. If i manually refresh the page, the new data appears
Try

revalidatePath("/…", "page")
Ghost antOP
Still not working 😦 Even with router.refresh at startTransition
const handleOcr = () => {
    startTransition(async () => {
      await startOcr(imageId);
      router.refresh();
    });
  };
Weird
:SheaWeird:
Try going back a couple of versions
13.5.5 or 13.5.4 for example
Ghost antOP
Okay 🙂