Skip to content

mrhrifat/nextjs-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

NextJS Interview Questions & Answers

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


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 How do you create a dynamic route in Next.js?
6 What is getStaticProps?
7 What is getServerSideProps?
8 What is getStaticPaths?
9 What is the difference between getStaticProps and getServerSideProps?
10 What is the Link component in Next.js?
11 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 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?

  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

    ⬆️ 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. How many ways we can manage routing of application with Next.js?

    We can manage or create routing of application in 2 ways currently.

    • Pages Router
    • App Router (Support from v13)

    ⬆️ Back to Top

  5. What is file-based routing in Next.js?

    Routing based on the file structure in the pages directory.

    ⬆️ Back to Top

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

    By using square brackets in the filename

    blogs/[id].js.

    blogs/[id]/index.js

    ⬆️ 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

  1. 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

  1. What is the difference between getStaticProps and getServerSideProps?

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

    ⬆️ Back to Top

  2. 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

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

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

    ⬆️ Back to Top

  4. 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

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

    The _app.js file in a Next.js application is used to initialize pages and configure global settings for your app. It wraps every page with a common layout or context providers, allowing you to manage global styles, state, or functionality that should be shared across all pages.

    // pages/_app.js
    import '../styles/globals.css';
    
    function MyApp({ Component, pageProps }) {
      return <Component {...pageProps} />;
    }
    
    export default MyApp;

    In this example, _app.js imports global CSS and defines the MyApp component that renders the current page (Component) with its associated props (pageProps). This setup ensures that global styles and common functionality are applied across all pages in your Next.js application.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

    By importing the CSS file in _app.js.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

    Pre-rendering pages at build time.

    ⬆️ Back to Top

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

    Rendering pages on each request.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

    A component that optimizes images for faster loading.

    ⬆️ Back to Top

  13. What is next.config.js?

    A configuration file to customize Next.js settings.

    ⬆️ Back to Top

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

    By adding a tsconfig.json file.

    ⬆️ Back to Top

  15. What is API Routes in Next.js?

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

    ⬆️ Back to Top

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

    By connecting the repository to Vercel and deploying it.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  18. 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

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

    By configuring redirects in next.config.js.

    ⬆️ Back to Top

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

    A component for modifying the of a page.

    ⬆️ Back to Top

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

    Using getStaticProps or getServerSideProps.

    ⬆️ Back to Top

  22. What is dynamic import in Next.js?

    A feature to load components or modules dynamically.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  24. What is fallback in getStaticPaths?

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  28. What is the use of next export command?

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

    ⬆️ Back to Top

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

    By using the built-in font optimization feature.

    ⬆️ Back to Top

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

    Port 3000.

    ⬆️ Back to Top

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

    By configuring headers in next.config.js.

    ⬆️ Back to Top

  32. What is Fast Refresh in Next.js?

    A feature for quick feedback when editing React components.

    ⬆️ Back to Top

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

    A folder for static assets served from the root URL.

    ⬆️ Back to Top

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

    By adding a babel.config.js file.

    ⬆️ Back to Top

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

    By configuring i18n settings in next.config.js.

    ⬆️ Back to Top

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

    A development mode that highlights potential problems in an application.

    ⬆️ Back to Top

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

    A single router instance accessible across the application.

    ⬆️ Back to Top

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

    A hook for handling translations when using i18n.

    ⬆️ Back to Top

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

    By adding _error.js in the pages directory.

    ⬆️ Back to Top

  40. What is AMP in Next.js?

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

    ⬆️ Back to Top

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

    By adding amp attribute to a page component.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

    For client-side navigation between pages.

    ⬆️ Back to Top

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

    pages contains routable components, components contains reusable UI components.

    ⬆️ Back to Top

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

    Using middleware functions in next.config.js.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  47. 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

  48. What is static optimization in Next.js?

    Automatically determining if a page can be statically generated.

    ⬆️ Back to Top

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

    Using useEffect and fetch or other data fetching libraries.

    ⬆️ Back to Top

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

    Using libraries like NextAuth.js or custom authentication logic.

    ⬆️ Back to Top

  51. 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

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

    By adding files to the pages/api directory.

    ⬆️ Back to Top

  53. What is ISR in Next.js?

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  57. What is the purpose of next/dynamic?

    To enable dynamic imports and code splitting.

    ⬆️ Back to Top

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

    Using the Head component from next/head.

    ⬆️ Back to Top

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

    To compose multiple plugins in next.config.js.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  61. 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

  62. What is the purpose of next/router?

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

    ⬆️ Back to Top

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

    Using libraries like styled-components or Emotion.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  69. 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

  70. What is ssr: false in dynamic import?

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  72. 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

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

    To run code before a request is completed.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

  75. What is the publicRuntimeConfig in Next.js?

    Configuration exposed to the browser.

    ⬆️ Back to Top

  76. What is the serverRuntimeConfig in Next.js?

    Configuration only available on the server side.

    ⬆️ Back to Top

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

    To customize the error page for HTTP errors.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

    To manage SEO metadata in a Next.js application.

    ⬆️ Back to Top

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

    By installing tailwindcss and configuring it with PostCSS.

    ⬆️ Back to Top

  81. 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

  82. What is next/script used for?

    To optimize loading third-party scripts.

    ⬆️ Back to Top

  83. 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

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

    By using Apollo Client or another GraphQL client.

    ⬆️ Back to Top

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

    It enables the use of the SWC compiler for minification.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

    By setting headers in the API route handlers.

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

    ⬆️ Back to Top

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

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

[:arrow_up: Back to Top](#table-of-contents)
  1. What is next-pwa used for?

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

    ⬆️ Back to Top

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

    To generate sitemaps for a Next.js application.

    ⬆️ Back to Top