Basics Flashcards
What is Next.js
Next.js is a React framework that allows for server-side rendering and provides a set of tools for building web applications. It simplifies the development process and enhances performance by enabling features like server-side rendering, automatic code splitting, and route pre-fetching.
Explain server-side rendering (SSR) in Next.js.
Server-side rendering in Next.js involves rendering React components on the server side instead of the client side. This improves initial page load performance and provides better SEO by delivering fully rendered HTML pages to the client.
What is the purpose of the pages directory in Next.js?
The pages directory in Next.js is a convention that automatically maps files inside it to routes. Each file represents a specific page or route in the application.
How does Next.js handle routing?
Next.js uses file-based routing. Each file inside the **pages **directory corresponds to a route in the application. For example, a file named about.js in the pages directory will create a route for the “/about” path.
What are the advantages of using Next.js over a traditional React application?
Next.js offers advantages like server-side rendering for improved performance, automatic code splitting for optimized loading, simplified routing, and better SEO support.
Explain the concept of data fetching in Next.js.
Next.js provides various methods for fetching data during server-side rendering or on the client side. These include getStaticProps, getServerSideProps, and getInitialProps for different use cases.
What is the purpose of getStaticProps in Next.js?
used to fetch data at build time. It enables pre-rendering of pages with data that doesn’t change frequently, resulting in static HTML files.
How does Next.js handle code splitting?
Next.js automatically performs code splitting by default. Each page in the pages directory becomes a separate chunk, and only the necessary JavaScript is loaded for the requested page.
Explain the purpose of getServerSideProps in Next.js.
used to fetch data on every request, providing server-side rendering with the latest data. It is suitable for pages that require frequently updated data.
What is the role of the _app.js file in Next.js?
The _app.js
file in the pages
directory is used to override the default App
component. It allows for customization of the layout and is commonly used for adding global styles or context providers.
How does Next.js handle CSS styling?
Next.js supports various methods for styling, including inline styles, CSS modules, and third-party libraries like Styled Components. It also allows global styles using the styles
key in the App
component.
What is the purpose of the public directory in Next.js?
Dynamic routes in Next.js can be created using square brackets []
in the filename. For example, a file named [id].js
will match routes like /1
, /2
, etc., and the value will be accessible through the id
parameter.
How can you create dynamic routes in Next.js?
Dynamic routes in Next.js can be created using square brackets []
in the filename. For example, a file named [id].js
will match routes like /1
, /2
, etc., and the value will be accessible through the id
parameter.
Explain the purpose of the _error.js file in Next.js.
The _error.js
file is used to override the default error handling in Next.js. It allows for customizing error pages for different HTTP status codes.
How does Next.js handle environment variables?
Next.js supports environment variables by prefixing them with NEXT_PUBLIC_
for client-side usage. For server-side usage, they can be accessed directly.