Nikul Panchal
Nikul Panchal

Reputation: 1673

how to set default page in next js?

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 ''
}

Upvotes: 12

Views: 37001

Answers (4)

Prince Morgan
Prince Morgan

Reputation: 1

You can use the code below to redirect from the default page to your desired page

import {redirect} from "next/navigation";

const homePage() => {
redirect("/desired-path")
}

export homePage;

Upvotes: 0

Bemsen Daniel
Bemsen Daniel

Reputation: 191

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;

Upvotes: -2

ashwin
ashwin

Reputation: 306

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

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

Upvotes: 25

Aung
Aung

Reputation: 330

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;

Upvotes: 0

Related Questions