Next.js Interview Questions and Answers
Ques 6. What is the difference between 'getServerSideProps' and 'getStaticProps'?
'getServerSideProps' fetches data on every request, while 'getStaticProps' fetches data at build time. 'getStaticProps' is suitable for static content that does not change often.
Ques 7. Explain how to handle dynamic routes in Next.js.
Dynamic routes in Next.js are defined using square brackets in the file name, like '[id].js'. You can use the 'getStaticPaths' and 'getStaticProps' functions to fetch data for dynamic routes.
Ques 8. What is the purpose of the '_app.js' file in Next.js?
The '_app.js' file in Next.js is used to customize the default 'App' component. It is commonly used for persisting layout between pages.
Ques 9. How can you add custom headers to a Next.js response?
You can add custom headers to a Next.js response using the 'res' object in 'getServerSideProps' or by using a custom server.
Ques 10. Explain the purpose of the 'public' directory in Next.js.
The 'public' directory in Next.js is used for serving static assets like images, fonts, and other files. Contents of this directory are served at the root level of the application.
Most helpful rated by users: