9

i am new in next.js, in next.js default page in index.js, i want to change its path as login , can anyone please help me how to do it ? right now i am using Router.push('/login'); but it is creating flickering issue, can anyone please help for this issue ?

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import Router from 'next/router'
import React, { useEffect } from "react";
import { Redirect } from 'react-router';


export default function Home(props) {
  useEffect(() => {
    const { pathname } = Router
    if (pathname == '/') {
      Router.push('/login');
    }
  }, [props]);

  return ''
}
Nikul Panchal
  • 1,542
  • 3
  • 35
  • 58

3 Answers3

16

You can add redirects to your next.config.js like this:

module.exports = {
  async redirects() {
    return [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ]
  },
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
ashwin
  • 186
  • 1
  • 6
0

Redirect from the server side;

export const getServerSideProps = async ({ res }) => {
    res.setHeader("location", `/kiosk/welcome`);

    res.statusCode = 302;
    res.end();

    return { props: {} };
};

const Index = () => <>Index</>;

export default Index;

Aung
  • 191
  • 1
  • 4
-1

You can set a base path. Next Js allows you to do this. https://nextjs.org/docs/api-reference/next.config.js/basepath For example, if you wish to use "/login" instead of "/" which is the default, open next.config.js and add the basePath config:

  const nextConfig = {
  basePath: "/login",
};

module.exports = nextConfig;
Bemsen Daniel
  • 181
  • 3
  • 13