Redirect not working in API route
Answered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
I want to navigate to external url when a button is clicked. And the URL data is provided from client side to api route. But api route is unable to navigate
page.jsx
const handleClick = async (e, productURL) => {
setLoading(true);
try {
const response = await fetch(${process.env.NEXT_PUBLIC_BASE_URL}/api/getUrl,{
method: "POST",
mode: 'no-cors',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ productURL: productURL}),
credentials: 'include',
redirect: 'follow'
});
} catch (error) {
console.log(error);
setLoading(false);
} finally {
setLoading(false);
}
};
api/getUrl/route.js
export async function POST(request) {
try {
const { productURL} = await request.json();
console.log("productURL->", productURL);
return NextResponse.redirect("https://www.google.com/", 302); // just checking if it navigates to google
} catch (error) {
console.log(error);
return NextResponse.json({ message: JSON.stringify('Some Issue!')}, { status: 400 });
}
}5 Replies
Asiatic LionOP
@joulev Hey, I used first router.push but it's giving warning in dev console that
Listener added for a 'DOMNodeInserted' mutation event. This event type is deprecated, and will be removed from this browser very soon. Usage of this event listener will cause performance issues today, and represents a large risk of future site breakage. Consider using MutationObserver instead. See https://chromestatus.com/feature/5083947249172480 for more informationnever seen this, i dont know, sorry
Asiatic LionOP
😭😭 can you give a tutorial kind of how to do this with server action?