NextJS Basics Flashcards

1
Q

What is Nextjs

A

Next js is a lightweight react js framework for static and server-rendered applications

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main features of Next js?

A

Server-rendered by default
Automatic code splitting for faster page loads
Simple client-side routing (page based)
Webpack-based dev environmet which supports Hot Module Replacement (HMR)
Able to implement with Express or any other Node.js HTTP server
Customizable with your own Babel and Webpack configurations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to create pages in next.js?

A

Pages folder contains all pages and routes are defined this way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do I fetch data in Next.js?

A

It’s depends. but next.js recommends getInitialProps is an async function and it can be used to retrieve data from anywhere.

getInitialProps receives a context object with has the following properties:

pathname - path section of URL
query - query string section of URL parsed as an object
asPath - String of the actual path (including the query) shows in the browser
req - HTTP request object (server only)
res - HTTP response object (server only)
err - Error object if any error is encountered during the rendering

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is getInitialProps ?

A

getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data already populated from the server. This is especially useful for SEO.

getInitialProps is used to asynchronously fetch some data, which then populates props.

returned object from getInitialProps is a plain Object

For the initial page load, getInitialProps will run on the server only. getInitialProps will then run on the client when navigating to a different route via the next/link component or by using next/router. However, if getInitialProps is used in a custom _app.js, and the page being navigated to implements getServerSideProps, then getInitialProps will run on the server.

gIP cannot be used in children components, only the default export of every page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the context object of getInitialProps

A

receives a single argument called context, it’s an object with the following properties:

pathname - Current route. That is the path of the page in /pages
query - Query string section of URL parsed as an object
asPath - String of the actual path (including the query) shown in the browser
req - HTTP request object (server only)
res - HTTP response object (server only)
err - Error object if any error is encountered during the rendering

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

getStaticProps

A

fetch data and populate at build time. (static gen)

use if the page is available at build time ahead of a user’s request, if data comes from headless CMS, data can be publicly cached, or page must be pre-rendered for SEO.

If you export an async function called getStaticProps from a page, Next.js will pre-render this page at build time using the props returned by getStaticProps.

export async function getStaticProps(context) {
return {
props: {}, // will be passed to the page component as props
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

getServerSideProps

A

Fetch data on each request. (SSR)

If you export an async function called getServerSideProps from a page, Next.js will pre-render this page on each request using the data returned by getServerSideProps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

getStaticPaths

A

Specify dynamic routes to pre-render based on data. (static gen)

You should use getStaticPaths if you’re statically pre-rendering pages that use dynamic routes.

If a page has dynamic routes (documentation) and uses getStaticProps it needs to define a list of paths that have to be rendered to HTML at build time.

object returned must have a fallback key, boolean, which if false returns a 404 to any paths not returned by 404. If we have a small number of paths to pre-render, we do this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly