Next.js Flashcards

1
Q

In Next.js, what is a page?

A

In Next.js, a page is a React Component exported from a file in the pages directory.

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

What does the Link component enable in Next.js ?

A

The Link component enables client-side navigation between two pages in the same Next.js app.

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

What is client side navigation?

A

Client-side navigation means that the page transition happens using JavaScript, which is faster than the default navigation done by the browser.

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

Next.js does code splitting automatically, what does this mean?

A

Next.js does code splitting automatically, so each page only loads what’s necessary for that page. That means when the homepage is rendered, the code for other pages is not served initially.

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

How do you link to an external page outside the Next.js app?

A

If you need to link to an external page outside the Next.js app, just use an <a> tag without Link.</a>

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

What is cumulative layout shift? How is this related to Next.js?

A

It is a core web vital that google uses in search ranking.

Cumulative Layout Shift (CLS) is an important, user-centric metric for measuring visual stability because it helps quantify how often users experience unexpected layout shifts—a low CLS helps ensure that the page is delightful.

Next.js has an image component that optimizes images. It loads the image in a way that avoids any layout shift.

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

What is hydration?

A

Hydration is when a prerendered HTML page receives the JavaScript to make it fully functional. The HTML page initially loads with very little JavaScript.

From the Next.js starting guide
Each generated HTML is associated with minimal JavaScript code necessary for that page. When a page is loaded by the browser, its JavaScript code runs and makes the page fully interactive. (This process is called hydration.)

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

What is pre rendering in Next.js ?

A

This is when a pre-rendered HTML page is loaded first and then the JavaScript (React code) that makes it functional is loaded after. This gives the page load the appearance of being very fast.

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

Next.js has two forms of pre-rendering, what are they?

A

Static Generation and Server-side Rendering.

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

What is Static Generation?

A

Static Generation is the pre-rendering method that generates the HTML at build time. The pre-rendered HTML is then reused on each request.

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

What is Server-side rendering?

A

Server-side Rendering is the pre-rendering method that generates the HTML on each request.

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

When deciding whether you should use static generation what question should you ask yourself?

A

You should ask yourself: “Can I pre-render this page ahead of a user’s request?” If the answer is yes, then you should choose Static Generation.

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

When is static generation not a good idea?

A

On the other hand, Static Generation is not a good idea if you cannot pre-render a page ahead of a user’s request. Maybe your page shows frequently updated data, and the page content changes on every request.

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

What is static generation with data?

A

This is when a static HTML page is generated but some data that goes into it is retrieved before it is generated .

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

How do you use static generation with data?

A

when you export a page component, you can also export an async function called getStaticProps. If you do this, then:

getStaticProps runs at build time in production, and…
Inside the function, you can fetch external data and send it as props to the page.

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

Where does getStaticProps run?

A

Server side

17
Q

How do you use server side rendering in Next.js ?

A

You need to export getServerSideProps from your page.

18
Q

What three things does a page file need to contain to statically generate pages with dynamic routes? Assume the path is /posts/<id></id>

A

The page file must contain…

  1. A React component to render this page.
  2. getStaticPaths which returns an array of possible values for id.
  3. getStaticProps which fetches necessary data for the post with id.
19
Q

When does getStaticPaths run in both development and production?

A

In development (npm run dev or yarn dev), getStaticPaths runs on every request.
In production, getStaticPaths runs at build time.

20
Q

Does the _document.js file get rendered client side?

A

No, Next.js is configured in a way that this _document.js file is only being rendered on the server.

21
Q

Next.js has support for API Routes, what does this let you create?

A

It lets you easily create an API endpoint as a Node.js serverless function.

22
Q

Can getStaticProps and getStaticPaths run on the server-side?

A

yes BUT getStaticProps and getStaticPaths run ONLY on the server-side and will never run on the client-side.