Search code examples
reactjsreact-hooksnext.jsapolloapollo-client

Propery Way to Redirect Automatic in NextJS


I have an index page and I wanted to redirect to /orders page when he gets into the index page. What is the proper and correct way to do this? I'm using NextJS, Apollo Client and React.

import { useRouter } from 'next/router'
import withApollo from '../apollo'

const Index = () => {
  const router = useRouter()
  return router.replace('/orders')
}

export default withApollo(Index)

Solution

  • For redirect in Nextjs, there is a setting in next.config.js you would need to add:

    You will need to add a redirects key to the settings:

    module.exports = {
      async redirects() {
        return [
          {
            source: '/',
            destination: '/orders',
            permanent: true,
          },
        ]
      },
    }