Next.js Discord

Discord Forum

{ redirect, permanentRedirect } from "next/navigation" sometimes throw errors for no reason

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I am interested in migrating some CRUD things from Route Handler to Server Actions that would be triggered as form actions.

Currently I am working on an action that triggers when a form is submitted inside the internal dashboard with a route that contains a dynamic segment: /admin/blob/[slug]/edit. My Server Action validates the JWT that is stored in cookies; if the JWT and its payload are valid then appropriate db interactions, analytics, and other stuff happens, if not, the user is redirected to /, because if JWT is invalid, they wouldn't have access to the admin dashboard anyway. All this works as expected and looks something like this:

const updatePost = async (blobFormData: FormData) => {
  // the checkRole function verifies the JWT and returns a boolean
  const permittedToUpdateBlob = await checkRole(cookies().get("access_token")?.value, "moderator");

  if (!permittedToUpdatePost) redirect("/");

  //...
}


Later in the function, I am checking if the name of the blob in question changed, because that affects the [slug]. In case the name changed I want to redirect the user to /admin/blob/[new slug]/edit and I am doing it like this:
  //...
  if (prevBlobData.slug !== serializedBlobFormData.slug) {
    const newPath = `/admin/post/${serializedBlobFormData.slug}/edit`; 

    revalidatePath(newPath);
    redirect(newPath);
  }

For some reason in this case it decides to throw an error:
https://gist.github.com/MISTCLICK/c726a3068af7490a3987855a7e6518fb

I don't really get, what this error actually means because of the stack-trace information being quite limited. Searching on the web didn't help out too, so I decided to try to find luck here. Thanks to anyone willing to help in advance!
Answered by West African Lion
Disregard. I've been sitting on this issue since early afternoon today and finally solved. I thought to go over the docs just one last time and noticed this:
Invoking the redirect() function throws a NEXT_REDIRECT error and terminates rendering of the route segment in which it was thrown.
Only then I realised that the part of my function where I was redirecting was wrapped in a trycatch block that literally cancels the redirect. :hidethepain:

I am happy and in pain from personal stupidity at the same time. Thanks for your effort to help me!
View full answer

6 Replies

You should manage your authentication checks within middleware - that way it gets applied to every matching path (including server actions)
@Marchy You should manage your authentication checks within middleware - that way it gets applied to every matching path (including server actions)
West African LionOP
I am managing auth with middleware, however based on my testing it doesn't apply to Server Actions. If you could pinpoint the information about that in the docs, that would help.

Secondly, that's not really my problem. My problem is that in one case redirect works and in another it does not. That is what I am curious to figure out.
  if (prevBlobData.slug !== serializedBlobFormData.slug) {
    const newPath = `/admin/post/${serializedBlobFormData.slug}/edit`; 

    revalidatePath(newPath);
    redirect(newPath);
  }


Could you try this without the dynamic slug?
I'm suspecting something is happening with serializedBlobFormData
West African LionOP
Disregard. I've been sitting on this issue since early afternoon today and finally solved. I thought to go over the docs just one last time and noticed this:
Invoking the redirect() function throws a NEXT_REDIRECT error and terminates rendering of the route segment in which it was thrown.
Only then I realised that the part of my function where I was redirecting was wrapped in a trycatch block that literally cancels the redirect. :hidethepain:

I am happy and in pain from personal stupidity at the same time. Thanks for your effort to help me!
Answer