NextJS/React Flashcards
studying nextjs react docs (307 cards)
What is the React Server Component Payload (RSC)?
The RSC Payload is a compact binary representation of the rendered React Server Components tree. It’s used by React on the client to update the browser’s DOM. The RSC Payload contains:
- The rendered result of Server Components
- Placeholders for where Client Components should be rendered and references to their JavaScript files
- Any props passed from a Server Component to a Client Component
What are three different server rendering strategies for Server Components?
- Static Rendering
- Dynamic Rendering
- Streaming
What are the benefits of Server side Rendering?
- Security
- Caching
- Performance
- Initial Page Load
- SEO
- Streaming
How are Server Components rendered?
What is Static Rendering (Default)
With Static Rendering, routes are rendered at build time, or in the background after data revalidation. The result is cached and can be pushed to a Content Delivery Network (CDN). This optimization allows you to share the result of the rendering work between users and server requests.
Static rendering is useful when a route has data that is not personalized to the user and can be known at build time, such as a static blog post or a product page.
How does Server Dynamic Rendering work?
With Dynamic Rendering, routes are rendered for each user at request time.
Dynamic rendering is useful when a route has data that is personalized to the user or has information that can only be known at request time, such as cookies or the URL’s search params.
Dynamic Routes with Cached Data
In most websites, routes are not fully static or fully dynamic - it’s a spectrum. For example, you can have an e-commerce page that uses cached product data that’s revalidated at an interval, but also has uncached, personalized customer data.
In Next.js, you can have dynamically rendered routes that have both cached and uncached data. This is because the RSC Payload and data are cached separately. This allows you to opt into dynamic rendering without worrying about the performance impact of fetching all the data at request time.
What happens if, during rendering, NextJS discovers a dynamic function or an uncached data request?
During rendering, NextJS will switch to dynamically rendering the whole route. This table summarizes how dynamic functions and data caching affect whether a route is statically or dynamically rendered
In the table above, for a route to be fully static, all data must be cached. However, you can have a dynamically rendered route that uses both cached and uncached data fetches.
As a developer, do you need to choose between static and dynamic rendering in NextJS?
No, NextJS will decide this for you. NextJS will automatically choose the best rendering strategy for each route based on the features and APIs used.
However, you do need to choose when to cache or revalidate specific data, and you may choose to stream parts of your UI.
What are dynamic functions?
Dynamic functions rely on information that can only be known at request time such as a user’s cookies, current requests headers, or the URL’s search params. In Next.js, these dynamic functions are:
-
cookies()
andheaders():
Using these in a Server Component will opt the whole route into dynamic rendering at request time. - searchParams: Using the Pages prop will opt the page into dynamic rendering at request time.
Using any of these functions will opt the whole route into dynamic rendering at request time.
How does Streaming server rendering work?
Streaming enables you to progressively render UI from the server. Work is split into chunks and streamed to the client as it becomes ready. This allows the user to see parts of the page immediately, before the entire content has finished rendering.
- built into App Router by default
What are Client Components in NextJS?
Client Components allow you to write interactive UI that is prerendered on the server and can use client JavaScript to run in the browser.
What are the benefits of Client Rendering?
How do you use Client Components in Next.js?
To use Client Components, you can add the React “use client” directive at the top of a file, above your imports.
“use client” is used to declare a boundary between a Server and Client Component modules. This means that by defining a “use client” in a file, all other modules imported into it, including child components, are considered part of the client bundle.
Can you define multiple “use client” entry points?
Yes, you can define multiple “use client” entry points in your React Component tree. This allows you to split your application into multiple client bundles.
However, “use client” doesn’t need to be defined in every component that needs to be rendered on the client. Once you define the boundary, all child components and modules imported into it are considered part of the client bundle.
The diagram below shows that using onClick and useState in a nested component (toggle.js) will cause an error if the “use client” directive is not defined. This is because, by default, all components in the App Router are Server Components where these APIs are not available. By defining the “use client” directive in toggle.js, you can tell React to enter the client boundary where these APIs are available.
How are Client Components Rendered?
In Next.js, Client Components are rendered differently depending on whether the request is part of a full page load (an initial visit to your application or a page reload triggered by a browser refresh) or a subsequent navigation.
How are Client Components Rendered on Full page load?
To optimize the initial page load, Next.js will use React’s APIs to render a static HTML preview on the server for both Client and Server Components. This means, when the user first visits your application, they will see the content of the page immediately, without having to wait for the client to download, parse, and execute the Client Component JavaScript bundle.
On the server:
- React renders Server Components into a special data format called the React Server Component Payload (RSC Payload), which includes references to Client Components.
- Next.js uses the RSC Payload and Client Component JavaScript instructions to render HTML for the route on the server.
Then, on the client:
- The HTML is used to immediately show a fast non-interactive initial preview of the route.
- The React Server Components Payload is used to reconcile the Client and Server Component trees, and update the DOM.
- The JavaScript instructions are used to hydrate Client Components and make their UI interactive.
What is hydration?
Hydration is the process of attaching event listeners to the DOM, to make the static HTML interactive. Behind the scenes, hydration is done with the hydrateRoot React API.
How are Client Components Rendered on Subsequent Navigations?
On subsequent navigations, Client Components are rendered entirely on the client, without the server-rendered HTML.
This means the Client Component JavaScript bundle is downloaded and parsed. Once the bundle is ready, React will use the RSC Payload to reconcile the Client and Server Component trees, and update the DOM.
When to use Server and Client Components?
What are some common patterns for working with Server Components?
- Sharing data between components
- Keeping Server-only code out of the Client Environment
- Using Third-party Packages and Providers
- Using Context Providers
What are some common patterns for working with Client Components?
- Moving Client Components Down the Tree
- Serialization - passing props from Server to Client Components
- Interleaving Server and Client Components
- anti pattern: importing server comps into client comps
- supported pattern: passing Server Comps to Client Components as props
Explain the Server Component pattern where you Share data between components.
When fetching data on the server, there may be cases where you need to share data across different components. For example, you may have a layout and a page that depend on the same data.
Instead of using React Context (which is not available on the server) or passing data as props, you can use fetch or React’s cache function to fetch the same data in the components that need it, without worrying about making duplicate requests for the same data. This is because React extends fetch to automatically memoize data requests, and the cache function can be used when fetch is not available.
Explain the Server Component pattern: Keeping Server-only Code out of the Client Environment
To prevent this sort of unintended client usage of server code, we can use the server-only
package to give other developers a build-time error if they ever accidentally import one of these modules into a Client Component.
To use server-only, first install the package:
Then import the package into any module that contains server-only code:
Now, any Client Component that imports getData() will receive a build-time error explaining that this module can only be used on the server.
The corresponding package client-only can be used to mark modules that contain client-only code – for example, code that accesses the window object.