Redirect from part

I have a <form> in a part that needs to return a redirect (status 303) on success to move to the next <form> in a series.

The way I hacked it solved it, was to return the url I wanted to redirect to as part of the header:

return {
  header: {
    "x-redirect": myUrlToRedirectTo
  }
}

And use a response processor to change the response:

export function responseProcessor(req, res) {
  const redirectUrl = res.headers?.["x-redirect"];
  return (redirectUrl !== undefined)
    ? {
        ...res,
        status: 303,
        redirect: redirectUrl
      }
    : res;
}

This technique can of course also be used to let a part decide which http status code to use. Just add the wanted status code in header, and parse in the filter like above.

Maybe serve up a nice 400 if a form has errors.


Now smash that :hearts:-button if this helped you! :arrow_down:

2 Likes