Skip to content

mrhrifat/nextjs-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 

Repository files navigation

NextJS Interview Questions & Answers

🚀 This repository aimed to contains 500 nextjs interview questions & answers with exmample.


Groups

No Contents
1 Common
2 Pages Router
3 App Router
4 Others

Table of Contents

No Contents
1 What is NextJS
2 How do you create a new Next.js project?
3 What is the purpose of the pages or app directory in Next.js?
4 What is file based routing in Next.js?
5 What do you create route at pages based routing in Next.js?
7 How do you create a dynamic route in Next.js?
8 What are the key features of Next.js?
9 What are the Differences Between Next.js and React.js
10 What is getStaticProps?
11 What is getServerSideProps?
12 What is getStaticPaths?
13 What is the difference between getStaticProps and getServerSideProps?
14 What is the Link component in Next.js?
15 What is the useRouter hook in Next.js?
12 How do you navigate programmatically in Next.js?
13 What is the _app.js file in Next.js?
14 What is the _document.js file in Next.js?
15 How do you add global CSS in Next.js?
16 How do you add component-level CSS in Next.js?
17 What is static site generation (SSG) in Next.js?
18 What is middleware?
18 What is server-side rendering (SSR) in Next.js?
19 What is incremental static regeneration (ISR) in Next.js?
20 What is the Image component in Next.js?
21 What is next.config.js?
22 How do you enable TypeScript in a Next.js project?
23 What is API Routes in Next.js?
24 How do you deploy a Next.js app to Vercel?
25 What is pre-rendering in Next.js?
26 What is the difference between static generation and server-side rendering?
27 How do you handle redirects in Next.js?
28 What is the Head component in Next.js?
29 How do you fetch data in a Next.js page?
30 What is dynamic import in Next.js?
31 How do you handle environment variables in Next.js?
32 What is fallback in getStaticPaths?
33 What is a custom server in Next.js?
34 What is the next/head package used for?
35 How do you create a 404 page in Next.js?
36 What is the use of next export command?
37 How do you optimize fonts in Next.js?
38 What is the default port for a Next.js app?
39 How do you add custom headers in Next.js?
40 What is Fast Refresh in Next.js?
41 What is the public folder in Next.js?
42 How do you configure a custom Babel setup in Next.js?
43 How do you handle internationalization (i18n) in Next.js?
44 What is React Strict Mode in Next.js?
45 What is a singleton router in Next.js?
46 What is the useTranslation hook in Next.js?
47 How do you create custom error pages in Next.js?
48 What is AMP in Next.js?
49 How do you enable AMP in Next.js?
50 What is the next/image component used for?
51 What is the next/link component used for?
52 What is the difference between pages and components directories?
53 How do you handle middleware in Next.js?
54 How do you add polyfills in Next.js?
55 What is the difference between client-side and server-side rendering in Next.js?
56 What is static optimization in Next.js?
57 How do you fetch data on the client-side in Next.js?
58 How do you implement authentication in Next.js?
59 What is the difference between _app.js and _document.js?
60 How do you create API endpoints in Next.js?
61 What is ISR in Next.js?
62 How do you configure Webpack in Next.js?
63 What is the purpose of next-env.d.ts?
64 How do you handle routing in a Next.js app?
65 What is the purpose of next/dynamic?
66 How do you add meta tags in Next.js?
67 What is the purpose of next-compose-plugins?
68 How do you handle form submissions in Next.js?
69 How do you set up Redux in a Next.js project?
70 What is the purpose of next/router?
71 How do you use CSS-in-JS with Next.js?
72 How do you handle redirects in Next.js?
73 How do you use Sass in a Next.js project?
74 What is the basePath option in Next.js?
75 How do you customize the 500 error page in Next.js?
76 What is the trailingSlash option in Next.js?
77 What is the difference between push and replace in useRouter?
78 What is ssr: false in dynamic import?
79 How do you configure PWA in Next.js?
80 How do you add Google Analytics to a Next.js project?
81 What is the purpose of middleware in Next.js?
82 How do you use Apollo Client with Next.js?
83 What is the publicRuntimeConfig in Next.js?
84 What is the serverRuntimeConfig in Next.js?
85 What is the purpose of _error.js in Next.js?
86 How do you perform client-side data fetching in Next.js?
87 What is the use of next-seo in Next.js?
88 How do you use Tailwind CSS in Next.js?
89 How do you configure next-i18next in Next.js?
90 What is next/script used for?
91 How do you enable custom fonts in Next.js?
92 How do you set up GraphQL in Next.js?
93 What is swcMinify in next.config.js?
94 What is the use of next-compose-plugins?
95 What is a hybrid application in Next.js?
96 How do you handle CORS in Next.js API routes?
97 What is the purpose of rewrites in next.config.js?
98 How do you manage cookies in Next.js?
99 How do you handle authentication tokens in Next.js?
100 What is next-pwa used for?
101 What is the next-sitemap package used for?

⬆️ Back to Top

  1. What is NextJS?

    Next.js is a React framework for building full-stack web applications.

    ⬆️ Back to Top

  2. How do you create a new Next.js project?

    Using command

    npx create-next-app@latest

    ⬆️ Back to Top

  3. What is the purpose of the pages or app directory in Next.js?

    It contains React components that are automatically routed based on their file name.

    ⬆️ Back to Top

  4. What is file based routing in Next.js?

    Routing based on the file structure in the pages or app directory.

    ⬆️ Back to Top

  5. What are the key features of Next.js?

    • Server Side Rendering (SSR): Next.js allows rendering React components on the server before sending them to the client, improving performance and SEO.
    • Static Site Generation (SSG): It pre-renders pages at build time, useful for blogs or e-commerce sites.
    • API Routes: You can build a backend using API routes in the same codebase without needing an external server.
    • File Based Routing: Next.js automatically creates routes based on the file structure inside the pages directory.
    • Client Side Rendering (CSR): Like React, Next.js also supports traditional client-side rendering.
    • Incremental Side Rendering:
    • Image Optimization: Built-in image optimization capabilities that reduce image sizes and enhance loading times.
    • Automatic Code Splitting: Next.js splits the code into smaller bundles, which are loaded only when required, improving performance.
    • TypeScript Support: Native support for TypeScript, enabling strict typing and better developer experience.
    • Incremental Static Regeneration (ISR): Pages can be statically generated at runtime and updated incrementally.
    • Fast Refresh: Provides an instant feedback loop while coding, similar to React's hot reloading.

    ⬆️ Back to Top

  6. What are the Differences Between Next.js and React.js?

    Feature Next.js React.js
    Rendering Supports Server-Side Rendering (SSR), Static Site Generation (SSG), and Client-Side Rendering (CSR). Only supports Client-Side Rendering (CSR) by default.
    Routing Built-in file-based routing system. Automatically generates routes based on the folder structure. No built-in routing. Requires libraries like React Router.
    SEO Excellent for SEO as it supports SSR and SSG, allowing pre-rendered content to be indexed by search engines. Limited SEO capabilities due to client-side rendering. Additional work is needed for SEO optimization.
    Performance Faster initial page load due to SSR, automatic code splitting, and static generation. May have slower page loads for large apps since everything is rendered on the client.
    Configuration Minimal configuration required. Comes with SSR, SSG, and routing out of the box. Requires manual setup for SSR, routing, and other advanced features.
    Learning Curve Slightly steeper due to built-in advanced features like SSR, SSG, and API routes. Easier to learn initially, but requires additional tools for SSR and routing.
    API Routes Built-in API routes that can handle backend logic within the same project. No support for API routes; requires external tools for backend development.
    Code Splitting Automatically splits code into smaller bundles, loading only what’s needed for a specific page. Requires manual code splitting or use of lazy loading to optimize performance.
    Deployment Optimized for easy deployment on platforms like Vercel (creators of Next.js) and supports serverless functions. Deployment typically requires additional configuration for optimized hosting and SSR.
    Image Optimization Has a built-in Image component for automatic image resizing and optimization. Does not provide image optimization; developers need third-party libraries for that.

    ⬆️ Back to Top

  7. What is Incremental Side Rendering?

    Incremental Static Regeneration is a technique in Next.js that allows you to update static pages at runtime without rebuilding the entire site. This feature introduces a seamless way to serve both static and dynamic content by revalidating and regenerating pages in the background.

    ⬆️ Back to Top

  8. What is the Link component in Next.js?

    A component for client-side navigation between pages.

    import Link from 'next/link'
    
    <Link href="/">Home</Link>
    <Link href="/about">About</Link>

    ⬆️ Back to Top

  9. What is the useRouter hook in Next.js?

    A hook that allows access to the router object and perform navigation.

    ⬆️ Back to Top

  10. How do you navigate programmatically in Next.js?

    Using useRouter() hook.

    const router = userRouter();
    
    function handleClick() {
      router.push(`/path`);
    }
    
    <button onClick={handleClick}>Go There</button>;

    ⬆️ Back to Top

  11. What is middleware?

    Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.

    ⬆️ Back to Top

  12. How do you add component-level CSS in Next.js?

    Using CSS modules with a .module.css file extension.

    ⬆️ Back to Top

  13. What is static site generation (SSG) in Next.js?

    Pre-rendering pages at build time. If a page uses Static Generation, the page HTML is generated at build time.

    ⬆️ Back to Top

  14. What is server-side rendering (SSR) in Next.js?

    Rendering pages on each request. If a page uses Server-side Rendering, the page HTML is generated on each request.

    ⬆️ Back to Top

  15. What is incremental static regeneration (ISR) in Next.js?

    Re-generating static pages at runtime as traffic comes in.

    ⬆️ Back to Top

  16. What is the Image component in Next.js?

    A component that optimizes images for faster loading.

    export default function Page() {
      return (
        <Image
          src={profilePic}
          alt="Picture of the author"
          // width={500} automatically provided
          // height={500} automatically provided
          // blurDataURL="data:..." automatically provided
          // placeholder="blur" // Optional blur-up while loading
        />
      );
    }

    ⬆️ Back to Top

  17. What is next.config.js?

    A configuration file to customize Next.js settings.

    ⬆️ Back to Top

  18. How do you enable TypeScript in a Next.js project?

    By adding a tsconfig.json file.

    ⬆️ Back to Top

  19. What is API Routes in Next.js?

    A feature to create API endpoints in the pages/api directory.

    ⬆️ Back to Top

  20. How do you deploy a Next.js app to Vercel?

    By connecting the repository to Vercel and deploying it.

    ⬆️ Back to Top

  21. What is pre-rendering in Next.js?

    Generating HTML for pages in advance, instead of on each request.

    ⬆️ Back to Top

  22. What is the difference between static generation and server-side rendering?

    Static generation pre-renders at build time, SSR pre-renders on each request.

    ⬆️ Back to Top

  23. How do you handle redirects in Next.js?

    There are a few ways you can handle redirects in Next.js. One of them is by configuring redirects in next.config.js.

    ⬆️ Back to Top

  24. What is the Head component in Next.js?

    A component for modifying the of a page.

    ⬆️ Back to Top

  25. What is the public folder in Next.js?

    A folder for static assets served from the root URL.

    ⬆️ Back to Top

  26. How do you fetch data in a Next.js page?

    Using getStaticProps or getServerSideProps.

    // getStaticProps
    export async function getStaticProps() {
      const res = await fetch("https://api.github.com/repos/vercel/next.js");
      const repo = await res.json();
      return { props: { repo } };
    }
    
    export default function Page({ repo }) {
      return repo.stargazers_count;
    }
    // getServerSideProps
    export async function getServerSideProps() {
      // Fetch data from external API
      const res = await fetch("https://api.github.com/repos/vercel/next.js");
      const repo = await res.json();
      // Pass data to the page via props
      return { props: { repo } };
    }
    
    export default function Page({ repo }) {
      return (
        <main>
          <p>{repo.stargazers_count}</p>
        </main>
      );
    }

    ⬆️ Back to Top

  27. What is dynamic import in Next.js?

    A feature to load components or modules dynamically.

    const ComponentA = dynamic(() => import("../components/A"));
    const ComponentB = dynamic(() => import("../components/B"));

    ⬆️ Back to Top

  28. How do you handle environment variables in Next.js?

    By adding them to .env.local and accessing via process.env.

    ⬆️ Back to Top

  29. What is fallback in getStaticPaths?

    Determines how to handle missing paths, with true, false, or ‘blocking’.

    ⬆️ Back to Top

  30. What is a custom server in Next.js?

    A way to customize the server-side behavior, e.g., with Express.

    ⬆️ Back to Top

  31. What is the next/head package used for?

    To manage the document head for meta tags, title, etc.

    ⬆️ Back to Top

  32. What is the use of next export command?

    To export a static version of the Next.js app.

    ⬆️ Back to Top

  33. How do you optimize fonts in Next.js?

    By using the built-in font optimization feature.

    ⬆️ Back to Top

  34. What is the default port for a Next.js app?

    Port 3000.

    ⬆️ Back to Top

  35. How do you add custom headers in Next.js?

    By configuring headers in next.config.js.

    ⬆️ Back to Top

  36. What is Fast Refresh in Next.js?

    A feature for quick feedback when editing React components.

    ⬆️ Back to Top

  37. How do you configure a custom Babel setup in Next.js?

    By adding a babel.config.js file.

    ⬆️ Back to Top

  38. How do you handle internationalization (i18n) in Next.js?

    By configuring i18n settings in next.config.js.

    ⬆️ Back to Top

  39. What is React Strict Mode in Next.js?

    A development mode that highlights potential problems in an application.

    ⬆️ Back to Top

  40. What is a singleton router in Next.js?

    A single router instance accessible across the application.

    ⬆️ Back to Top

Pages Router

⬆️ Back to Top

  1. What do you create route at pages based routing in Next.js?

    Create a directory into pages directory & create index file.

         pages/index.jsx // /
    
         pages/blog/index.jsx // /blog
    
    

    ⬆️ Back to Top

  2. How do you create a dynamic route in Next.js?

    By using square brackets in the filename

         pages/blogs/[id].js.pages // /blogs/1, /blogs/2, /blogs/...
    
         blogs/[id]/index.js // /blogs/1, /blogs/2, /blogs/...
    
         app/products[id]/page.jsx // /products/1, /products/2, /products/...
    

    ⬆️ Back to Top

  3. What is the _app.js file in Next.js?

    A custom App component that initializes pages and can add global styles or layout components.

    ⬆️ Back to Top

  4. What is the _document.js file in Next.js?

    A custom Document component that allows customization of the HTML document structure.

    ⬆️ Back to Top

  5. How do you create a 404 page in Next.js?

    By adding a 404.js file in the pages directory.

    ⬆️ Back to Top

  6. What is the difference between getStaticProps and getServerSideProps?

    getStaticProps fetches data at build time, while getServerSideProps fetches data on each request.

    ⬆️ Back to Top

  7. What is getStaticProps?

    A function used for static site generation to fetch data at build time.

    export async function getStaticProps() {
      const res = await fetch("https://api.github.com/repos/vercel/next.js");
      const repo = await res.json();
      return { props: { repo } };
    }
    
    export default function Page({ repo }) {
      return repo.stargazers_count;
    }

    ⬆️ Back to Top

  8. What is getServerSideProps?

    A function used for server-side rendering to fetch data on each request.

    export async function getServerSideProps() {
      // Fetch data from external API
      const res = await fetch("https://api.github.com/repos/vercel/next.js");
      const repo = await res.json();
      // Pass data to the page via props
      return { props: { repo } };
    }
    
    export default function Page({ repo }) {
      return (
        <main>
          <p>{repo.stargazers_count}</p>
        </main>
      );
    }

    ⬆️ Back to Top

  9. What is getStaticPaths?

    A function used with getStaticProps to specify dynamic routes to be pre-rendered.

    export async function getStaticPaths() {
      return {
        paths: [
          {
            params: {
              name: "next.js",
            },
          },
        ],
      };
    }
    
    export async function getStaticProps() {
      const res = await fetch("https://api.github.com/repos/vercel/next.js");
      const repo = await res.json();
      return { props: { repo } };
    }
    
    export default function Page({ repo }) {
      return repo.stargazers_count;
    }

    ⬆️ Back to Top

  10. How do you add global CSS in Next.js?

    By importing the CSS file in _app.js.

    ⬆️ Back to Top

  11. What is the useTranslation hook in Next.js?

    A hook for handling translations when using i18n.

    ⬆️ Back to Top

  12. How do you create custom error pages in Next.js?

    By adding _error.js in the pages directory.

    ⬆️ Back to Top

  13. What is AMP in Next.js?

    Accelerated Mobile Pages, a framework for fast-loading mobile pages.

    ⬆️ Back to Top

  14. How do you enable AMP in Next.js?

    By adding amp attribute to a page component.

    ⬆️ Back to Top

  15. What is the next/image component used for?

    To optimize and serve images in a Next.js application.

    ⬆️ Back to Top

  16. What is the next/link component used for?

    For client-side navigation between pages.

    ⬆️ Back to Top

  17. What is the difference between pages and components directories?

    pages contains routable components, components contains reusable UI components.

    ⬆️ Back to Top

  18. How do you handle middleware in Next.js?

    Using middleware functions in next.config.js.

    ⬆️ Back to Top

  19. How do you add polyfills in Next.js?

    By customizing the webpack configuration in next.config.js.

    ⬆️ Back to Top

  20. What is the difference between client-side and server-side rendering in next.js?

    Client-side rendering happens in the browser, server-side rendering happens on the server.

    ⬆️ Back to Top

  21. What is static optimization in Next.js?

    Automatically determining if a page can be statically generated.

    ⬆️ Back to Top

  22. How do you fetch data on the client-side in Next.js?

    Using useEffect and fetch or other data fetching libraries.

    ⬆️ Back to Top

  23. How do you implement authentication in Next.js?

    Using libraries like NextAuth.js or custom authentication logic.

    ⬆️ Back to Top

  24. What is the difference between _app.js and _document.js?

    _app.js is for global components, _document.js is for modifying the HTML document structure.

    ⬆️ Back to Top

  25. How do you create API endpoints in Next.js?

    By adding files to the pages/api directory.

    ⬆️ Back to Top

  26. What is ISR in Next.js?

    Incremental Static Regeneration, updating static pages after build without redeploying.

    ⬆️ Back to Top

  27. How do you configure Webpack in Next.js?

    By extending the Webpack configuration in next.config.js.

    ⬆️ Back to Top

  28. What is the purpose of next-env.d.ts?

    It provides type definitions for TypeScript support in Next.js.

    ⬆️ Back to Top

  29. How do you handle routing in a Next.js app?

    Using the file-based routing system in the pages directory.

    ⬆️ Back to Top

  30. What is the purpose of next/dynamic?

    To enable dynamic imports and code splitting.

    "use client";
    
    import { useState } from "react";
    import dynamic from "next/dynamic";
    
    // Client Components:
    const ComponentA = dynamic(() => import("../components/A"));
    const ComponentB = dynamic(() => import("../components/B"));
    const ComponentC = dynamic(() => import("../components/C"), { ssr: false });
    
    export default function ClientComponentExample() {
      const [showMore, setShowMore] = useState(false);
    
      return (
        <div>
          {/* Load immediately, but in a separate client bundle */}
          <ComponentA />
    
          {/* Load on demand, only when/if the condition is met */}
          {showMore && <ComponentB />}
          <button onClick={() => setShowMore(!showMore)}>Toggle</button>
    
          {/* Load only on the client side */}
          <ComponentC />
        </div>
      );
    }

    ⬆️ Back to Top

  31. How do you add meta tags in Next.js?

    Using the Head component from next/head.

    ⬆️ Back to Top

  32. What is the purpose of next-compose-plugins?

    To compose multiple plugins in next.config.js.

    ⬆️ Back to Top

  33. How do you handle form submissions in Next.js?

    Using client-side form handling or API routes for server-side handling.

    ⬆️ Back to Top

  34. How do you set up Redux in a Next.js project?

    By creating a Redux store and integrating it with _app.js.

    ⬆️ Back to Top

  35. What is the purpose of next/router?

    To handle routing and navigation within a Next.js app.

    ⬆️ Back to Top

  36. How do you use CSS-in-JS with Next.js?

    Using libraries like styled-components or Emotion.

    ⬆️ Back to Top

  37. How do you handle redirects in Next.js?

    By adding a redirects property in next.config.js.

    ⬆️ Back to Top

  38. How do you use Sass in a Next.js project?

    By installing sass and importing .scss files in your components.

    ⬆️ Back to Top

  39. What is the basePath option in Next.js?

    It allows you to specify a base path for the application.

    ⬆️ Back to Top

  40. How do you customize the 500 error page in Next.js?

    By creating a 500.js file in the pages directory.

    export default function Custom500() {
      return <h1>500 - Server-side error occurred</h1>;
    }

    ⬆️ Back to Top

  41. What is the trailingSlash option in Next.js?

    It configures whether to include a trailing slash in the URLs.

    ⬆️ Back to Top

  42. What is the difference between push and replace in useRouter?

    push adds a new entry in the history stack, replace replaces the current entry.

    ⬆️ Back to Top

  43. What is ssr: false in dynamic import?

    It disables server-side rendering for the dynamically imported component.

    ⬆️ Back to Top

  44. How do you configure PWA in Next.js?

    By using plugins like next-pwa and configuring next.config.js.

    ⬆️ Back to Top

  45. How do you add Google Analytics to a Next.js project?

    By including the Google Analytics script in _app.js or _document.js.

    ⬆️ Back to Top

  46. What is the purpose of middleware in Next.js?

    To run code before a request is completed.

    ⬆️ Back to Top

  47. How do you use Apollo Client with Next.js?

    By setting up Apollo Provider in _app.js and creating a client.

    ⬆️ Back to Top

  48. What is the publicRuntimeConfig in Next.js?

    Configuration exposed to the browser.

    ⬆️ Back to Top

  49. What is the serverRuntimeConfig in Next.js?

    Configuration only available on the server side.

    ⬆️ Back to Top

  50. What is the purpose of _error.js in Next.js?

    To customize the error page for HTTP errors.

    ⬆️ Back to Top

  51. How do you perform client-side data fetching in Next.js?

    Using useEffect and fetch or any other data-fetching library.

    ⬆️ Back to Top

  52. What is the use of next-seo in Next.js?

    To manage SEO metadata in a Next.js application.

    ⬆️ Back to Top

  53. How do you use Tailwind CSS in Next.js?

    By installing tailwindcss and configuring it with PostCSS.

    ⬆️ Back to Top

  54. How do you configure next-i18next in Next.js?

    By installing next-i18next and setting up the configuration in next.config. js.

    ⬆️ Back to Top

  55. What is next/script used for?

    To optimize loading third-party scripts.

    ⬆️ Back to Top

  56. How do you enable custom fonts in Next.js?

    By using the next/font package or including fonts in the public directory.

    ⬆️ Back to Top

  57. How do you set up GraphQL in Next.js?

    By using Apollo Client or another GraphQL client.

    ⬆️ Back to Top

  58. What is swcMinify in next.config.js?

    It enables the use of the SWC compiler for minification.

    ⬆️ Back to Top

  59. What is the use of next-compose-plugins?

    To enable composition of multiple plugins in Next.js configuration.

    ⬆️ Back to Top

  60. What is a hybrid application in Next.js?

    An application that uses both static generation and server-side rendering.

    ⬆️ Back to Top

App Router

⬆️ Back to Top

  1. What do you create route at app based routing in Next.js?

    Create a directory into app directory & create page file.

        app/page.jsx // /
    
        app/blog/page.jsx // /blog
    
    

    ⬆️ Back to Top

  2. How do you handle CORS in Next.js API routes?

    By setting headers in the API route handlers.

    ⬆️ Back to Top

  3. What is the purpose of rewrites in next.config.js?

    To map an incoming request path to a different destination path.

    ⬆️ Back to Top

  4. How do you manage cookies in Next.js?

    Using libraries like cookie or js-cookie to set and retrieve cookies.

    ⬆️ Back to Top

  5. How do you handle authentication tokens in Next.js?

    By storing tokens in cookies or local storage and sending them with requests.

    ⬆️ Back to Top

  6. What is next-pwa used for?

    To configure and enable Progressive Web App features in Next.js.

    ⬆️ Back to Top

  7. What is the next-sitemap package used for?

    To generate sitemaps for a Next.js application.

    ⬆️ Back to Top

Others

⬆️ Back to Top