20

I have a website built in React Js and the same one on Next Js as well.

The problem which I am facing right now is, the router seems very slow in the nextJs compare to react-router-dom, It's taking almost 2-3 seconds to change the route.

Here are the URLs where you can feel the difference between the performance by moving around different pages.

https://cutt.ly/mhbPkOE (React Router Dom) vs https://cutt.ly/BhbPvHv (NextJs)

I had read some comments on Github where few experts are saying that It will resolve in production. but It looks same in production too.

Please have a look at the following code

_app.jsx

// import App from 'next/app'
import React from "react"
import Router from 'next/router';

import "../static/sass/application.scss";

import "bootstrap/dist/css/bootstrap.min.css";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import 'semantic-ui-css/semantic.min.css'
import { wrapper } from "../../redux/utils/store"

import App from 'next/app';
// A simple component that we created
import {LoaderOverlay} from '../components/Reusable'
class MyApp extends App {
    constructor(props){
        super(props)
        this.state = {
            isLoading: false,
        }
        Router.onRouteChangeStart = (url) => {
            // Some page has started loading
            this.setState({
                isLoading: true,
            }) // set state to pass to loader prop
        };
    
        Router.onRouteChangeComplete = (url) => {
            // Some page has finished loading
            this.setState({
                isLoading: false,
            }) // set state to pass to loader prop
        };
    
        Router.onRouteChangeError = (err, url) => {
            this.setState({isLoading: false,})
        }; 
    };
    render() {
        const {Component, pageProps} = this.props
        return (
            <div>
                {this.state.isLoading ? (
                    <LoaderOverlay/>
                ) : (
                    <Component {...pageProps} />
                )}
            </div>
        )
    }
}
export default wrapper.withRedux(MyApp);

_document.jsx

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
    static async getInitialProps(ctx) {
        const originalRenderPage = ctx.renderPage

        ctx.renderPage = () =>
            originalRenderPage({
            // useful for wrapping the whole react tree
            enhanceApp: (App) => App,
            // useful for wrapping in a per-page basis
            enhanceComponent: (Component) => Component,
            })

        // Run the parent `getInitialProps`, it now includes the custom `renderPage`
        const initialProps = await Document.getInitialProps(ctx)

        return initialProps
    }

    render() {
        return (
            <Html lang="en">
                <Head>
                <link async rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css"/>
                </Head>
                <body>
                    <div className={'main-wrapper'}>
                        <Main />
                    </div>
                    <NextScript />
                </body>
            </Html>
        )
    }
}

export default MyDocument
3
  • well that is because in next.js the pages are server-rendered while on SPA react it isn't you can render your pages as static pages on next.js depends on your needs everything has its pros and cons maybe it is better to build your app as React SPA if you don't need SEO optimization. Commented Dec 4, 2020 at 16:22
  • well, I already have this app in React JS, but obviously I need SEO optimized as well that's why I choose Next Js to make my app SEO friendly. I was hoping It won't affect performance at least in the router. Commented Dec 4, 2020 at 16:27
  • Same problem in 2023. I have a very minimalistic app, some routes don't need any new data from the server (all the data is already on the page, I just have cards that when clicked are opened in a modal and show the data in a bit different format). I'm using shadow routing (when the actual URL that the browser sees is different from what the router sees). I have the shallow prop on all the Links. Somehow, still, in production, my cards are taking anywhere from 300ms to 2s to open in a modal. If I use React useState to track the opened card instead of the router, the opening takes 50ms Commented May 24, 2023 at 8:32

5 Answers 5

13

Development mode (next dev) is much slower because the routes aren't pre-built.

All delay related to routing assuming you don't have any server side blocking data requirements via getInitialProps, getServerSideProps, should not be present when running production mode with next build followed by next start.

Sign up to request clarification or add additional context in comments.

1 Comment

This is helpful. I initially had a server.js file created to serve my app in Azure (which meant I cannot use next start) and it was very slow (around 11s). But based on this answer, I used PM2 to serve my application from Azure App service and now the app in production is quick (around 250ms)
1

I had the same issue. And I managed to speed things up a bit by doing the following

  • I had a fetch requests directly on the page so I guess the server has to do the fetching before rendering the UI so it takes time for the initial load.
  • I moved this request to another server component and imported it in the page component
  • I did this for any other places in other pages as well.
  • I implemented react suspense. So in the initial load, page component has something to render.

This way I managed to reduce the page load time, which helped me to reduce the time between moving between pages.

So in summary my idea was to render what is available as soon as possible and render the components that are associated with API calls later. until they fetch the data, I showed a skeleton with the help of react suspense

Comments

0

Not sure if you have found a fix for this yet, but I came across this article about "shallow routing". I can't see much improvement in my application when using it, but maybe it will help someone else:

https://nextjs.org/docs/routing/shallow-routing

Comments

-5

Hey I think you are in your production mode. That's why it is slow. But if you will host your site it will be pretty much like react only. But then also if you want to routing fast Then npm i [email protected] --save will work fine..

Comments

-8

to solve this issue followed the commands:

  1. yarn build/nmp build
  2. yarn start/npm start

I hope this will solve this issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.