Cherreads

Chapter 8 - Next.js

1) What is Next.js?

Answer: A React framework for building server-rendered and statically generated web applications.

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

Answer: Using npx create-next-app command.

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

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

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

Answer: Routing based on the file structure in the pages directory.

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

Answer: By using square brackets in the filename, e.g., [id].js.

6) What is getStaticProps?

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

7) What is getServerSideProps?

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

8) What is getStaticPaths?

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

9) What is the difference between getStaticProps and getServerSideProps?

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

10) What is the Link component in Next.js?

Answer: A component for client-side navigation between pages.

11) What is the useRouter hook in Next.js?

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

12) How do you navigate programmatically in Next.js?

Answer: Using useRouter().push('/path').

13) What is the _app.js file in Next.js?

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

14) What is the _document.js file in Next.js?

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

15) How do you add global CSS in Next.js?

Answer: By importing the CSS file in _app.js.

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

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

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

Answer: Pre-rendering pages at build time.

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

Answer: Rendering pages on each request.

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

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

20) What is the Image component in Next.js?

Answer: A component that optimizes images for faster loading.

21) What is next.config.js?

Answer: A configuration file to customize Next.js settings.

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

Answer: By adding a tsconfig.json file.

23) What is API Routes in Next.js?

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

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

Answer: By connecting the repository to Vercel and deploying it.

25) What is pre-rendering in Next.js?

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

26) What is the difference between static generation and server-side rendering?

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

27) How do you handle redirects in Next.js?

Answer: By configuring redirects in next.config.js.

28) What is the Head component in Next.js?

Answer: A component for modifying the of a page.

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

Answer: Using getStaticProps or getServerSideProps.

30) What is dynamic import in Next.js?

Answer: A feature to load components or modules dynamically.

31) How do you handle environment variables in Next.js?

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

32) What is fallback in getStaticPaths?

Answer: Determines how to handle missing paths, with true, false, or 'blocking'.

33) What is a custom server in Next.js?

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

34) What is the next/head package used for?

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

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

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

36) What is the use of next export command?

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

37) How do you optimize fonts in Next.js?

Answer: By using the built-in font optimization feature.

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

Answer: Port 3000.

39) How do you add custom headers in Next.js?

Answer: By configuring headers in next.config.js.

40) What is Fast Refresh in Next.js?

Answer: A feature for quick feedback when editing React components.

41) What is the public folder in Next.js?

Answer: A folder for static assets served from the root URL.

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

Answer: By adding a babel.config.js file.

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

Answer: By configuring i18n settings in next.config.js.

44) What is React Strict Mode in Next.js?

Answer: A development mode that highlights potential problems in an application.

45) What is a singleton router in Next.js?

Answer: A single router instance accessible across the application.

46) What is the useTranslation hook in Next.js?

Answer: A hook for handling translations when using i18n.

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

Answer: By adding _error.js in the pages directory.

48) What is AMP in Next.js?

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

49) How do you enable AMP in Next.js?

Answer: By adding amp attribute to a page component.

50) What is the next/image component used for?

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

51) What is the next/link component used for?

Answer: For client-side navigation between pages.

52) What is the difference between pages and components directories?

Answer: pages contains routable components, components contains reusable UI components.

53) How do you handle middleware in Next.js?

Answer: Using middleware functions in next.config.js.

54) How do you add polyfills in Next.js?

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

55) What is the difference between client-side and server-side rendering in Next.js?

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

56) What is static optimization in Next.js?

Answer: Automatically determining if a page can be statically generated.

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

Answer: Using useEffect and fetch or other data fetching libraries.

58) How do you implement authentication in Next.js?

Answer: Using libraries like NextAuth.js or custom authentication logic.

59) What is the difference between _app.js and _document.js?

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

60) How do you create API endpoints in Next.js?

Answer: By adding files to the pages/api directory.

61) What is ISR in Next.js?

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

62) How do you configure Webpack in Next.js?

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

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

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

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

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

65) What is the purpose of next/dynamic?

Answer: To enable dynamic imports and code splitting.

66) How do you add meta tags in Next.js?

Answer: Using the Head component from next/head.

67) What is the purpose of next-compose-plugins?

Answer: To compose multiple plugins in next.config.js.

68) How do you handle form submissions in Next.js?

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

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

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

70) What is the purpose of next/router?

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

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

Answer: Using libraries like styled-components or Emotion.

72) How do you handle redirects in Next.js?

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

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

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

74) What is the basePath option in Next.js?

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

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

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

76) What is the trailingSlash option in Next.js?

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

77) What is the difference between push and replace in useRouter?

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

78) What is ssr: false in dynamic import?

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

79) How do you configure PWA in Next.js?

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

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

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

81) What is the purpose of middleware in Next.js?

Answer: To run code before a request is completed.

82) How do you use Apollo Client with Next.js?

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

83) What is the publicRuntimeConfig in Next.js?

Answer: Configuration exposed to the browser.

84) What is the serverRuntimeConfig in Next.js?

Answer: Configuration only available on the server side.

85) What is the purpose of _error.js in Next.js?

Answer: To customize the error page for HTTP errors.

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

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

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

Answer: To manage SEO metadata in a Next.js application.

88) How do you use Tailwind CSS in Next.js?

Answer: By installing tailwindcss and configuring it with PostCSS.

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

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

90) What is next/script used for?

Answer: To optimize loading third-party scripts.

91) How do you enable custom fonts in Next.js?

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

92) How do you set up GraphQL in Next.js?

Answer: By using Apollo Client or another GraphQL client.

93) What is swcMinify in next.config.js?

Answer: It enables the use of the SWC compiler for minification.

94) What is the use of next-compose-plugins?

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

95) What is a hybrid application in Next.js?

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

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

Answer: By setting headers in the API route handlers.

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

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

98) How do you manage cookies in Next.js?

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

99) How do you handle authentication tokens in Next.js?

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

100) What is next-pwa used for?

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

101) What is the next-sitemap package used for?

Answer: To generate sitemaps for a Next.js application.

More Chapters