Next.js Discord

Discord Forum

Implement 301 redirect from old url to new url fetched from endpoint

Unanswered
Daggertooth pike conger posted this in #help-forum
Open in Discord
Daggertooth pike congerOP
Hi guys, I need a help with implementation of permanent redirect on server side.
I have a gql endpoint that have old-url and new-url returned. App should fetch this endpoint and if old-url is entered, it should redirect to new-url.

Implemented this in this way.
First approach: in [...slug].tsx, getStaticPaths will prerender old-url pages. getStaticProps will determine if old-url is entered, it will return redirect 301. It works on dev env, but when I am trying to build it, having this error. "The redirect value was returned from getStaticProps during prerendering which is invalid."
Second approach: I made redirect from next.config.js, but it gave me several errors. Due to project sensitivity, can't iterate over details.

The app should work as SSG, so it should have getStaticPaths, getStaticProps as it is, it does many thing there originally. So, want to make it work maintaining the existing functionality. Could someone help? If something is not clear, feel free to comment and I will reply on the spot. 🙂

Thank you.

0 Replies