Google News
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.