Next.js Discord

Discord Forum

redirect inside server action inside a client component inside a server component not working

Answered
Californian posted this in #help-forum
Open in Discord
CalifornianOP
export default SomeServerPage() {

  async function handleSaveAction() {
    try {
      // do some API work
    } catch() {}
    
    // this throws error nor actually redirects
    redirect("/view");
  }


  return (
    <SomeClientComponent onSave={handleSaveAction} />
  )
}


I understand that redirect throws error with 303 status code but even if I ignore that the browser still does not redirect 😦
I made sure to take out the redirect out of try/catch, and etc etc..
Answered by Californian
don't know why but it started working.......
View full answer

24 Replies

@Californian export default SomeServerPage() { async function handleSaveAction() { try { // do some API work } catch() {} // this throws error nor actually redirects redirect("/view"); } return ( <SomeClientComponent onSave={handleSaveAction} /> ) } I understand that redirect throws error with 303 status code but even if I ignore that the browser still does not redirect 😦 I made sure to take out the redirect out of try/catch, and etc etc..
try adding use server inside handleSaveAction:

export default SomeServerPage() {

  async function handleSaveAction() {
+   "use server";
    try {
      // do some API work
    } catch() {}
    
    // this throws error nor actually redirects
    redirect("/view");
  }


  return (
    <SomeClientComponent onSave={handleSaveAction} />
  )
}
CalifornianOP
@joulev my bad for not including it in the example code, I already added the "use server"; 😢
interesting. what do you see? the redirect silently failing to happen, or is there an error showing up?
CalifornianOP
and redirect is not happening 😦
but looking at the code, it is meant to not work??
function getRedirectError(url, type, statusCode) {
    if (statusCode === void 0) statusCode = _redirectstatuscode.RedirectStatusCode.TemporaryRedirect;
    const error = new Error(REDIRECT_ERROR_CODE);
    error.digest = REDIRECT_ERROR_CODE + ";" + type + ";" + url + ";" + statusCode + ";";
    const requestStore = _requestasyncstorageexternal.requestAsyncStorage.getStore();
    if (requestStore) {
        error.mutableCookies = requestStore.mutableCookies;
    }
    return error;
}
function redirect(/** The URL to redirect to */ url, type) {
    if (type === void 0) type = "replace";
    const actionStore = _actionasyncstorageexternal.actionAsyncStorage.getStore();
    throw getRedirectError(url, type, // If we're in an action, we want to use a 303 redirect
    // as we don't want the POST request to follow the redirect,
    // as it could result in erroneous re-submissions.
    (actionStore == null ? void 0 : actionStore.isAction) ? _redirectstatuscode.RedirectStatusCode.SeeOther : _redirectstatuscode.RedirectStatusCode.TemporaryRedirect);
}
@Californian Click to see attachment
this means it has been caught somewhere for some reasons. could you make a minimal reproduction repo for me to check?
CalifornianOP
sounds gooood..! So, it is meant to work this way!
just not in my code,
@Californian sounds gooood..! So, it is meant to work this way!
yeah essentially redirect throw the NEXT_REDIRECT error that should not be caught at all. then when nextjs detects that there is a NEXT_REDIRECT, it will attempt the redirection
it showing up on the terminal like this means it has been caught somewhere preventing nextjs from redirecting
CalifornianOP
oh my bad, again, the console log of the NEXT_REDIRECT error is me try/catching the error and console logging for debugging
it is not caught in the real code
CalifornianOP
huh.......
CalifornianOP
don't know why but it started working.......
Answer
CalifornianOP
thanks code whisperer
hehe it sometimes do be like that. yeah your code looks completely fine so im not sure what could be wrong, perhaps some weird caching issues for nextjs
CalifornianOP
@joulev you have special buf aura skill?
Code Whisperer
Effects: buffs code execution successibility by 100%
lol i wish i could apply that skill on my own code
CalifornianOP
anyways Thank you! love ya ❤️
anyway now that you have fixed it, i'll leave this thread. have a good day
CalifornianOP
Thank you my man :)))