Google News
logo
Next.js Interview Questions
Next.js is an open-source, lightweight React.js framework that gives you building blocks to create web applications. Next.js was first released as an open-source project on GitHub on October 25, 2016. It was created by Zeit. By this framework, mean Next.js handles the tooling and configuration needed for React, and provides additional structure, features, and optimizations for your web application.
For React developers, NextJS has a lot of advantages. Let's go over all of NextJS's features :
 
 
Automatic Routing : There's no need to set up URLs for routing. The files should be saved in the pages folder. The file system will be mapped to all URLs. Customization is possible.

Dynamic Components : Next.js allows us to import JavaScript modules and React Components.
 
Component-specific styles : Global and component-specific styles are supported by styled-jsx.
 
Server-side rendering : As React components are prerendered on the server, they load more quickly on the client.
 
Node Ecosystem : As Next.js is React-based, it fits well with the Node ecosystem.
 
Automatic code split : Next.js renders pages with all of the required libraries. Next.js creates multiple resources rather than a single large javascript file. Only the required javascript pages are loaded when a page is loaded.
 
Prefetch : Next.js is a framework for developing web applications. The prefetch property of the Link component, which is used to link multiple components, can be used to prefetch page resources in the background.
 
Export Static Site : With Next.js, we can export our entire static site from our web application.

Hot Code Reload : The Next.js server detects modified files and automatically reloads them.
 
Built-in Typescript Support : Next.js is a Typescript-based framework with excellent Typescript support.
If you want to build a complete web application with React from scratch, you have to fulfill the following points :
 
* Your code has to be bundled using a bundler like webpack and transformed using a compiler like Babel.
* You have to do production optimizations such as code splitting.
* You have to pre-render some pages for performance and SEO statically. You might also want to use server-side rendering or client-side rendering.
* You might have to write some server-side code to connect your React app to your data store.

Next.js fulfills the above all requirements.
 
Reasons why the world's leading companies prefer Next.js :

* Zero Setup : Next.js provides automatic code-splitting, filesystem-based routing, hot code reloading, and universal rendering; that's why the world's leading companies prefer it.
 
* Fully Extensible : Next.js is fully extensible and has complete control over Babel and Webpack. It provides a customizable server, routing, and next plugins.
 
* Ready for Production : Next.js is optimized for smaller build sizes, faster dev compilation, and many other improvements, making it a popular choice.
 
* Next.js can Deploy Anywhere : Next.js is an open-source, lightweight React.js framework that facilitates developers to build static and server-side rendering web applications.
There are a few things you need to consider when building modern applications. Such as:
 
* User Interface - how users will consume and interact with your application.

* Routing - how users navigate between different parts of your application.

* Data Fetching - where your data lives and how to get it.

* Rendering - when and where you render static or dynamic content.

* Integrations - what third-party services you use (CMS, auth, payments, etc) and how you connect to them.

* Infrastructure - where you deploy, store, and run your application code (Serverless, CDN, Edge, etc).

* Performance - how to optimize your application for end-users.

* Scalability - how your application adapts as your team, data, and traffic grow.

* Developer Experience - your team’s experience building and maintaining your application.

For each part of your application, you will need to decide whether you will build a solution yourself or use other tools such as libraries and frameworks.
System Requirements
* Node.js 12.22.0 or later
* MacOS, Windows (including WSL), and Linux are supported

Automatic Setup : We recommend creating a new Next.js app using create-next-app, which sets up everything automatically for you. To create a project, run:
npx create-next-app@latest
# or
yarn create next-app
# or
pnpm create next-app
If you want to start with a TypeScript project you can use the --typescript flag :
npx create-next-app@latest --typescript
# or
yarn create next-app --typescript
# or
pnpm create next-app --typescript
After the installation is complete :
 
* Run npm run dev or yarn dev or pnpm dev to start the development server on http://localhost:3000
* Visit http://localhost:3000 to view your application
* Edit pages/index.js and see the updated result in your browser
 

Manual Setup : 
Install next, react and react-dom in your project:
npm install next react react-dom
# or
yarn add next react react-dom
# or
pnpm add next react react-dom
Open package.json and add the following scripts :
"scripts": {
  "dev": "next dev",
  "build": "next build",
  "start": "next start",
  "lint": "next lint"
}
These scripts refer to the different stages of developing an application:
 
* dev - Runs next dev to start Next.js in development mode
* build - Runs next build to build the application for production usage
* start - Runs next start to start a Next.js production server
* lint - Runs next lint to set up Next.js' built-in ESLint configuration

Create two directories pages and public at the root of your application:
 
* pages - Associated with a route based on their file name. For example pages/about.js is mapped to /about
* public - Stores static assets such as images, fonts, etc. Files inside public directory can then be referenced by your code starting from the base URL (/).

Next.js is built around the concept of pages. A page is a React Component exported from a .js, .jsx, .ts, or .tsx file in the pages directory. You can even add dynamic route parameters with the filename.
 
Inside the pages directory add the index.js file to get started. This is the page that is rendered when the user visits the root of your application
 
Populate pages/index.js with the following contents :
function HomePage() {
  return <div>Welcome to Next.js!</div>
}

export default HomePage

 

After the set up is complete :
 
* Run : npm run dev or yarn dev or pnpm dev to start the development server on http://localhost:3000
* Visit : http://localhost:3000 to view your application
* Edit : pages/index.js and see the updated result in your browser
Next.js is a popular framework of React.js that is most popularly used for building the following types of apps and websites :
 
* Static Websites
* Desktop Websites
* SEO Friendly Websites
* Server Rendered Apps
* Progressive Web Apps (PWA) etc.
There are multiple ways to fetch data in Next.js, but Next.js itself recommends getInitialProps, an async function to retrieve data from anywhere. When we use getInitialProps to retrieve data, it receives a context object which has the following properties :
 
* pathname : It specifies the path section of the URL.

* query : It is used to specify the query string section of URL parsed as an object.

* asPath : It specifies the string of the actual path (including the query) shows in the browser.

* req : It is used to specify the HTTP request object (server only).

* res : It is used to specify the HTTP response object (server only).

* err : It is used to specify the error object if any error is encountered during the rendering.
Generally, we use the app.disable('etag') syntax to disable the etag generation in Next.js. But, this may not work for all static contents. So, we should use the following syntax to disable the etag for all static contents.
 
Syntax :
app.use(express.static(path.join(__dirname, 'public'), {  
etag: false  
})); ​
 
To configure the build-id in Next.js, we must configure a static ID between our builds. So, we have to provide the "generateBuildId" function with the following configuration.
 
Syntax :
// next.config.js  
module.exports = {  
   generateBuildId: async () => {  
  // For example get the latest git commit hash here  
  return 'my-build-id';  
  }  
};​
   
create-next-app allows you to create a new Next.js app within seconds. It is officially maintained by the creators of Next.js, and includes a number of benefits :
 
Interactive Experience : Running npx create-next-app@latest (with no arguments) launches an interactive experience that guides you through setting up a project.
 
Zero Dependencies : Initializing a project is as quick as one second. Create Next App has zero dependencies.
 
Offline Support : Create Next App will automatically detect if you're offline and bootstrap your project using your local package cache.
 
Support for Examples : Create Next App can bootstrap your application using an example from the Next.js examples collection (e.g. npx create-next-app --example api-routes).
 
Tested : The package is part of the Next.js monorepo and tested using the same integration test suite as Next.js itself, ensuring it works as expected with every release.
Data fetching in Next.js allows you to render your content in different ways, depending on your application's use case. These include pre-rendering with Server-side Rendering or Static Generation, and updating or creating content at runtime with Incremental Static Regeneration.
Client-side data fetching is useful when your page doesn't require SEO indexing, when you don't need to pre-render your data, or when the content of your pages needs to update frequently. Unlike the server-side rendering APIs, you can use client-side data fetching at the component level.
 
If done at the page level, the data is fetched at runtime, and the content of the page is updated as the data changes. When used at the component level, the data is fetched at the time of the component mount, and the content of the component is updated as the data changes.
 
It's important to note that using client-side data fetching can affect the performance of your application and the load speed of your pages. This is because the data fetching is done at the time of the component or pages mount, and the data is not cached.
The team behind Next.js has created a React hook library for data fetching called SWR (stale-while-revalidate). It is highly recommended if you are fetching data on the client-side. It handles caching, revalidation, focus tracking, refetching on intervals, and more.
 
Using the same example as above, we can now use SWR to fetch the profile data. SWR will automatically cache the data for us and will revalidate the data if it becomes stale.
import useSWR from 'swr'

const fetcher = (...args) => fetch(...args).then((res) => res.json())

function Profile() {
  const { data, error } = useSWR('/api/profile-data', fetcher)

  if (error) return <div>Failed to load</div>
  if (!data) return <div>Loading...</div>

  return (
    <div>
      <h1>{data.name}</h1>
      <p>{data.bio}</p>
    </div>
  )
}
 
If you export a function called getServerSideProps (Server-Side Rendering) from a page, Next.js will pre-render this page on each request using the data returned by getServerSideProps.
export async function getServerSideProps(context) {
  return {
    props: {}, // will be passed to the page component as props
  }
}

When does getServerSideProps run : getServerSideProps only runs on server-side and never runs on the browser. If a page uses getServerSideProps, then:

When you request this page directly, getServerSideProps runs at request time, and this page will be pre-rendered with the returned props

When you request this page on client-side page transitions through next/link or next/router, Next.js sends an API request to the server, which runs getServerSideProps

getServerSideProps returns JSON which will be used to render the page. All this work will be handled automatically by Next.js, so you don’t need to do anything extra as long as you have getServerSideProps defined.

You can use the next-code-elimination tool to verify what Next.js eliminates from the client-side bundle.

getServerSideProps can only be exported from a page. You can’t export it from non-page files.

Note that you must export getServerSideProps as a standalone function — it will not work if you add getServerSideProps as a property of the page component.

If you export a function called getStaticProps (Static Site Generation) from a page, Next.js will pre-render this page at build time using the props returned by getStaticProps.
export async function getStaticProps(context) {
  return {
    props: {}, // will be passed to the page component as props
  }
}

 

You should use getStaticProps if :
 
* The data required to render the page is available at build time ahead of a user’s request

* The data comes from a headless CMS

* The page must be pre-rendered (for SEO) and be very fast — getStaticProps generates HTML and JSON files, both of which can be cached by a CDN for performance

* The data can be publicly cached (not user-specific). This condition can be bypassed in certain specific situation by using a Middleware to rewrite the path.
The Next.js Image component, next/image, is an extension of the HTML <img> element, evolved for the modern web. It includes a variety of built-in performance optimizations to help you achieve good Core Web Vitals. These scores are an important measurement of user experience on your website, and are factored into Google's search rankings.
 
Some of the optimizations built into the Image component include :
 
* Improved Performance : Always serve correctly sized image for each device, using modern image formats

* Visual Stability :
Prevent Cumulative Layout Shift automatically

* Faster Page Loads :
Images are only loaded when they enter the viewport, with optional blur-up placeholders

* Asset Flexibility :
On-demand image resizing, even for images stored on remote servers
Next.js comes with built-in support for environment variables, which allows you to do the following:
 
* Use .env.local to load environment variables
* Expose environment variables to the browser by prefixing with NEXT_PUBLIC_

Default Environment Variables : In general only one .env.local file is needed. However, sometimes you might want to add some defaults for the development (next dev) or production (next start) environment.
 
Next.js allows you to set defaults in .env (all environments), .env.development (development environment), and .env.production (production environment).
 
.env.local always overrides the defaults set.
When a file is added to the pages directory, it's automatically available as a route.
 
The files inside the pages directory can be used to define most common patterns.
 
Index routes : The router will automatically route files named index to the root of the directory.
 
* pages/index.js/
* pages/blog/index.js/blog

Nested routes : The router supports nested files. If you create a nested folder structure, files will automatically be routed in the same way still.
 
* pages/blog/first-post.js/blog/first-post
* pages/dashboard/settings/username.js/dashboard/settings/username

Dynamic route segments : To match a dynamic segment, you can use the bracket syntax. This allows you to match named parameters.
 
* pages/blog/[slug].js/blog/:slug (/blog/hello-world)
* pages/[username]/settings.js/:username/settings (/foo/settings)
* pages/post/[...all].js/post/* (/post/2020/id/title)
Next.js can be deployed to any hosting provider that supports Docker containers. You can use this approach when deploying to container orchestrators such as Kubernetes or HashiCorp Nomad, or when running inside a single node in any cloud provider.
 
* Install Docker on your machine
* Clone the with-docker example
* Build your container: docker build -t nextjs-docker .
* Run your container: docker run -p 3000:3000 nextjs-docker

If you need to use different Environment Variables across multiple environments, check out our with-docker-multi-env example.
The first step to identifying which authentication pattern you need is understanding the data-fetching strategy you want. We can then determine which authentication providers support this strategy.

There are two main patterns :
 
* Use static generation to server-render a loading state, followed by fetching user data client-side.
* Fetch user data server-side to eliminate a flash of unauthenticated content.
To create a page directory inside our project we have to populate the ./pages/index.js with the following contents:
function HomePage() {  
  return <div>Welcome to Next.js!</div>  
}  
To start developing our application, we have to run the npm run dev or yarn dev command. This will start the development server on http://localhost:3000. Now we can visit the localhost: http://localhost:3000 to view our application.
We can create our custom error page by defining a _error.js in the pages folder. See the following example :
import React from "react";  
class Error extends React.Component {  
  static getInitialProps({ res, err }) {  
    const statusCode = res ? res.statusCode : err ? err.statusCode : null;  
    return { statusCode };  
  }  
  render() {  
    return (  
   
        {this.props.statusCode  
          ? `An error ${this.props.statusCode} has occurred on the server`  
          : "An error occurred on client-side"}  
        
    );  
  }  
}  
export default Error;​
  
Generally, code splitting is one of the most compelling features of webpack. This feature facilitates us to split our code into various bundles, which can be loaded only on-demand or in parallel. This is mainly used to achieve the smaller bundles and facilitates us to control resource load prioritization which finally has a great impact on the load time.
 
There are mainly three approaches to code splitting :
 
  Entry Points : It is used to split code using entry configuration manually.

  Prevent Duplication : It uses Entry dependencies or SplitChunksPlugin to dedupe and split chunks.

  Dynamic Imports : It splits the code via inline function calls within modules

It is mainly used to enable pages that can never load unnecessary code.
Many people consider Gatsby and Next.js the same thing mostly because they almost perform the same function. However, the two frameworks are different and feature a couple of similarities at first glance. They include:
 
* The two frameworks both generate highly performing websites
* Next.js and Gatsby develop SPA out of the box
* The two frameworks create websites that are SEO friendly hence ranking top on browsers
* Under both circumstances, the developers have extraordinary experiences
                      Next.Js                       React
  • Vercel created the Next.js framework.
  • The React front-end library was created by Facebook.
  • Next.js is open-source based on Node.js and Babel and integrates with React for developing single-page apps. 
  • React is a JavaScript library that helps us to build user interfaces using components as building blocks.

 

  • Next.js is a framework that allows us to use the React library to create web app pages and UI. Next.js employs React not only to create UI components but also to construct entire pages.
  • As a library, React.js is part of a framework, specifically the UI components section.
  • Because of static sites and server-side rendering, Next.js apps are lightning-fast. They're effective right out of the box because of performance-enhancing features like Image Optimization, etc.

 

  • When it comes to React, a few things rule it out of the discussion. It only supports client-side rendering out of the box, which is insufficient to build a high-performance application.

 

  • We add the page to the pages folder and the required header component link to create pages for the Next.js project. This simplifies our lives because we write less code, and the project is simple to understand.
  • We must first create a component and then add it to the router to create a React project page.

 

This is an important question and is asked in many Next.js interview questions. There are two ways to enable AMP in Next.js.
 
* AMP-First Pages
* Hybrid AMP Pages

AMP-First Pages : The AMP-First Pages are served to the primary traffic of the website as well as traffic generated from the search engine. Use the following syntax to implement AMP-first pages.
 
Example :
// pages/index.js  
import { withAmp } from 'next/amp'  
function HomePage() {  
return <p> Welcome to AMP + Next.js.</p>  
}  
export default withAmp(HomePage)  

Hybrid AMP Pages :
The Hybrid AMP pages allow the users to have coexisted AMP version of a traditional page so that the search engines can display the AMP version or the page in different mobile search results.
 
See the following example to understand how to implement the Hybrid AMP to pages :
 
Example :
// pages/index.js  
import { withAmp } from 'next/amp'  
function HomePage() {  
return <p> Welcome to AMP + Next.js.</p>  
}  
export default withAmp(HomePage)  
URL imports are an experimental feature that allows you to import modules directly from external servers (instead of from the local disk).
 
Warning : This feature is experimental. Only use domains that you trust to download and execute on your machine. Please exercise discretion, and caution until the feature is flagged as stable.
 
To opt-in, add the allowed URL prefixes inside next.config.js :
module.exports = {
  experimental: {
    urlImports: ['https://example.com/modules/'],
  },
}
Then, you can import modules directly from URLs :
import { a, b, c } from 'https://example.com/modules/some/module.js'
URL Imports can be used everywhere normal package imports can be used.
When a page qualifies for Automatic Static Optimization we show an indicator to let you know.
 
This is helpful since automatic static optimization can be very beneficial and knowing immediately in development if the page qualifies can be useful.
 
In some cases this indicator might not be useful, like when working on electron applications. To remove it open next.config.js and disable the autoPrerender config in devIndicators:
module.exports = {
  devIndicators: {
    autoPrerender: false,
  },
}
When you edit your code, and Next.js is compiling the application, a compilation indicator appears in the bottom right corner of the page.
 
Note : This indicator is only present in development mode and will not appear when building and running the app in production mode.
 
In some cases this indicator can be misplaced on your page, for example, when conflicting with a chat launcher. To change its position, open next.config.js and set the buildActivityPosition in the devIndicators object to bottom-right (default), bottom-left, top-right or top-left:
module.exports = {
  devIndicators: {
    buildActivityPosition: 'bottom-right',
  },
}
In some cases this indicator might not be useful for you. To remove it, open next.config.js and disable the buildActivity config in devIndicators object :
module.exports = {
  devIndicators: {
    buildActivity: false,
  },
}