Conditional redirection in Next.js Conditional redirection in Next.js reactjs reactjs

Conditional redirection in Next.js


you can check if user has accessed the page via server or client side.and after that you can conditionally redirect with the proper tool.getInitialProps function gets a ctx object. you can check whether its on server or client like this:

import Router from 'next/router'export default class Browse extends Component {    static async getInitialProps (ctx) {        if (ctx && ctx.req) {            console.log('server side')            ctx.res.writeHead(302, {Location: `/`})            ctx.res.end()        } else {            console.log('client side')            Router.push(`/`)        }...