[Server-Actions] Navigate to different route on same origin
Unanswered
Egyptian Mau posted this in #help-forum
Egyptian MauOP
Is there any way to omit the router when using the redirect function in an server action? I have an route on the same origin that is not hosted by the NextJS app, however when I try it it seems to redirect me inside the app which then throws a not found
23 Replies
@Egyptian Mau Is there any way to omit the router when using the redirect function in an server action? I have an route on the same origin that is not hosted by the NextJS app, however when I try it it seems to redirect me inside the app which then throws a not found
Can you share your code? It should redirect to different origin if you prefix https:// or http://
Egyptian MauOP
I can't share my full code but I'll provide some snippets
https://local.ah.be was my origin derived from that header
allHeaders.get('origin'),
I noticed an origin header in our request that I tried to use initially.https://local.ah.be was my origin derived from that header
const redirectToPaymentFlow = (
payment: PaymentProcessFragment,
origin: string | null = null,
) => {
// Redirect to External Provider for 3DS Flow
if (payment.action?.__typename === 'CheckoutRedirectAction') {
redirect(payment.action.uri);
}
// On website 3DS flow callback
const baseCallbackUrl = `${WebEndpoint.CONFIRM}/callback?paymentData=blabla`;
const callbackUrl = origin
? new URL(baseCallbackUrl, origin)
: baseCallbackUrl;
redirect(callbackUrl.toString());
};
does that satisfy you enough? Just let me know if you need more
Egyptian MauOP
Do you mean this?
https://nextjs.org/docs/app/api-reference/functions/permanentRedirect
It should have been just redirect but I wanted to try if it mattered
https://nextjs.org/docs/app/api-reference/functions/permanentRedirect
It should have been just redirect but I wanted to try if it mattered
the result of callback url is https://localhost.ah.be/callback?paymentData=blabla
Can you try changing type
The second argument
It is a different url, so it’s should be replace
Egyptian MauOP
Tried it again, still the same result occurs
And just to let you know, server actions are only supposed to be used for mutations
you can use a route handler for this useCase
@Egyptian Mau ?
Egyptian MauOP
Sorry, Ive been on holiday for the last couple days, it’s basically a mutation
There is a submit button that submits the form, does an GQL call on the server
@Anay-208 | Ping in replies you can use a route handler for this useCase
Egyptian MauOP
Server side route handler?
Yes, route handlers are always server side
What I recommend is it can return the redirect URI in body and client can redirect
Egyptian MauOP
I can still do that indeed, I am also using useFormState for additional client handling
Egyptian MauOP
When looking at the nextjs code I think it can only work when the origin is different which is not the case
Given our whole site setup is slighty complex as we have multiple apps running over a proxy
Currently, you can’t use server action to do this, since there is no mutation