NextJS/React Flashcards

studying nextjs react docs (307 cards)

1
Q

What is the React Server Component Payload (RSC)?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are three different server rendering strategies for Server Components?

A
  • Static Rendering
  • Dynamic Rendering
  • Streaming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the benefits of Server side Rendering?

A
  • Security
  • Caching
  • Performance
  • Initial Page Load
  • SEO
  • Streaming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are Server Components rendered?

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

What is Static Rendering (Default)

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does Server Dynamic Rendering work?

A

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.

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

Dynamic Routes with Cached Data

A

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.

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

What happens if, during rendering, NextJS discovers a dynamic function or an uncached data request?

A

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.

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

As a developer, do you need to choose between static and dynamic rendering in NextJS?

A

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.

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

What are dynamic functions?

A

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() and headers(): 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Streaming server rendering work?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are Client Components in NextJS?

A

Client Components allow you to write interactive UI that is prerendered on the server and can use client JavaScript to run in the browser.

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

What are the benefits of Client Rendering?

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

How do you use Client Components in Next.js?

A

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.

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

Can you define multiple “use client” entry points?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How are Client Components Rendered?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How are Client Components Rendered on Full page load?

A

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:

  1. React renders Server Components into a special data format called the React Server Component Payload (RSC Payload), which includes references to Client Components.
  2. Next.js uses the RSC Payload and Client Component JavaScript instructions to render HTML for the route on the server.

Then, on the client:

  1. The HTML is used to immediately show a fast non-interactive initial preview of the route.
  2. The React Server Components Payload is used to reconcile the Client and Server Component trees, and update the DOM.
  3. The JavaScript instructions are used to hydrate Client Components and make their UI interactive.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is hydration?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How are Client Components Rendered on Subsequent Navigations?

A

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.

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

When to use Server and Client Components?

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

What are some common patterns for working with Server Components?

A
  1. Sharing data between components
  2. Keeping Server-only code out of the Client Environment
  3. Using Third-party Packages and Providers
  4. Using Context Providers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are some common patterns for working with Client Components?

A
  1. Moving Client Components Down the Tree
  2. Serialization - passing props from Server to Client Components
  3. Interleaving Server and Client Components
    - anti pattern: importing server comps into client comps
    - supported pattern: passing Server Comps to Client Components as props
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Explain the Server Component pattern where you Share data between components.

A

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.

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

Explain the Server Component pattern: Keeping Server-only Code out of the Client Environment

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Explain the Server Component pattern for using third-party packages and providers.
Since Server Components are a new React feature, third-party packages and providers don't yet have the "use client" directive, and so won't work in Server Components. The Solution is wrap third-party components that rely on client-only features in your own Client Components. You won't need to do this for every component, but you do need to for providers b/c they reply on React state and context and are typically needed at the root of the application.
26
How can you use Context Providers with Server Components? (React context is not supported in SCs)
create context in a Client Component using "use client" and then import it into a Server Component. Good to know: You should render providers as deep as possible in the tree – notice how ThemeProvider only wraps {children} instead of the entire document. This makes it easier for Next.js to optimize the static parts of your Server Components.
27
Explain the Client Component pattern: Moving Client Components Down the Tree.
To reduce the Client JavaScript bundle size, we recommend moving Client Components down your component tree. For example, you may have a Layout that has static elements (e.g. logo, links, etc) and an interactive search bar that uses state. Instead of making the whole layout a Client Component, move the interactive logic to a Client Component (e.g. ) and keep your layout as a Server Component. This means you don't have to send all the component Javascript of the layout to the client.
28
Explain the Client Component pattern: Passing props from Server to Client Components (Serialization)
If you fetch data in a Server Component, you may want to pass data down as props to Client Components. Props passed from the Server to Client Components need to be serializable by React. If your Client Components depend on data that is not serializable, you can fetch data on the client with a third party library or on the server via a Route Handler.
29
Explain the Client Component pattern: Interleaving Server and Client Components.
When interleaving Client and Server Components, it may be helpful to visualize your UI as a tree of components. Starting with the root layout, which is a Server Component, you can then render certain subtrees of components on the client by adding the "use client" directive. Within those client subtrees, you can still nest Server Components or call Server Actions, however there are some things to keep in mind: - During a request-response lifecycle, your code moves from the server to the client. If you need to access data or resources on the server while on the client, you'll be making a new request to the server - not switching back and forth. - When a new request is made to the server, all Server Components are rendered first, including those nested inside Client Components. The rendered result (RSC Payload) will contain references to the locations of Client Components. Then, on the client, React uses the RSC Payload to reconcile Server and Client Components into a single tree. - Since Client Components are rendered after Server Components, you cannot import a Server Component into a Client Component module (since it would require a new request back to the server). Instead, you can pass a Server Component as props to a Client Component. See the unsupported pattern and supported pattern sections below.
30
Unsupported Pattern: Importing Server Components into Client Components - what does it look like?
31
Supported Pattern: Passing Server Components to Client Components as Props - what does it look like?
The following pattern is supported. You can pass Server Components as a prop to a Client Component. A common pattern is to use the React children prop to create a "slot" in your Client Component. With this approach, and are decoupled and can be rendered independently. In this case, the child can be rendered on the server, well before is rendered on the client. Good to know: - The pattern of "lifting content up" has been used to avoid re-rendering a nested child component when a parent component re-renders. - You're not limited to the children prop. You can use any prop to pass JSX.
32
What are runtimes in the context of Next.js
In the context of Next.js, runtime refers to the set of libraries, APIs, and general functionality available to your code during execution. On the server, there are two runtimes where parts of your application code can be rendered: - The Node.js Runtime (default) has access to all Node.js APIs and compatible packages from the ecosystem. - The Edge Runtime is based on Web APIs.
33
What are the differences to consider when choosing a runtime in NextJS?
34
What is the Edge Runtime in NextJS and when should you use it?
In Next.js, the lightweight Edge Runtime is a subset of available Node.js APIs. The Edge Runtime is ideal if you need to deliver dynamic, personalized content at low latency with small, simple functions. The Edge Runtime's speed comes from its minimal use of resources, but that can be limiting in many scenarios. For example, code executed in the Edge Runtime on Vercel cannot exceed between 1 MB and 4 MB, this limit includes imported packages, fonts and files, and will vary depending on your deployment infrastructure. In addition, the Edge Runtime does not support all Node.js APIs meaning some npm packages may not work. For example, "Module not found: Can't resolve 'fs'" or similar errors. We recommend using the Node.js runtime if you need to use these APIs or packages.
35
What is the Node.js Runtime in NextJS and when should you use it?
Using the Node.js runtime gives you access to all Node.js APIs, and all npm packages that rely on them. However, it's not as fast to start up as routes using the Edge runtime. Deploying your Next.js application to a Node.js server will require managing, scaling, and configuring your infrastructure. Alternatively, you can consider deploying your Next.js application to a serverless platform like Vercel, which will handle this for you.
36
When should you use Serverless Node.js? What are the downsides to using it?
Serverless is ideal if you need a scalable solution that can handle more complex computational loads than the Edge Runtime. With Serverless Functions on Vercel, for example, your overall code size is 50MB including imported packages, fonts, and files. The downside compared to routes using the Edge is that it can take hundreds of milliseconds for Serverless Functions to boot up before they begin processing requests. Depending on the amount of traffic your site receives, this could be a frequent occurrence as the functions are not frequently "warm".
37
With SSR (Server Side Rendering), what are the series of steps that need to be completed before a user can see and interact with a page?
1. First, all data for a given page is fetched on the server. 2. The server then renders the HTML for the page. 3. The HTML, CSS, and JavaScript for the page are sent to the client. 4. A non-interactive user interface is shown using the generated HTML, and CSS. 5. Finally, React hydrates the user interface to make it interactive. These steps are sequential and blocking, meaning the server can only render the HTML for a page once all the data has been fetched. And, on the client, React can only hydrate the UI once the code for all components in the page has been downloaded.
38
How does SSR help improve the perceived loading performance? What are the limitations?
By showing a non-interactive page to the user as soon as possible. However, it can still be slow as all data fetching on server needs to be completed before the page can be shown to the user.
39
What are the benefits of Streaming rendering?
Streaming allows you to break down the page's HTML into smaller chunks and progressively send those chunks from the server to the client. This enables parts of the page to be displayed sooner, without waiting for all the data to load before any UI can be rendered. Streaming works well with React's component model because each component can be considered a chunk. Components that have higher priority (e.g. product information) or that don't rely on data can be sent first (e.g. layout), and React can start hydration earlier. Components that have lower priority (e.g. reviews, related products) can be sent in the same server request after their data has been fetched. Streaming is particularly beneficial when you want to prevent long data requests from blocking the page from rendering as it can reduce the Time To First Byte (TTFB) and First Contentful Paint (FCP). It also helps improve Time to Interactive (TTI), especially on slower devices.
40
What is Time to First Byte (TTFB)?
Time to First Byte (TTFB) is a foundational metric for measuring connection setup time and web server responsiveness in both the lab and the field. It measures the time between the request for a resource and when the first byte of a response begins to arrive. This makes it helpful in identifying when a web server is too slow to respond to requests. In the case of navigation requests—that is, requests for an HTML document—it precedes every other meaningful loading performance metric. TTFB is the sum of the following request phases: - Redirect time - Service worker startup time (if applicable) - DNS lookup - Connection and TLS negotiation Request, until the first byte of the response arrives - Reducing latency in connection setup time and on the backend helps lower your TTFB.
41
What is a good TTFB score?
As a rough guide, most sites should strive to have a TTFB of 0.8 seconds or less. Because TTFB happens before user-centric metrics such as First Contentful Paint (FCP) and Largest Contentful Paint (LCP), we recommend that your server respond to navigation requests quickly enough so that the 75th percentile of users experience an FCP within the "good" threshold. Key point: Because TTFB isn't a Core Web Vitals metric, it's not absolutely necessary for sites to have an excellent TTFB, as long as the longer TTFB doesn't make it harder for your site to score well on the metrics that matter. When optimizing load times, consider how your site delivers content. A low TTFB is especially important if a site delivers its initial markup quickly and then has to wait for scripts to populate it with meaningful content, as is often the case with Single Page Applications (SPAs). On the other hand, a server-rendered site that doesn't require much client-side work can have better FCP and LCP values than a client-rendered site, even if its TTFB is higher.
42
What is First Contentful Paint? (FCP)
First Contentful Paint (FCP) is one of six metrics tracked in the Performance section of the Lighthouse report. Each metric captures some aspect of page load speed. FCP measures how long it takes the browser to render the first piece of DOM content after a user navigates to your page. Images, non-white elements, and SVGs on your page are considered DOM content; anything inside an iframe isn't included.
43
What is Time to Interactive (TTI)? Why is it important to measure?
TTI measures how long it takes a page to become fully interactive. A page is considered fully interactive when: - The page displays useful content, which is measured by the First Contentful Paint, - Event handlers are registered for most visible page elements, and - The page responds to user interactions within 50 milliseconds. Measuring TTI is important because some sites optimize content visibility at the expense of interactivity. This can create a frustrating user experience: the site appears to be ready, but when the user tries to interact with it, nothing happens. sites performing in the ninety-ninth percentile render TTI in about 2.2 seconds. If your website's TTI is 2.2 seconds, your TTI score is 99.
44
How do you improve your TTI score?
One improvement that can have a particularly big effect on TTI is deferring or removing unnecessary JavaScript work. Look for opportunities to optimize your JavaScript. In particular, consider reducing JavaScript payloads with code splitting and applying the PRPL pattern. Optimizing third-party JavaScript also yields significant improvements for some sites. These two Diagnostic audits provide additional opportunities to reduce JavaScript work: - Minimize main thread work - Reduce JavaScript execution time
45
How does the Suspense component work? what are the benefits?
works by wrapping a component that performs an asynchronous action (e.g. fetch data), showing fallback UI (e.g. skeleton, spinner) while it's happening, and then swapping in your component once the action completes. By using Suspense, you get the benefits of: 1. Streaming Server Rendering - Progressively rendering HTML from the server to the client. 2.Selective Hydration - React prioritizes what components to make interactive first based on user interaction.
46
How does Streaming with Suspense interact with SEO?
- Next.js will wait for data fetching inside `generateMetadata` to complete before streaming UI to the client. This guarantees the first part of a streamed response includes tags. - Since streaming is server-rendered, it does not impact SEO. You can use the Rich Results Test tool from Google to see how your page appears to Google's web crawlers and view the serialized HTML (source).
47
How do Status Codes work when Streaming w/ Suspense?
When streaming, a 200 status code will be returned to signal that the request was successful. The server can still communicate errors or issues to the client within the streamed content itself, for example, when using `redirect` or `notFound`. Since the response headers have already been sent to the client, the status code of the response cannot be updated. This does not affect SEO.
48
What are Server Actions? How do you use them?
Server Actions are asynchronous functions that are executed on the server. They can be used in Server and Client Components to handle form submissions and data mutations in Next.js applications. A Server Action can be defined with the React "use server" directive. You can place the directive at the top of an async function to mark the function as a Server Action, or at the top of a separate file to mark all exports of that file as Server Actions.
49
How do you use a Server Action in a Server Component?
Server Components can use the inline function level or module level "use server" directive. To inline a Server Action, add "use server" to the top of the function body:
50
How do you use Server Actions in a Client Component?
Client Components can only import actions that use the module-level "use server" directive. To call a Server Action in a Client Component, create a new file and add the "use server" directive at the top of it. All functions within the file will be marked as Server Actions that can be reused in both Client and Server Components:
51
Can you pass a Server Action to a Client Component as a prop? How?
52
How do you invoke a Server Action? Describe the behavior.
53
What does it mean that Server Components support progressive enhancement by default?
Server Components support progressive enhancement by default, meaning the form will be submitted even if JavaScript hasn't loaded yet or is disabled.
54
Are Server Actions limited to
elements?
No, Server Actions are not limited to and can be invoked from event handlers, useEffect, third-party libraries, and other form elements like
55
Which part of the NextJS architecture do Server Actions interact with?
Server Actions integrate with the Next.js caching and revalidation architecture. When an action is invoked, Next.js can return both the updated UI and new data in a single server roundtrip.
56
True or False: actions can use multiple types of HTTP methods.
False. Behind the scenes, actions use the POST method, and only this HTTP method can invoke them.
57
What does React require of args and return values for Server Actions?
The arguments and return value of Server Actions must be serializable by React. See the React docs for a list of serializable arguments and values.
58
What does it mean that "Server Actions are functions"?
Server Actions are functions. This means they can be reused anywhere in your application.
59
What runtime do Server Actions use?
Server Actions inherit the runtime from the page or layout they are used on.
60
Where do Server Actions get their Route Segment Config?
Server Actions inherit the Route Segment Config from the page or layout they are used on, including fields like maxDuration.
61
What happens when Server Actions are invoked in a ?
React extends the HTML element to allow Server Actions to be invoked with the action prop. When invoked in a form, the action automatically receives the FormData object. You don't need to use React useState to manage fields, instead, you can extract the data using the native FormData methods: Good to know: - Example: Form with Loading & Error States - When working with forms that have many fields, you may want to consider using the `entries()` method with JavaScript's `Object.fromEntries()`. For example: const rawFormData = Object.fromEntries(formData). One thing to note is that the `formData` will include additional `$ACTION`_ properties. - See React documentation to learn more.
62
How do you pass additional arguments to Server Actions in a form?
You can pass additional arguments to a Server Action using the JavaScript bind method.
63
How do you handle pending states in forms using Server Actions?
You can use the React `useFormStatus` hook to show a pending state while the form is being submitted.
64
Can you use `useFormStatus` in a Server Component?
No, `useFormStatus` is a React hook and therefore must be used in a Client Component.
65
How do Server Actions allow you to do server-side validation and error handling with forms?
We recommend using HTML validation like required and type="email" for basic client-side form validation. For more advanced server-side validation, you can use a library like zod to validate the form fields before mutating the data. Once the fields have been validated on the server, you can return a serializable object in your action and use the React `useFormState` hook to show a message to the user. By passing the action to `useFormState`, the action's function signature changes to receive a new `prevState` or `initialState` parameter as its first argument. `useFormState` is a React hook and therefore must be used in a Client Component.
66
What does the React `useOptimistic` hook do?
You can use the React `useOptimistic` hook to optimistically update the UI before the Server Action finishes, rather than waiting for the response:
67
Can you invoke Server Actions in elements nested in forms? If so, why would you want to?
You can invoke a Server Action in elements nested inside such as
68
What is programmatic form submission? When and how do you use it?
You can trigger a form submission using the `requestSubmit()` method. For example, when the user presses ⌘ + Enter, you can listen for the onKeyDown event: This will trigger the submission of the nearest ancestor, which will invoke the Server Action.
69
Aside from forms, where else are Server Actions commonly invoked?
they can also be invoked from other parts of your code such as event handlers and useEffect.
70
What do you need to consider when invoking Server Actions in Event Handlers? Give an example.
You can invoke a Server Action from event handlers such as `onClick`. For example, to increment a like count: To improve the user experience, we recommend using other React APIs like `useOptimistic` and `useTransition` to update the UI before the Server Action finishes executing on the server, or to show a pending state. For cases like `onChange` in a form field, where multiple events might be fired in quick succession, we recommend *debouncing* to prevent unnecessary Server Action invocations.
71
How do you use the `useEffect` hook to invoke a Server Action? When might this be useful?
You can use the React `useEffect` hook to invoke a Server Action when the component mounts or a dependency changes. This is useful for mutations that depend on global events or need to be triggered automatically. For example, `onKeyDown` for app shortcuts, an intersection observer hook for infinite scrolling, or when the component mounts to update a view count:
72
How do Server Actions handle errors?
When an error is thrown, it'll be caught by the nearest error.js or boundary on the client. We recommend using try/catch to return errors to be handled by your UI. For example, your Server Action might handle errors from creating a new item by returning a message: Good to know: Aside from throwing the error, you can also return an object to be handled by useFormState. See Server-side validation and error handling.
73
How do you revalidate data in the Next.js Cache inside of Server Actions?
You can revalidate the Next.js Cache inside your Server Actions with the `revalidatePath` API Or invalidate a specific data fetch with a cache tag using `revalidateTag`:
74
How do Server Actions handle redirects?
If you would like to redirect the user to a different route after the completion of a Server Action, you can use `redirect` API. `redirect` needs to be called outside of the `try/catch` block:
75
Do Server Actions interact with Cookies? If so, how?
You can `get`, `set`, and `delete` cookies inside a Server Action using the `cookies` API:
76
In the context of Authentication and authorization, how should you treat Server Actions?
You should treat Server Actions as you would public-facing API endpoints, and ensure that the user is authorized to perform the action. For example:
77
When does defining a Server Action create a closure?
Defining a Server Action inside a component creates a closure where the action has access to the outer function's scope. For example, the publish action has access to the publishVersion variable. Closures are useful when you need to capture a snapshot of data (e.g. publishVersion) at the time of rendering so that it can be used later when the action is invoked. However, for this to happen, the captured variables are sent to the client and back to the server when the action is invoked. Next.js automatically encrypts these variables to prevent data exposure.
78
What do you need to be aware of when self-hosting your Next.js application across multiple servers?
Overwriting encryption keys (advanced). When self-hosting your Next.js application across multiple servers, each server instance may end up with a different encryption key, leading to potential inconsistencies. To mitigate this, you can overwrite the encryption key using the `process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` environment variable. Specifying this variable ensures that your encryption keys are persistent across builds, and all server instances use the same key. This is an advanced use case where consistent encryption behavior across multiple deployments is critical for your application. You should consider standard security practices such key rotation and signing. Good to know: Next.js applications deployed to Vercel automatically handle this.
79
What does Next.js do to prevent sensitive data from getting sent to the client in the case of Server Action closures? What are the implications of this?
To prevent sensitive data from being exposed to the client, Next.js automatically encrypts the closed-over variables. A new private key is generated for each action every time a Next.js application is built. This means actions can only be invoked for a specific build.
80
How are Server Actions protected from CSRF attacks?
Behind the scenes, Server Actions use the POST method, and only this HTTP method is allowed to invoke them. This prevents most CSRF vulnerabilities in modern browsers, particularly with SameSite cookies being the default. As an additional protection, Server Actions in Next.js also compare the Origin header to the Host header (or X-Forwarded-Host). If these don't match, the request will be aborted. In other words, Server Actions can only be invoked on the same host as the page that hosts it. NOTE: For large applications that use reverse proxies or multi-layered backend architectures (where the server API differs from the production domain), it's recommended to use the configuration option `serverActions.allowedOrigins` option to specify a list of safe origins. The option accepts an array of strings.
81
What is the best practice for fetching data on the server w/ Next.js? Why - what are the benefits?
Whenever possible, we recommend fetching data on the server with Server Components.
82
What is the pattern for only fetching data when it's needed?
If you need to use the same data (e.g. current user) in multiple components in a tree, you do not have to fetch data globally, nor forward props between components. Instead, you can use `fetch` or React `cache` in the component that needs the data without worrying about the performance implications of making multiple requests for the same data. This is possible because `fetch` requests are automatically memoized. Learn more about request memoization. Good to know: This also applies to layouts, since it's not possible to pass data between a parent layout and its children.
83
What are the two data fetching patterns inside React components?
When fetching data inside React components, you need to be aware of two data fetching patterns: Parallel and Sequential. - With `sequential data fetching`, requests in a route are dependent on each other and therefore create waterfalls. There may be cases where you want this pattern because one fetch depends on the result of the other, or you want a condition to be satisfied before the next fetch to save resources. However, this behavior can also be unintentional and lead to longer loading times. - With `parallel data fetching`, requests in a route are eagerly initiated and will load data at the same time. This reduces client-server waterfalls and the total time it takes to load data.
84
What is "all or nothing" data fetching?
An alternative approach to prevent waterfalls is to fetch data globally, at the root of your application, but this will block rendering for all route segments beneath it until the data has finished loading. This can be described as "all or nothing" data fetching. Either you have the entire data for your page or application, or none.
85
What happens to fetch requests that use await? What should you be aware of?
Any fetch requests with await will block rendering and data fetching for the entire tree beneath it, unless they are wrapped in a boundary or loading.js is used. Another alternative is to use parallel data fetching or the preload pattern.
86
What is the Parallel Data Fetching pattern?
To fetch data in parallel, you can eagerly initiate requests by defining them outside the components that use the data, then calling them from inside the component. This saves time by initiating both requests in parallel, however, the user won't see the rendered result until both promises are resolved. To improve the user experience, you can add a Suspense Boundary to break up the rendering work and show part of the result as soon as possible.
87
How does the pre-loading data pattern work? What is it used for?
The pre-loading pattern is used to prevent waterfalls. You can optionally create a `preload` function to further optimize parallel data fetching. With this approach, you don't have to pass promises down as props. The preload function can also have any name as it's a pattern, not an API.
88
What are the benefits of using React `cache`, `server-only`, and the Preload Pattern?
You can combine the `cache` function, the preload pattern, and the `server-only` package to create a data fetching utility that can be used throughout your app. With this approach, you can eagerly fetch data, cache responses, and guarantee that this data fetching only happens on the server. The utils/get-item exports can be used by Layouts, Pages, or other components to give them control over when an item's data is fetched. Good to know: We recommend using the `server-only` package to make sure server data fetching functions are never used on the client.
89
How can you prevent sensitive data from being exposed to the client?
We recommend using React's taint APIs, `taintObjectReference` and `taintUniqueValue`, to prevent whole object instances or sensitive values from being passed to the client. To enable tainting in your application, set the Next.js Config experimental.taint option to true. Then pass the object or value you want to taint to the experimental_taintObjectReference or experimental_taintUniqueValue functions:
90
What are four ways of fetching data with Next.js and React?
1. On the server, with fetch 2. On the server, with third-party libraries 3. On the client, via a Route Handler 4. On the client, with third-party libraries.
91
How do you fetch data on the Server using `fetch`? what does this allow you to do?
Next.js extends the native fetch Web API to allow you to configure the caching and revalidating behavior for each fetch request on the server. React extends fetch to automatically memoize fetch requests while rendering a React component tree. You can use fetch with async/await in Server Components, in Route Handlers, and in Server Actions.
92
When are `fetch` requests NOT memoized?
In Route handlers, fetch requests are not memoized as Route Handlers are not part of the React component tree.
93
When are `fetch` requests NOT cached?
In Server Actions, fetch requests are not cached (defaults cache: no-store).
94
What is a side effect of using Next.js `cookies` and `header` functions inside of Server Components?
Next.js provides helpful functions you may need when fetching data in Server Components such as cookies and headers. These will cause the route to be dynamically rendered as they rely on request time information.
95
What is Next.js's default caching behavior?
By default, Next.js automatically caches the returned values of fetch in the Data Cache on the server. This means that the data can be fetched at build time or request time, cached, and reused on each data request. However, there are exceptions, fetch requests are not cached when: - Used inside a Server Action. - Used inside a Route Handler that uses the POST method.
96
What is the Data Cache?
The Data Cache is a persistent HTTP cache. Depending on your platform, the cache can scale automatically and be shared across multiple regions. Learn more about the Data Cache.
97
What is the process of Revalidating Data in relation to caching?
Revalidation is the process of purging the Data Cache and re-fetching the latest data. This is useful when your data changes and you want to ensure you show the latest information.
98
What are the two ways cached data can be revalidated?
1. Time-based revalidation 2. On-demand revalidation
99
What is Time-based revalidation? When is it useful?
Automatically revalidate data after a certain amount of time has passed. This is useful for data that changes infrequently and freshness is not as critical.
100
What is On-demand revalidation? When is it useful?
Manually revalidate data based on an event (e.g. form submission). On-demand revalidation can use a tag-based or path-based approach to revalidate groups of data at once. This is useful when you want to ensure the latest data is shown as soon as possible (e.g. when content from your headless CMS is updated).
101
How do you implement Time-based revalidation?
To revalidate data at a timed interval, you can use the `next.revalidate` option of `fetch` to set the cache lifetime of a resource (in seconds). Alternatively, to revalidate all fetch requests in a route segment, you can use the Segment Config Options. If you have multiple fetch requests in a statically rendered route, and each has a different revalidation frequency. The lowest time will be used for all requests. For dynamically rendered routes, each fetch request will be revalidated independently.
102
In the context of time-based data revalidation, how do multiple fetch requests get handled differently by static vs dynamically rendered routes?
In a statically rendered route, and each has a different revalidation frequency. The lowest time will be used for all requests. For dynamically rendered routes, each fetch request will be revalidated independently.
103
How do you implement On-demand Revalidation?
Data can be revalidated on-demand by path (`revalidatePath`) or by cache tag (`revalidateTag`) inside a Server Action or Route Handler. Next.js has a cache tagging system for invalidating fetch requests across routes. 1. When using `fetch`, you have the option to tag cache entries with one or more tags. 2. Then, you can call `revalidateTag` to revalidate all entries associated with that tag.
104
What happens when an error gets thrown when you are attempting to revalidate data?
If an error is thrown while attempting to revalidate data, the last successfully generated data will continue to be served from the cache. On the next subsequent request, Next.js will retry revalidating the data.
105
When are `fetch` requests *not* cached?
106
How can you opt out of caching for individual `fetch` requests?
To opt out of caching for individual fetch requests, you can set the cache option in fetch to 'no-store'. This will fetch data dynamically, on every request.
107
How can you opt out of caching for multiple `fetch` requests?
If you have multiple `fetch` requests in a route segment (e.g. a Layout or Page), you can configure the caching behavior of all data requests in the segment using the Segment Config Options. However, we recommend configuring the caching behavior of each `fetch` request individually. This gives you more granular control over the caching behavior.
108
What should you remember about Fetching data on the Server with third-party libraries?
In cases where you're using a third-party library that doesn't support or expose fetch (for example, a database, CMS, or ORM client), you can configure the caching and revalidating behavior of those requests using the Route Segment Config Option and React's `cache` function. Whether the data is cached or not will depend on whether the route segment is statically or dynamically rendered. If the segment is static (default), the output of the request will be cached and revalidated as part of the route segment. If the segment is dynamic, the output of the request will not be cached and will be re-fetched on every request when the segment is rendered.
109
What if you need to fetch data in a client component? What do you use? Why might you want to do this?
If you need to fetch data in a client component, you can call a Route Handler from the client. Route Handlers execute on the server and return the data to the client. This is useful when you don't want to expose sensitive information to the client, such as API tokens.
110
Do Server Components ever need the Route Handler to fetch data?
Since Server Components render on the server, you don't need to call a Route Handler from a Server Component to fetch data. Instead, you can fetch the data directly inside the Server Component.
111
Can you fetch data on the Client with third party libraries? When would you want to do this?
You can also fetch data on the client using a third-party library such as SWR or TanStack Query. These libraries provide their own APIs for memoizing requests, caching, revalidating, and mutating data.
112
Should you ever wrap `fetch` in `use` in Client Components? why or why not?
use is a React function that accepts and handles a promise returned by a function. Wrapping `fetch` in `use` is currently not recommended in Client Components and may trigger multiple re-renders. Learn more about use in the React docs.
113
What does the Tree refer to?
A convention for visualizing a hierarchical structure. For example, a component tree with parent and children components, a folder structure, etc.
114
What is a Subtree?
Part of a tree, starting at a new root (first) and ending at the leaves (last).
115
What is the Root?
Root: The first node in a tree or subtree, such as a root layout.
116
What is a Leaf?
Nodes in a subtree that have no children, such as the last segment in a URL path.
117
What is a URL Segment?
Part of the URL path delimited by slashes.
118
What is the URL Path?
Part of the URL that comes after the domain (composed of segments).
119
What is the app Router? Where does it live?
In version 13, Next.js introduced a new App Router built on React Server Components, which supports shared layouts, nested routing, loading states, error handling, and more. The App Router works in a new directory named app. By default, components inside app are React Server Components. This is a performance optimization and allows you to easily adopt them, and you can also use Client Components. NOTE: app router takes priority over old pages router, incremental switch over allowed
120
What are the roles of Folders and Files in the Next.js file-system based router?
- Folders are used to define routes. A route is a single path of nested folders, following the file-system hierarchy from the root folder down to a final leaf folder that includes a page.js file. See Defining Routes. - Files are used to create UI that is shown for a route segment. See special files.
121
What are Route Segments?
Each folder in a route represents a route segment. Each route segment is mapped to a corresponding segment in a URL path.
122
How do you create a nested route?
To create a nested route, you can nest folders inside each other. For example, you can add a new /dashboard/settings route by nesting two new folders in the app directory. The `/dashboard/settings` route is composed of three segments: - / (Root segment) - dashboard (Segment) - settings (Leaf segment)
123
What are Next.js file conventions?
Next.js provides a set of special files to create UI with specific behavior in nested routes:
124
Route special file: `layout`
Shared UI for a segment and its children
125
Route special file: `page`
Unique UI of a route and make routes publicly accessible
126
Route special file: `loading`
Loading UI for a segment and its children
127
Route special file: `not-found`
Not found UI for a segment and its children
128
Route special file: `error`
Error UI for a segment and its children
129
Route special file: `global-error`
Global Error UI
130
Route special file: `route`
Server-side API endpoint
131
Route special file: `template`
Specialized re-rendered Layout UI
132
Route special file: `default`
Fallback UI for Parallel Routes
133
What is the specific order/hierarchy in which React Components defined in special files of a route segment get rendered?
1. layout.js 2. template.js 3. error.js (React error boundary) 4. loading.js (React suspense boundary) 5. not-found.js (React error boundary) 6. page.js or nested layout.js
134
How are components nested in nested routes?
In a nested route, the components of a segment will be nested inside the components of its parent segment.
135
What is Colocation and why do you need it?
In addition to special files, you have the option to colocate your own files (e.g. components, styles, tests, etc) inside folders in the app directory. This is because while folders define routes, only the contents returned by page.js or route.js are publicly addressable.
136
What are examples of advanced routing patterns?
1. Parallel Routes 2. Intercepting Routes These patterns allow you to build richer and more complex UIs, democratizing features that were historically complex for small teams and individual developers to implement.
137
What do Parallel Routes pattern allow you to do?
Parallel Routes: Allow you to simultaneously show two or more pages in the same view that can be navigated independently. You can use them for split views that have their own sub-navigation. E.g. Dashboards.
138
What does the Intercepting Routes pattern allow you to do?
Intercepting Routes: Allow you to intercept a route and show it in the context of another route. You can use these when keeping the context for the current page is important. E.g. Seeing all tasks while editing one task or expanding a photo in a feed.
139
Which special files allow you to create UI for a route?
The special files layout.js, page.js, and template.js allow you to create UI for a route.
140
What is a page UI? What should you know about them?
A page is UI that is unique to a route. You can define a page by default exporting a component from a page.js file. - The .js, .jsx, or .tsx file extensions can be used for Pages. - A page is always the leaf of the route subtree. - A page.js file is required to make a route segment publicly accessible. - Pages are Server Components by default, but can be set to a Client Component. - Pages can fetch data. View the Data Fetching section for more information.
141
A page is always the leaf of the route subtree. True or False?
True A page is always the leaf of the route subtree.
142
Pages can't fetch data. True or False?
False. Pages can fetch data.
143
A page.js file is required to make a route segment publicly accessible. True or False?
True. A page.js file is required to make a route segment publicly accessible.
144
Pages are Client Components by default. True or False?
False. Pages are Server Components by default, but can be set to a Client Component
145
What is a Layout UI? Why use them? How do you define them and what props does it need to take?
A layout is UI that is shared between multiple routes. - On navigation, layouts preserve state, remain interactive, and do not re-render. - Layouts can also be nested. You can define a layout by default exporting a React component from a layout.js file. The component should accept a children prop that will be populated with a child layout (if it exists) or a page during rendering.
146
How do you define a layout UI?
You can define a layout by default exporting a React component from a layout.js file. The component should accept a children prop that will be populated with a child layout (if it exists) or a page during rendering.
147
Why would you want to use a layout UI?
On navigation, layouts preserve state, remain interactive, and do not re-render. - Layouts can also be nested.
148
What does it mean that layouts in the folder hierarchy are nested by default?
By default, layouts in the folder hierarchy are nested, which means they wrap child layouts via their children prop. You can nest layouts by adding layout.js inside specific route segments (folders). For example, to create a layout for the /dashboard route, add a new layout.js file inside the dashboard folder:
148
What is the role of the Root Layout? Do you need it?
The root layout is defined at the top level of the app directory and applies to all routes. This layout is required and must contain html and body tags, allowing you to modify the initial HTML returned from the server.
149
How do you nest layouts?
If you were to combine the two layouts above, the root layout (app/layout.js) would wrap the dashboard layout (app/dashboard/layout.js), which would wrap route segments inside app/dashboard/*. The two layouts would be nested as such:
150
True or False: Only the root layout can contain and tags.
True. Only the root layout can contain and tags.
151
True or False: When a layout.js and page.js file are defined in the same folder, the layout will wrap the page.
True. When a layout.js and page.js file are defined in the same folder, the layout will wrap the page.
152
True or False: Layouts are Client Components by default.
False. Layouts are Server Components by default but can be set to a Client Component.
153
True or False: Layouts can't fetch data.
False. Layouts can fetch data. View the Data Fetching section for more information.
154
Is passing data between a parent layout and it's children possible?
No. Passing data between a parent layout and its children is not possible. However, you can fetch the same data in a route more than once, and React will automatically dedupe the requests without affecting performance.
155
What happens if you fetch the same data in a route more than once?
React will automatically dedupe the requests without affecting performance.
156
True or False: Layouts have access to the route segments below itself.
False. Layouts do not have access to the route segments below itself. To access all route segments, you can use useSelectedLayoutSegment or useSelectedLayoutSegments in a Client Component.
157
How can Layouts access route layouts below itself?
To access all route segments, you can use `useSelectedLayoutSegment` or `useSelectedLayoutSegments` in a Client Component.
158
What are two ways to use Route Groups in relation to layouts?
- to opt specific route segments in and out of shared layouts. - to create multiple root layouts.
159
What are Templates? How are they different from Layouts?
Templates are similar to layouts in that they wrap each child layout or page. - Unlike layouts that persist across routes and maintain state, templates create a new instance for each of their children on navigation. This means that when a user navigates between routes that share a template - a new instance of the component is mounted - DOM elements are recreated - state is not preserved - effects are re-synchronized.
160
What happens when a user navigates between routes that share a template?
- a new instance of the component is mounted - DOM elements are recreated - state is not preserved - effects are re-synchronized.
161
What are cases where a template is more suitable than a layout?
- Features that rely on `useEffect` (e.g logging page views) and `useState` (e.g a per-page feedback form). - To change the default framework behavior. For example, Suspense Boundaries inside layouts only show the fallback the first time the Layout is loaded and not when switching pages. For templates, the fallback is shown on each navigation.
162
How do you define a template?
A template can be defined by exporting a default React component from a template.js file. The component should accept a children prop.
163
What happens when you nest a template?
In terms of nesting, template.js is rendered between a layout and its children. Here's a simplified output:
164
How do you define Metadata in routes?
In the app directory, you can modify the HTML elements such as title and meta using the Metadata APIs. Metadata can be defined by exporting a metadata object or generateMetadata function in a layout.js or page.js file. Good to know: You should not manually add tags such as and <meta> to root layouts. Instead, you should use the Metadata API which automatically handles advanced requirements such as streaming and de-duplicating <head> elements. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/824/692/a_image_thumb.?1711579748" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='165' id='card-496824771'> <div class='header'> 165 </div> <div class='card-face question'> <div class='question-content'> What are the 4 ways to navigate between routes in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> 1. Using the <Link> Component 2. Using the `useRouter` hook (Client Components) 3. Using the `redirect` function (Server Components) 4. Using the native History API </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='166' id='card-496824882'> <div class='header'> 166 </div> <div class='card-face question'> <div class='question-content'> What is the <Link> Component in Next.js? what is it used for? </div> </div> <div class='card-face answer'> <div class='answer-content'> <Link> is a built-in component that extends the HTML <a> tag to provide prefetching and client-side navigation between routes. It is the primary and recommended way to navigate between routes in Next.js. You can use it by importing it from next/link, and passing a href prop to the component: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/824/882/a_image_thumb.?1711579883" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='167' id='card-496825086'> <div class='header'> 167 </div> <div class='card-face question'> <div class='question-content'> How do you Link to Dynamic Segments? </div> </div> <div class='card-face answer'> <div class='answer-content'> When linking to dynamic segments, you can use template literals and interpolation to generate a list of links. For example, to generate a list of blog posts: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/825/086/a_image_thumb.?1711579908" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='168' id='card-496825145'> <div class='header'> 168 </div> <div class='card-face question'> <div class='question-content'> How do you check Active Links? </div> </div> <div class='card-face answer'> <div class='answer-content'> You can use `usePathname()` to determine if a link is active. For example, to add a class to the active link, you can check if the current pathname matches the href of the link: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/825/145/a_image_thumb.?1711579937" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='169' id='card-496825207'> <div class='header'> 169 </div> <div class='card-face question'> <div class='question-content'> How do you scroll to a specific `id` on navigation? </div> </div> <div class='card-face answer'> <div class='answer-content'> The default behavior of the Next.js App Router is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. If you'd like to scroll to a specific id on navigation, you can append your URL with a # hash link or just pass a hash link to the href prop. This is possible since <Link> renders to an <a> element. Next.js will scroll to the Page if it is not visible in the viewport upon navigation. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/825/207/a_image_thumb.?1711581683" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='170' id='card-496830925'> <div class='header'> 170 </div> <div class='card-face question'> <div class='question-content'> How do you disable scroll restoration in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> The default behavior of the Next.js App Router is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. If you'd like to disable this behavior, you can pass scroll={false} to the <Link> component, or scroll: false to router.push() or router.replace(). </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/830/925/a_image_thumb.?1711581729" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='171' id='card-496831034'> <div class='header'> 171 </div> <div class='card-face question'> <div class='question-content'> What does the `useRouter` hook allow you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> The useRouter hook allows you to programmatically change routes from Client Components. Recommendation: Use the <Link> component to navigate between routes unless you have a specific requirement for using useRouter. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='172' id='card-496831126'> <div class='header'> 172 </div> <div class='card-face question'> <div class='question-content'> What is the `redirect` function used for? </div> </div> <div class='card-face answer'> <div class='answer-content'> For Server Components, use the redirect function. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/831/126/a_image_thumb.?1711581855" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='173' id='card-496831229'> <div class='header'> 173 </div> <div class='card-face question'> <div class='question-content'> What status code does `redirect` return by default? </div> </div> <div class='card-face answer'> <div class='answer-content'> redirect returns a 307 (Temporary Redirect) status code by default. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='174' id='card-496831272'> <div class='header'> 174 </div> <div class='card-face question'> <div class='question-content'> What status code does `redirect` return when used in a Server Action? </div> </div> <div class='card-face answer'> <div class='answer-content'> When used in a Server Action, it returns a 303 (See Other), which is commonly used for redirecting to a success page as a result of a POST request. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='175' id='card-496831331'> <div class='header'> 175 </div> <div class='card-face question'> <div class='question-content'> Should `redirect` be called inside or outside of try/catch blocks? why? </div> </div> <div class='card-face answer'> <div class='answer-content'> redirect internally throws an error so it should be called outside of try/catch blocks. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='176' id='card-496831369'> <div class='header'> 176 </div> <div class='card-face question'> <div class='question-content'> Under what conditions can `redirect` be called in Client Components? </div> </div> <div class='card-face answer'> <div class='answer-content'> `redirect` can be called in Client Components during the rendering process but not in event handlers. You can use the `useRouter` hook instead. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='177' id='card-496831431'> <div class='header'> 177 </div> <div class='card-face question'> <div class='question-content'> Can you pass an absolute URL to `redirect`? Why would you do this? </div> </div> <div class='card-face answer'> <div class='answer-content'> `redirect` also accepts absolute URLs and can be used to redirect to external links. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='178' id='card-496831557'> <div class='header'> 178 </div> <div class='card-face question'> <div class='question-content'> Can you redirect before the render process? </div> </div> <div class='card-face answer'> <div class='answer-content'> If you'd like to redirect before the render process, use next.config.js or Middleware. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='179' id='card-496831595'> <div class='header'> 179 </div> <div class='card-face question'> <div class='question-content'> How does Next.js enable routing with the native History API? </div> </div> <div class='card-face answer'> <div class='answer-content'> Next.js allows you to use the native `window.history.pushState` and `window.history.replaceState` methods to update the browser's history stack without reloading the page. `pushState` and `replaceState` calls integrate into the Next.js Router, allowing you to sync with `usePathname` and `useSearchParams`. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='180' id='card-496831748'> <div class='header'> 180 </div> <div class='card-face question'> <div class='question-content'> How do you use `window.history.pushState` for routing in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> Use it to add a new entry to the browser's history stack. The user can navigate back to the previous state. For example, to sort a list of products: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/831/748/a_image_thumb.?1711582206" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='181' id='card-496831832'> <div class='header'> 181 </div> <div class='card-face question'> <div class='question-content'> How do you use `window.history.replaceState` for routing in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> Use it to replace the current entry on the browser's history stack. The user is not able to navigate back to the previous state. For example, to switch the application's locale: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/831/832/a_image_thumb.?1711582238" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='182' id='card-496831925'> <div class='header'> 182 </div> <div class='card-face question'> <div class='question-content'> How does Routing and Navigation work in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> The App Router uses a hybrid approach for routing and navigation. - On the server, your application code is automatically code-split by route segments. - And on the client, Next.js prefetches and caches the route segments. This means, when a user navigates to a new route, the browser doesn't reload the page, and only the route segments that change re-render - improving the navigation experience and performance. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='183' id='card-496832085'> <div class='header'> 183 </div> <div class='card-face question'> <div class='question-content'> Explain Code Splitting. What are the benefits? What kind of components use it? </div> </div> <div class='card-face answer'> <div class='answer-content'> Code splitting allows you to split your application code into smaller bundles to be downloaded and executed by the browser. This reduces the amount of data transferred and execution time for each request, leading to improved performance. Server Components allow your application code to be automatically code-split by route segments. This means only the code needed for the current route is loaded on navigation. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='184' id='card-496832190'> <div class='header'> 184 </div> <div class='card-face question'> <div class='question-content'> Explain Prefetching. </div> </div> <div class='card-face answer'> <div class='answer-content'> Prefetching is a way to preload a route in the background before the user visits it. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='185' id='card-496832240'> <div class='header'> 185 </div> <div class='card-face question'> <div class='question-content'> What are two ways routes are prefetched in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> - <Link> component: Routes are automatically prefetched as they become visible in the user's viewport. Prefetching happens when the page first loads or when it comes into view through scrolling. - router.prefetch(): The useRouter hook can be used to prefetch routes programmatically. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='186' id='card-496832306'> <div class='header'> 186 </div> <div class='card-face question'> <div class='question-content'> How does <Link>'s prefetching behavior differ for static and dynamic routes? </div> </div> <div class='card-face answer'> <div class='answer-content'> - Static Routes: `prefetch` defaults to `true`. The entire route is prefetched and cached. - Dynamic Routes: `prefetch` default to automatic. Only the shared layout, down the rendered "tree" of components until the first `loading.js` file, is prefetched and cached for 30s. This reduces the cost of fetching an entire dynamic route, and it means you can show an instant loading state for better visual feedback to users. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='187' id='card-496832419'> <div class='header'> 187 </div> <div class='card-face question'> <div class='question-content'> How do you disable prefetching? </div> </div> <div class='card-face answer'> <div class='answer-content'> You can disable prefetching by setting the prefetch prop to false. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='188' id='card-496832563'> <div class='header'> 188 </div> <div class='card-face question'> <div class='question-content'> True or False: Prefetching is enabled in development and production. </div> </div> <div class='card-face answer'> <div class='answer-content'> False. Prefetching is not enabled in development, only in production. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='189' id='card-496832617'> <div class='header'> 189 </div> <div class='card-face question'> <div class='question-content'> What is the Router Cache? </div> </div> <div class='card-face answer'> <div class='answer-content'> Next.js has an `in-memory client-side cache` called the Router Cache. As users navigate around the app, the React Server Component Payload of prefetched route segments and visited routes are stored in the cache. This means on navigation, the cache is reused as much as possible, instead of making a new request to the server - improving performance by reducing the number of requests and data transferred. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='190' id='card-496832719'> <div class='header'> 190 </div> <div class='card-face question'> <div class='question-content'> What does Partial Rendering mean? </div> </div> <div class='card-face answer'> <div class='answer-content'> Partial rendering means only the route segments that change on navigation re-render on the client, and any shared segments are preserved. For example, when navigating between two sibling routes, `/dashboard/settings` and `/dashboard/analytics`, the settings and analytics pages will be rendered, and the shared dashboard layout will be preserved. Without partial rendering, each navigation would cause the full page to re-render on the client. Rendering only the segment that changes reduces the amount of data transferred and execution time, leading to improved performance. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/832/719/a_image_thumb.?1711582676" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='191' id='card-496832847'> <div class='header'> 191 </div> <div class='card-face question'> <div class='question-content'> What is Soft Navigation? What does it enable? </div> </div> <div class='card-face answer'> <div class='answer-content'> Browsers perform a "hard navigation" when navigating between pages. The Next.js App Router enables "soft navigation" between pages, ensuring only the route segments that have changed are re-rendered (partial rendering). This enables client React state to be preserved during navigation. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='192' id='card-496832958'> <div class='header'> 192 </div> <div class='card-face question'> <div class='question-content'> What is Next.js's default behavior for Back and Forward Navigation? </div> </div> <div class='card-face answer'> <div class='answer-content'> By default, Next.js will maintain the scroll position for backwards and forwards navigation, and re-use route segments in the Router Cache. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='193' id='card-496833151'> <div class='header'> 193 </div> <div class='card-face question'> <div class='question-content'> What does the `loading.js` special file enable you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> The special file loading.js helps you create meaningful Loading UI with React Suspense. With this convention, you can show an instant loading state from the server while the content of a route segment loads. The new content is automatically swapped in once rendering is complete. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/833/151/a_image_thumb.?1711582832" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='194' id='card-496833250'> <div class='header'> 194 </div> <div class='card-face question'> <div class='question-content'> What is an Instant Loading State? </div> </div> <div class='card-face answer'> <div class='answer-content'> An instant loading state is fallback UI that is shown immediately upon navigation. You can pre-render loading indicators such as skeletons and spinners, or a small but meaningful part of future screens such as a cover photo, title, etc. This helps users understand the app is responding and provides a better user experience. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='195' id='card-496833361'> <div class='header'> 195 </div> <div class='card-face question'> <div class='question-content'> How do you create a loading state? </div> </div> <div class='card-face answer'> <div class='answer-content'> Create a loading state by adding a loading.js file inside a folder. In the same folder, loading.js will be nested inside layout.js. It will automatically wrap the page.js file and any children below in a <Suspense> boundary. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/833/361/a_image_thumb.?1711582983" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='196' id='card-496833611'> <div class='header'> 196 </div> <div class='card-face question'> <div class='question-content'> True or False: With server-centric routing, navigation is not immediate. </div> </div> <div class='card-face answer'> <div class='answer-content'> False. Navigation is immediate, even with server-centric routing. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='197' id='card-496833659'> <div class='header'> 197 </div> <div class='card-face question'> <div class='question-content'> What does it mean that Navigation is interruptible? </div> </div> <div class='card-face answer'> <div class='answer-content'> Navigation is interruptible, meaning changing routes does not need to wait for the content of the route to fully load before navigating to another route. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='198' id='card-496833710'> <div class='header'> 198 </div> <div class='card-face question'> <div class='question-content'> True or False: Shared layouts remain interactive while new route segments load. </div> </div> <div class='card-face answer'> <div class='answer-content'> True. Shared layouts remain interactive while new route segments load. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='199' id='card-496833780'> <div class='header'> 199 </div> <div class='card-face question'> <div class='question-content'> When should you use the `loading.js` convention? </div> </div> <div class='card-face answer'> <div class='answer-content'> Recommendation: Use the loading.js convention for route segments (layouts and pages) as Next.js optimizes this functionality. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='200' id='card-496833825'> <div class='header'> 200 </div> <div class='card-face question'> <div class='question-content'> What is an alternative to `loading.js`? </div> </div> <div class='card-face answer'> <div class='answer-content'> Streaming with Suspense In addition to loading.js, you can also manually create Suspense Boundaries for your own UI components. The App Router supports streaming with Suspense for both Node.js and Edge runtimes. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='201' id='card-496833879'> <div class='header'> 201 </div> <div class='card-face question'> <div class='question-content'> What are the benefits of using <Suspense>? </div> </div> <div class='card-face answer'> <div class='answer-content'> By using Suspense, you get the benefits of: - Streaming Server Rendering - Progressively rendering HTML from the server to the client. - Selective Hydration - React prioritizes what components to make interactive first based on user interaction. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='202' id='card-496834032'> <div class='header'> 202 </div> <div class='card-face question'> <div class='question-content'> What is Streaming Server Rendering? </div> </div> <div class='card-face answer'> <div class='answer-content'> Progressively rendering HTML from the server to the client. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='203' id='card-496834073'> <div class='header'> 203 </div> <div class='card-face question'> <div class='question-content'> What is Selective Hydration? </div> </div> <div class='card-face answer'> <div class='answer-content'> React prioritizes what components to make interactive first based on user interaction. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='204' id='card-496834142'> <div class='header'> 204 </div> <div class='card-face question'> <div class='question-content'> How does Streaming impact SEO in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> - Next.js will wait for data fetching inside `generateMetadata` to complete before streaming UI to the client. This guarantees the first part of a streamed response includes <head> tags. - Since streaming is server-rendered, it does not impact SEO. You can use the Rich Results Test tool from Google to see how your page appears to Google's web crawlers and view the serialized HTML (source). </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='205' id='card-496834227'> <div class='header'> 205 </div> <div class='card-face question'> <div class='question-content'> How does Streaming impact Status Codes in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> When streaming, a 200 status code will be returned to signal that the request was successful. The server can still communicate errors or issues to the client within the streamed content itself, for example, when using `redirect` or `notFound`. Since the response headers have already been sent to the client, the status code of the response cannot be updated. This does not affect SEO. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='206' id='card-496834316'> <div class='header'> 206 </div> <div class='card-face question'> <div class='question-content'> What does the `error.js` file convention allow you to do? What are 4 features? </div> </div> <div class='card-face answer'> <div class='answer-content'> The error.js file convention allows you to gracefully handle unexpected runtime errors in nested routes. 1. Automatically wrap a route segment and its nested children in a React Error Boundary. 2. Create error UI tailored to specific segments using the file-system hierarchy to adjust granularity. 3. Isolate errors to affected segments while keeping the rest of the application functional. 4. Add functionality to attempt to recover from an error without a full page reload. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='207' id='card-496834476'> <div class='header'> 207 </div> <div class='card-face question'> <div class='question-content'> How do you create an error UI? </div> </div> <div class='card-face answer'> <div class='answer-content'> Create error UI by adding an error.js file inside a route segment and exporting a React component: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/834/476/a_image_thumb.?1711583450" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='208' id='card-496834572'> <div class='header'> 208 </div> <div class='card-face question'> <div class='question-content'> What does error.js work? </div> </div> <div class='card-face answer'> <div class='answer-content'> - `error.js` automatically creates a React Error Boundary that wraps a nested child segment or `page.js` component. - The React component exported from the `error.js` file is used as the *fallback* component. - If an error is thrown within the error boundary, the error is *contained*, and the fallback component is *rendered*. -When the fallback error component is active, layouts *above* the error boundary *maintain* their state and *remain* interactive, and the error component can display functionality to recover from the error. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/834/572/a_image_thumb.?1711583596" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='209' id='card-496834852'> <div class='header'> 209 </div> <div class='card-face question'> <div class='question-content'> How does an error component recover from errors? </div> </div> <div class='card-face answer'> <div class='answer-content'> The cause of an error can sometimes be temporary. In these cases, simply trying again might resolve the issue. An error component can use the `reset()` function to prompt the user to attempt to recover from the error. When executed, the function will try to re-render the Error boundary's contents. If successful, the fallback error component is replaced with the result of the re-render. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/834/852/a_image_thumb.?1711583658" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='210' id='card-496834991'> <div class='header'> 210 </div> <div class='card-face question'> <div class='question-content'> How does the nested component hierarchy impact the behavior of `error.js` files across a nested route? </div> </div> <div class='card-face answer'> <div class='answer-content'> - Errors bubble up to the nearest parent error boundary. This means an `error.js` file will handle errors for all its nested child segments. More or less granular error UI can be achieved by placing `error.js` files at different levels in the nested folders of a route. - An `error.js` boundary will not handle errors thrown in a `layout.js` component in the same segment because the error boundary is nested inside that layout's component. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/834/991/a_image_thumb.?1711583779" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='211' id='card-496835252'> <div class='header'> 211 </div> <div class='card-face question'> <div class='question-content'> How do you handle errors in Layouts? </div> </div> <div class='card-face answer'> <div class='answer-content'> `error.js` boundaries do *not* catch errors thrown in `layout.js` or `template.js` components of the same segment. This intentional hierarchy keeps important UI that is shared between sibling routes (such as navigation) visible and functional when an error occurs. To handle errors within a specific layout or template, place an `error.js` file in the layout's parent segment. To handle errors within the root layout or template, use a variation of `error.js` called `global-error.js.` </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='212' id='card-496835379'> <div class='header'> 212 </div> <div class='card-face question'> <div class='question-content'> True or False: error.js can be used to catch errors in Layouts and Templates. </div> </div> <div class='card-face answer'> <div class='answer-content'> False. `error.js` boundaries do not catch errors thrown in `layout.js` or `template.js` components of the same segment. This intentional hierarchy keeps important UI that is shared between sibling routes (such as navigation) visible and functional when an error occurs. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='213' id='card-496835441'> <div class='header'> 213 </div> <div class='card-face question'> <div class='question-content'> How do you handle errors in a specific template or layout? </div> </div> <div class='card-face answer'> <div class='answer-content'> To handle errors within a specific layout or template, place an `error.js` file in the layout's parent segment. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='214' id='card-496835463'> <div class='header'> 214 </div> <div class='card-face question'> <div class='question-content'> How do you handle errors within the root layout or template? </div> </div> <div class='card-face answer'> <div class='answer-content'> To handle errors within the root layout or template, use a variation of `error.js` called `global-error.js`. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='215' id='card-496835543'> <div class='header'> 215 </div> <div class='card-face question'> <div class='question-content'> How does `global-error.js` differ from the root `error.js`? </div> </div> <div class='card-face answer'> <div class='answer-content'> The root `app/error.js` boundary does not catch errors thrown in the root `app/layout.js` or `app/template.js` component. To specifically handle errors in these root components, use a variation of error.js called `app/global-error.js` located in the root app directory. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/835/543/a_image_thumb.?1711584319" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='216' id='card-496835787'> <div class='header'> 216 </div> <div class='card-face question'> <div class='question-content'> True or False: if you have a `global-error.js` defined, you don't need to define a root `error.js`. </div> </div> <div class='card-face answer'> <div class='answer-content'> False. Even if a `global-error.js ` is defined, it is still recommended to define a root error.js whose fallback component will be rendered within the root layout, which includes globally shared UI and branding. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='217' id='card-496835873'> <div class='header'> 217 </div> <div class='card-face question'> <div class='question-content'> True or False: `global-error.js` is likely to be triggered at least as much as root `error.js` </div> </div> <div class='card-face answer'> <div class='answer-content'> False. `global-error.js` is unlikely to be triggered often as root components are typically less dynamic, and other `error.js` boundaries will catch most errors. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='218' id='card-496835941'> <div class='header'> 218 </div> <div class='card-face question'> <div class='question-content'> True or False: `global-error.js` is considered the catch-all error for an entire application. </div> </div> <div class='card-face answer'> <div class='answer-content'> True. `global-error.js` is the least granular error UI and can be considered "catch-all" error handling for the whole application. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='219' id='card-496836029'> <div class='header'> 219 </div> <div class='card-face question'> <div class='question-content'> Why is it important for `global-error.js` to define it's own <html> and <body> tags? </div> </div> <div class='card-face answer'> <div class='answer-content'> `global-error.js` must define its own <html> and <body> tags b/c unlike the root `error.js`, the` global-error.js` error boundary wraps the entire application, *and* its fallback component replaces the root layout when active. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='220' id='card-496836206'> <div class='header'> 220 </div> <div class='card-face question'> <div class='question-content'> True or False: `global-error.js` is only enabled in production. </div> </div> <div class='card-face answer'> <div class='answer-content'> True. `global-error.js` is only enabled in production. In development, our error overlay will show instead. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='221' id='card-496836249'> <div class='header'> 221 </div> <div class='card-face question'> <div class='question-content'> How does Next.js handle Server Errors? </div> </div> <div class='card-face answer'> <div class='answer-content'> If an error is thrown inside a Server Component, Next.js will forward an `Error` object (stripped of sensitive error information in production) to the nearest `error.js` file as the `error` prop. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='222' id='card-496836308'> <div class='header'> 222 </div> <div class='card-face question'> <div class='question-content'> What does Next.js do to secure sensitive server error information? </div> </div> <div class='card-face answer'> <div class='answer-content'> During production, the` Error` object forwarded to the client only includes a generic `message` and `digest` property. This is a security precaution to avoid leaking potentially sensitive details included in the error to the client. The message property contains a generic message about the error and the digest property contains an automatically generated hash of the error that can be used to match the corresponding error in server-side logs. During development, the` Error` object forwarded to the client will be serialized and include the message of the original error for easier debugging. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='223' id='card-496836574'> <div class='header'> 223 </div> <div class='card-face question'> <div class='question-content'> What are the ways Next.js handles redirects? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/836/574/a_image_thumb.?1711584573" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='224' id='card-496836648'> <div class='header'> 224 </div> <div class='card-face question'> <div class='question-content'> What does the `redirect` function allow you to do? where can you call it? </div> </div> <div class='card-face answer'> <div class='answer-content'> The `redirect` function allows you to redirect the user to another URL. You can call redirect in Server Components, Route Handlers, and Server Actions. `redirect` is often used after a mutation or event. For example, creating a post: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/836/648/a_image_thumb.?1711584648" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='225' id='card-496836774'> <div class='header'> 225 </div> <div class='card-face question'> <div class='question-content'> What does the `permanentRedirect` function allow you to do? Where can you call it? When do you use it? </div> </div> <div class='card-face answer'> <div class='answer-content'> The `permanentRedirect` function allows you to permanently redirect the user to another URL. You can call `permanentRedirect` in Server Components, Route Handlers, and Server Actions. `permanentRedirect` is often used after a mutation or event that changes an entity's canonical URL, such as updating a user's profile URL after they change their username: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/836/774/a_image_thumb.?1711584715" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='226' id='card-496836895'> <div class='header'> 226 </div> <div class='card-face question'> <div class='question-content'> What status code does `permanentRedirect` return by default? </div> </div> <div class='card-face answer'> <div class='answer-content'> `permanentRedirect` returns a 308 (permanent redirect) status code by default. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='227' id='card-496836941'> <div class='header'> 227 </div> <div class='card-face question'> <div class='question-content'> Can `permanentRedirect` be used to redirect to external links? </div> </div> <div class='card-face answer'> <div class='answer-content'> Yes. `permanentRedirect` also accepts absolute URLs and can be used to redirect to external links. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='228' id='card-496837009'> <div class='header'> 228 </div> <div class='card-face question'> <div class='question-content'> If you need to redirect inside an event handler in a Client Component, what should you use? </div> </div> <div class='card-face answer'> <div class='answer-content'> If you need to redirect inside an event handler in a Client Component, you can use the `push` method from the `useRouter hook`. For example: NOTE: If you don't need to programatically navigate a user, you should use a <Link> component. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/837/009/a_image_thumb.?1711584858" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='229' id='card-496837165'> <div class='header'> 229 </div> <div class='card-face question'> <div class='question-content'> What does the `redirects` option in the `next.config.js` file allow you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> The redirects option in the next.config.js file allows you to redirect an incoming request path to a different destination path. This is useful when you change the URL structure of pages or have a list of redirects that are known ahead of time. redirects supports path, header, cookie, and query matching, giving you the flexibility to redirect users based on an incoming request. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/837/165/a_image_thumb.?1711584914" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='230' id='card-496837262'> <div class='header'> 230 </div> <div class='card-face question'> <div class='question-content'> What status code does `redirects` return without the permanent option? </div> </div> <div class='card-face answer'> <div class='answer-content'> returns a 307 (Temporary Redirect) </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='231' id='card-496837374'> <div class='header'> 231 </div> <div class='card-face question'> <div class='question-content'> What status code does `redirects` return with the permanent option? </div> </div> <div class='card-face answer'> <div class='answer-content'> 308 (Permanent Redirect) status code with the permanent option. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='232' id='card-496837418'> <div class='header'> 232 </div> <div class='card-face question'> <div class='question-content'> True or False: `redirects` runs *before* Middleware. </div> </div> <div class='card-face answer'> <div class='answer-content'> True. `redirects` runs *before* Middleware. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='233' id='card-496837489'> <div class='header'> 233 </div> <div class='card-face question'> <div class='question-content'> What is Vercel's limit for `redirects`? </div> </div> <div class='card-face answer'> <div class='answer-content'> 1,024 redirects. `redirects` may have a limit on platforms. For example, on Vercel, there's a limit of 1,024 redirects. To manage a large number of redirects (1000+), consider creating a custom solution using Middleware. See managing redirects at scale for more. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='234' id='card-496837593'> <div class='header'> 234 </div> <div class='card-face question'> <div class='question-content'> How does `redirect` behave in the streaming context? </div> </div> <div class='card-face answer'> <div class='answer-content'> When used in a streaming context, this will insert a meta tag to emit the redirect on the client side. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='235' id='card-496837715'> <div class='header'> 235 </div> <div class='card-face question'> <div class='question-content'> How does `redirect` behave when used in a Server Action? </div> </div> <div class='card-face answer'> <div class='answer-content'> it will serve a 303 HTTP redirect response to the caller. Otherwise, it will serve a 307 HTTP redirect response to the caller. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='236' id='card-496837780'> <div class='header'> 236 </div> <div class='card-face question'> <div class='question-content'> In Server Actions and Route Handlers should `redirect` be called before or after `try/catch` blocks? </div> </div> <div class='card-face answer'> <div class='answer-content'> After. In Server Actions and Route Handlers, redirect should be called after the try/catch block. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='237' id='card-496837852'> <div class='header'> 237 </div> <div class='card-face question'> <div class='question-content'> What parameters does the `redirect` function take? </div> </div> <div class='card-face answer'> <div class='answer-content'> redirect(path, type) By default, `redirect` will use `push` (adding a new entry to the browser history stack) in Server Actions and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the type parameter. The type parameter has no effect when used in Server Components. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/837/852/a_image_thumb.?1711585366" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='238' id='card-496837980'> <div class='header'> 238 </div> <div class='card-face question'> <div class='question-content'> Does `redirect` return a value? </div> </div> <div class='card-face answer'> <div class='answer-content'> No. `redirect` does not return any value. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='239' id='card-496838104'> <div class='header'> 239 </div> <div class='card-face question'> <div class='question-content'> What happens when you invoke the `redirect()` function in a Server Component? </div> </div> <div class='card-face answer'> <div class='answer-content'> Invoking the `redirect()` function throws a NEXT_REDIRECT error and terminates rendering of the route segment in which it was thrown. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/838/104/a_image_thumb.?1711585417" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='240' id='card-496838192'> <div class='header'> 240 </div> <div class='card-face question'> <div class='question-content'> How do you use `redirect` in a Client Component? </div> </div> <div class='card-face answer'> <div class='answer-content'> `redirect` can be used in a Client Component through a Server Action. If you need to use an event handler to redirect the user, you can use the `useRouter` hook. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/838/192/a_image_thumb.?1711585476" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='241' id='card-496838317'> <div class='header'> 241 </div> <div class='card-face question'> <div class='question-content'> Why does `redirect` use 307 and 308? </div> </div> <div class='card-face answer'> <div class='answer-content'> The introduction of the 307 status code means that the request method is preserved as POST. 302 - Temporary redirect, will change the request method from POST to GET 307 - Temporary redirect, will preserve the request method as POST The redirect() method uses a 307 by default, instead of a 302 temporary redirect, meaning your requests will always be preserved as POST requests. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='242' id='card-496838563'> <div class='header'> 242 </div> <div class='card-face question'> <div class='question-content'> What does Middleware allow you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly. Middleware runs before cached content and routes are matched. See Matching Paths for more details. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='243' id='card-496838585'> <div class='header'> 243 </div> <div class='card-face question'> <div class='question-content'> What can integrating middleware into your application significantly improve? </div> </div> <div class='card-face answer'> <div class='answer-content'> performance, security, and user experience </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='244' id='card-496838639'> <div class='header'> 244 </div> <div class='card-face question'> <div class='question-content'> What are common scenarios where Middleware is particularly effective? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/838/639/a_image_thumb.?1711585716" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='245' id='card-496838722'> <div class='header'> 245 </div> <div class='card-face question'> <div class='question-content'> What are scenarios where middleware may not be the optimal approach? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/838/722/a_image_thumb.?1711585762" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='246' id='card-496838808'> <div class='header'> 246 </div> <div class='card-face question'> <div class='question-content'> What is the convention for defining Middleware in Next.js applications? </div> </div> <div class='card-face answer'> <div class='answer-content'> Use the file `middleware.ts` (or .js) in the root of your project to define Middleware. For example, at the same level as `pages` or `app`, or inside `src` if applicable. Note: While only one `middleware.ts` file is supported per project, you can still organize your middleware logic modularly. Break out middleware functionalities into separate .ts or .js files and import them into your main middleware.ts file. This allows for cleaner management of route-specific middleware, aggregated in the middleware.ts for centralized control. By enforcing a single middleware file, it simplifies configuration, prevents potential conflicts, and optimizes performance by avoiding multiple middleware layers. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/838/808/a_image_thumb.?1711585899" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='247' id='card-496839092'> <div class='header'> 247 </div> <div class='card-face question'> <div class='question-content'> Why is it important to use Matchers when using middleware in your app? </div> </div> <div class='card-face answer'> <div class='answer-content'> Middleware will be invoked for every route in your project. Given this, it's crucial to use matchers to precisely target or exclude specific routes. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='248' id='card-496839165'> <div class='header'> 248 </div> <div class='card-face question'> <div class='question-content'> What is the execution order for Matching Paths? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/839/165/a_image_thumb.?1711585973" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='249' id='card-496839250'> <div class='header'> 249 </div> <div class='card-face question'> <div class='question-content'> What are the two ways to define which paths Middleware will run on? </div> </div> <div class='card-face answer'> <div class='answer-content'> 1. Custom matcher config 2. Conditional Statements </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='250' id='card-496839294'> <div class='header'> 250 </div> <div class='card-face question'> <div class='question-content'> What is Matcher? </div> </div> <div class='card-face answer'> <div class='answer-content'> `matcher` allows you to filter Middleware to run on specific paths. - You can match a single path or multiple paths with an array syntax. - The matcher config allows full regex so matching like negative lookaheads or character matching is supported. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/839/294/a_image_thumb.?1711586098" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='251' id='card-496839499'> <div class='header'> 251 </div> <div class='card-face question'> <div class='question-content'> How can you use Matcher to bypass Middleware for certain requests? </div> </div> <div class='card-face answer'> <div class='answer-content'> You can also bypass Middleware for certain requests by using the missing or has arrays, or a combination of both: </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/839/499/a_image_thumb.?1711586150" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='252' id='card-496839597'> <div class='header'> 252 </div> <div class='card-face question'> <div class='question-content'> Why do the `matcher` values need to be constants? </div> </div> <div class='card-face answer'> <div class='answer-content'> The `matcher` values need to be constants so they can be statically analyzed at build-time. Dynamic values such as variables will be ignored. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='253' id='card-496839677'> <div class='header'> 253 </div> <div class='card-face question'> <div class='question-content'> What are 4 rules for configured matchers? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/839/677/a_image_thumb.?1711586219" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='254' id='card-496839794'> <div class='header'> 254 </div> <div class='card-face question'> <div class='question-content'> How are conditional statements used for managing which routes can be accessed by middleware? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/839/794/a_image_thumb.?1711586295" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='255' id='card-496840055'> <div class='header'> 255 </div> <div class='card-face question'> <div class='question-content'> What does the `NextResponse` API allow you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/055/a_image_thumb.?1711586318" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='256' id='card-496840096'> <div class='header'> 256 </div> <div class='card-face question'> <div class='question-content'> How do you use the NextResponse` API to produce a response from Middleware? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/096/a_image_thumb.?1711586352" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='257' id='card-496840160'> <div class='header'> 257 </div> <div class='card-face question'> <div class='question-content'> How do you manage middleware using Cookies? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/160/a_image_thumb.?1711586432" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='258' id='card-496840288'> <div class='header'> 258 </div> <div class='card-face question'> <div class='question-content'> How do you manage middleware using the `NextResponse` API to set headers? </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/288/a_image_thumb.?1711586488" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='259' id='card-496840400'> <div class='header'> 259 </div> <div class='card-face question'> <div class='question-content'> How do you set CORS headers in Middleware? Why? </div> </div> <div class='card-face answer'> <div class='answer-content'> You can set CORS headers in Middleware to allow cross-origin requests, including simple and preflighted requests. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/400/a_image_thumb.?1711586526" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='260' id='card-496840483'> <div class='header'> 260 </div> <div class='card-face question'> <div class='question-content'> How do you produce a response from Middleware? </div> </div> <div class='card-face answer'> <div class='answer-content'> You can respond from Middleware directly by returning a `Response` or `NextResponse` instance. (This is available since Next.js v13.1.0) </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/483/a_image_thumb.?1711586571" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='261' id='card-496840570'> <div class='header'> 261 </div> <div class='card-face question'> <div class='question-content'> Why use `waitUntil` and `NextFetchEvent` in Middleware? </div> </div> <div class='card-face answer'> <div class='answer-content'> The `NextFetchEvent` object extends the native `FetchEvent` object, and includes the `waitUntil()` method. The `waitUntil()` method takes a promise as an argument, and extends the lifetime of the Middleware until the promise settles. This is useful for performing work in the background. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/840/570/a_image_thumb.?1711586672" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='262' id='card-496840796'> <div class='header'> 262 </div> <div class='card-face question'> <div class='question-content'> True or False: Middleware supports both Edge and Node.js runtimes. </div> </div> <div class='card-face answer'> <div class='answer-content'> False. Middleware currently only supports the Edge runtime. The Node.js runtime can not be used. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='263' id='card-496841221'> <div class='header'> 263 </div> <div class='card-face question'> <div class='question-content'> What are the differences between the Data Cache and Request Memoization in Next.js? </div> </div> <div class='card-face answer'> <div class='answer-content'> While both caching mechanisms help improve performance by re-using cached data, the Data Cache is persistent across incoming requests and deployments, whereas memoization only lasts the lifetime of a request. With memoization, we reduce the number of duplicate requests in the same render pass that have to cross the network boundary from the rendering server to the Data Cache server (e.g. a CDN or Edge Network) or data source (e.g. a database or CMS). With the Data Cache, we reduce the number of requests made to our origin data source. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='264' id='card-496841222'> <div class='header'> 264 </div> <div class='card-face question'> <div class='question-content'> True or False. Next.js automatically renders and caches routes at build time. </div> </div> <div class='card-face answer'> <div class='answer-content'> True. Next.js automatically renders and caches routes at build time. This is an optimization that allows you to serve the cached route instead of rendering on the server for every request, resulting in faster page loads. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='265' id='card-496841611'> <div class='header'> 265 </div> <div class='card-face question'> <div class='question-content'> How does React Rendering work on the Server? </div> </div> <div class='card-face answer'> <div class='answer-content'> On the server, Next.js uses React's APIs to orchestrate rendering. The rendering work is split into chunks: by individual routes segments and Suspense boundaries. Each chunk is rendered in two steps: 1. React renders Server Components into a special data format, optimized for streaming, called the `React Server Component Payload`. 2. Next.js uses the React Server Component Payload and Client Component JavaScript instructions to render HTML on the server. This means we don't have to wait for everything to render before caching the work or sending a response. Instead, we can stream a response as work is completed. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='266' id='card-496841709'> <div class='header'> 266 </div> <div class='card-face question'> <div class='question-content'> What is the React Server Component Payload? </div> </div> <div class='card-face answer'> <div class='answer-content'> The React Server Component 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. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='266' id='card-496841710'> <div class='header'> 266 </div> <div class='card-face question'> <div class='question-content'> What does the React Server Component Payload contain? </div> </div> <div class='card-face answer'> <div class='answer-content'> - 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 </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='267' id='card-496841894'> <div class='header'> 267 </div> <div class='card-face question'> <div class='question-content'> What is the Next.js Full Route Cache? </div> </div> <div class='card-face answer'> <div class='answer-content'> The default behavior of Next.js is to cache the rendered result (React Server Component Payload and HTML) of a route on the server. This applies to statically rendered routes at build time, or during revalidation. </div> <img class="card-image" src="https://s3.amazonaws.com/brainscape-prod/system/cm/496/841/894/a_image_thumb.?1711587387" /> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='268' id='card-496842006'> <div class='header'> 268 </div> <div class='card-face question'> <div class='question-content'> Explain React Hydration and Reconciliation on the Client? </div> </div> <div class='card-face answer'> <div class='answer-content'> At request time, on the client: 1. The HTML is used to immediately show a fast non-interactive initial preview of the Client and Server Components. 2. The React Server Components Payload is used to reconcile the Client and rendered Server Component trees, and update the DOM. 3. The JavaScript instructions are used to hydrate Client Components and make the application interactive. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='269' id='card-496842072'> <div class='header'> 269 </div> <div class='card-face question'> <div class='question-content'> What is the Next.js Router Cache? </div> </div> <div class='card-face answer'> <div class='answer-content'> The Router Cache is how Next.js caches on the Client side. The React Server Component Payload is stored in the client-side Router Cache - a separate in-memory cache, split by individual route segment. This Router Cache is used to improve the navigation experience by storing previously visited routes and prefetching future routes. On subsequent navigations or during prefetching, Next.js will check if the React Server Components Payload is stored in the Router Cache. If so, it will skip sending a new request to the server. If the route segments are not in the cache, Next.js will fetch the React Server Components Payload from the server, and populate the Router Cache on the client. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='270' id='card-500102768'> <div class='header'> 270 </div> <div class='card-face question'> <div class='question-content'> # [](http://) How do you create API endpoints in NextJs? </div> </div> <div class='card-face answer'> <div class='answer-content'> Route Handlers ## Footnote https://nextjs.org/docs/app/building-your-application/routing/route-handlers </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='271' id='card-500103359'> <div class='header'> 271 </div> <div class='card-face question'> <div class='question-content'> What are 2 cases where you have to write database queries in NextJs? </div> </div> <div class='card-face answer'> <div class='answer-content'> - When creating your API endpoints, you need to write logic to interact with your database. - If you are using **React Server Components** (fetching data on the server), you can** skip the API layer**, and **query your database directly** without risking exposing your database secrets to the client. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='272' id='card-500110053'> <div class='header'> 272 </div> <div class='card-face question'> <div class='question-content'> What are 3 benefits of **Static Rendering**? </div> </div> <div class='card-face answer'> <div class='answer-content'> - **Faster Websites** - Prerendered content can be cached and globally distributed. This ensures that users around the world can access your website's content more quickly and reliably. - **Reduced Server Load** - Because the content is cached, your server does not have to dynamically generate content for each user request. - **SEO** - Prerendered content is easier for search engine crawlers to index, as the content is already available when the page loads. This can lead to improved search engine rankings. Static rendering is useful for UI with **no data or data that is shared across users**, such as a static blog post or a product page. It might not be a good fit for a dashboard that has personalized data that is regularly updated. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='273' id='card-500110382'> <div class='header'> 273 </div> <div class='card-face question'> <div class='question-content'> When does **dynamic rendering** happen? </div> </div> <div class='card-face answer'> <div class='answer-content'> With dynamic rendering, content is rendered on the server for each user **at request time **(when the user visits the page). </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='274' id='card-500110458'> <div class='header'> 274 </div> <div class='card-face question'> <div class='question-content'> What are 3 benefits of **dynamic rendering**? </div> </div> <div class='card-face answer'> <div class='answer-content'> - **Real-Time Data** - Dynamic rendering allows your application to display real-time or frequently updated data. This is ideal for applications where data changes often. - **User-Specific Content** - It's easier to serve personalized content, such as dashboards or user profiles, and update the data based on user interaction. - **Request Time Information** - Dynamic rendering allows you to access information that can only be known at request time, such as cookies or the URL search parameters. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='275' id='card-500110700'> <div class='header'> 275 </div> <div class='card-face question'> <div class='question-content'> By default, `@vercel/postgres` doesn't set its own caching semantics. What does this allow a framework to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> This allows the framework to set its own static and dynamic behavior. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='276' id='card-500110888'> <div class='header'> 276 </div> <div class='card-face question'> <div class='question-content'> Which `Next.js API` can you use inside your `Server Components` or `data fetching functions` to **opt out of static rendering**? </div> </div> <div class='card-face answer'> <div class='answer-content'> ` unstable_noStore` from `next/cache` ex. ``` import { unstable_noStore as noStore } from 'next/cache'; export async function fetchFilteredCustomers(query: string) { noStore(); // ... } ``` The STABLE version is `export const dynamic = "force-dynamic"`. (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config) </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='277' id='card-500113461'> <div class='header'> 277 </div> <div class='card-face question'> <div class='question-content'> What is **interruptable navigation**? </div> </div> <div class='card-face answer'> <div class='answer-content'> The user doesn't have to wait for the page to finish loading before navigating away </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='278' id='card-500113725'> <div class='header'> 278 </div> <div class='card-face question'> <div class='question-content'> what are **loading skeletons** in relation to `loading.tsx` files? </div> </div> <div class='card-face answer'> <div class='answer-content'> A **loading skeleton** is a simplified version of the UI. Many websites use them as a placeholder (or fallback) to indicate to users that the content is loading. Any UI you embed into `loading.tsx` will be **embedded as part of the static file, and sent first**. Then, the rest of the dynamic content will be streamed from the server to the client. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='279' id='card-500114269'> <div class='header'> 279 </div> <div class='card-face question'> <div class='question-content'> How do you create Route Groups? </div> </div> <div class='card-face answer'> <div class='answer-content'> - Create a new folder called `/(overview)` inside the `dashboard` folder. Then, move your `loading.tsx` and `page.tsx` files inside the folder - **Route groups** allow you to organize files into logical groups **without affecting the URL path structure**. When you create a new folder using parentheses `()`, t**he name won't be included in the URL path**. So `/dashboard/(overview)/page.tsx` becomes `/dashboard.` In this example, you're using a route group to ensure `loading.tsx` only applies to your dashboard overview page. However, you can also use route groups to separate your application into sections (e.g. `(marketing)` routes and `(shop)` routes) or by teams for larger applications. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='280' id='card-500114775'> <div class='header'> 280 </div> <div class='card-face question'> <div class='question-content'> What does **Suspense** allow you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> **Suspense** allows you to **defer rendering parts of your application until some condition is met** (e.g. data is loaded). You can wrap your `dynamic components` in `Suspense`. Then, _pass it a **fallback component**_ to show while the dynamic component _loads_. It's worth noting that wrapping a component in **Suspense** doesn't make the component itself dynamic (remember you used `unstable_noStore` to achieve this behavior), but rather **Suspense** is used as a _boundary_ between the static and dynamic parts of your route. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='281' id='card-500116727'> <div class='header'> 281 </div> <div class='card-face question'> <div class='question-content'> When can you use the `CardWrapper` pattern? ## Footnote https://nextjs.org/learn/dashboard-app/streaming </div> </div> <div class='card-face answer'> <div class='answer-content'> You can use this pattern when you want multiple components to load in at the same time. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='282' id='card-500116836'> <div class='header'> 282 </div> <div class='card-face question'> <div class='question-content'> What should you consider when deciding where to place your **Suspense** boundaries? </div> </div> <div class='card-face answer'> <div class='answer-content'> Where you place your **Suspense boundaries** will depend on a few things: 1. How you want the user to experience the page as it streams. 2. What content you want to prioritize. 3. If the components rely on data fetching. example considerations/consequenses: - You could stream the **whole page** like we did with `loading.tsx`... but that may lead to a longer loading time if one of the components has a slow data fetch. - You could stream **every component individually**... but that may lead to UI popping into the screen as it becomes ready. - You could also **create a staggered effect by streaming page sections**. But you'll need to create **wrapper** components. In general, it's good practice to **move your data fetches down to the components that need it**, and **then wrap those components in Suspense**. But there is nothing wrong with streaming the sections or the whole page if that's what your application needs. ## Footnote https://nextjs.org/learn/dashboard-app/streaming </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='283' id='card-500119814'> <div class='header'> 283 </div> <div class='card-face question'> <div class='question-content'> `useSearchParams` </div> </div> <div class='card-face answer'> <div class='answer-content'> Allows you to access the parameters of the current URL. For example, the search params for this URL `/dashboard/invoices?page=1&query=pending` would look like this: `{page: '1', query: 'pending'}`. NOTE: **Page** components accept a `prop` called `searchParams`, (page.tsx) </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='284' id='card-500119918'> <div class='header'> 284 </div> <div class='card-face question'> <div class='question-content'> `usePathname` </div> </div> <div class='card-face answer'> <div class='answer-content'> Lets you read the current URL's pathname. For example, for the route `/dashboard/invoices`, `usePathname` would return `'/dashboard/invoices'`. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='285' id='card-500120033'> <div class='header'> 285 </div> <div class='card-face question'> <div class='question-content'> `useRouter` </div> </div> <div class='card-face answer'> <div class='answer-content'> Enables navigation between routes within client components programmatically. There are multiple methods you can use. (https://nextjs.org/docs/app/api-reference/functions/use-router#userouter) - `router.push(href: string, { scroll: boolean }):` Perform a client-side navigation to the provided route. Adds a new entry into the browser’s history stack. - `router.replace(href: string, { scroll: boolean })`: Perform a client-side navigation to the provided route without adding a new entry into the browser’s history stack. -`router.refresh()`: Refresh the current route. Making a new request to the server, re-fetching data requests, and re-rendering Server Components. The client will merge the updated React Server Component payload without losing unaffected client-side React (e.g. useState) or browser state (e.g. scroll position). -`router.prefetch(href: string)`: Prefetch the provided route for faster client-side transitions. - `router.back()`: Navigate back to the previous route in the browser’s history stack. - `router.forward()`: Navigate forwards to the next page in the browser’s history stack. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='286' id='card-500120323'> <div class='header'> 286 </div> <div class='card-face question'> <div class='question-content'> - `router.push(href: string, { scroll: boolean }):` </div> </div> <div class='card-face answer'> <div class='answer-content'> Perform a client-side navigation to the provided route. Adds a new entry into the browser’s history stack. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='287' id='card-500120370'> <div class='header'> 287 </div> <div class='card-face question'> <div class='question-content'> `router.replace(href: string, { scroll: boolean })`: </div> </div> <div class='card-face answer'> <div class='answer-content'> Perform a client-side navigation to the provided route without adding a new entry into the browser’s history stack. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='288' id='card-500120517'> <div class='header'> 288 </div> <div class='card-face question'> <div class='question-content'> `router.refresh()`: </div> </div> <div class='card-face answer'> <div class='answer-content'> Refresh the current route. Making a new request to the server, re-fetching data requests, and re-rendering Server Components. The client will merge the updated React Server Component payload without losing unaffected client-side React (e.g. useState) or browser state (e.g. scroll position). </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='289' id='card-500120612'> <div class='header'> 289 </div> <div class='card-face question'> <div class='question-content'> `router.prefetch(href: string)`: </div> </div> <div class='card-face answer'> <div class='answer-content'> Prefetch the provided route for faster client-side transitions. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='290' id='card-500120647'> <div class='header'> 290 </div> <div class='card-face question'> <div class='question-content'> `router.back()`: </div> </div> <div class='card-face answer'> <div class='answer-content'> Navigate back to the previous route in the browser’s history stack. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='291' id='card-500120693'> <div class='header'> 291 </div> <div class='card-face question'> <div class='question-content'> `router.forward()` </div> </div> <div class='card-face answer'> <div class='answer-content'> Navigate forwards to the next page in the browser’s history stack </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='292' id='card-500120807'> <div class='header'> 292 </div> <div class='card-face question'> <div class='question-content'> What are the steps for implementing **search functionality** in NextJs </div> </div> <div class='card-face answer'> <div class='answer-content'> Here's a quick overview of the implementation steps: 1. Capture the user's input. 2. Update the URL with the search params. 3. Keep the URL in sync with the input field. (client side) 4. Update the table to reflect the search query. (server side) </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='293' id='card-500122253'> <div class='header'> 293 </div> <div class='card-face question'> <div class='question-content'> `defaultValue` vs. `value` / **Controlled** vs. **Uncontrolled** </div> </div> <div class='card-face answer'> <div class='answer-content'> If you're using `state` to manage the value of an `input`, you'd use the `value` attribute to make it a **controlled component**. This means **React would manage the input's state.** However, since you're *not* using state, you can use `defaultValue`. This means the **native input** will manage its own state. This is okay since you're saving the search query to the URL instead of state. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='294' id='card-500123232'> <div class='header'> 294 </div> <div class='card-face question'> <div class='question-content'> When to use the `useSearchParams()` hook vs. the `searchParams` prop? </div> </div> <div class='card-face answer'> <div class='answer-content'> You might have noticed you used two different ways to extract search params. Whether you use one or the other **depends on** whether you're working on **the client** or **the server**. `<Search>` is a **Client Component**, so you used the `useSearchParams()` hook to access the params *from the client.* `<Table>` is a **Server Component** that **fetches its own data**, so you can pass the `searchParams` prop from the page to the component. As a **general rule**, if you want to read the params from the client, use the `useSearchParams()` hook as this avoids having to go back to the server. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='295' id='card-500123795'> <div class='header'> 295 </div> <div class='card-face question'> <div class='question-content'> What is **Debouncing**? </div> </div> <div class='card-face answer'> <div class='answer-content'> a programming practice that limits the rate at which a function can fire. In our case, you only want to query the database when the user has stopped typing. ex. ``` Searching... E Searching... Em Searching... Emi Searching... Emil ``` **How Debouncing Works:** 1. **Trigger Event:** When an event that should be debounced (like a keystroke in the search box) occurs, a timer starts. 2. **Wait:** If a new event occurs before the timer expires, the timer is reset. 3. **Execution:** If the timer reaches the end of its countdown, the debounced function is executed. ex `npm i use-debounce` By debouncing, you can reduce the number of requests sent to your database, thus saving resources. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='296' id='card-500124576'> <div class='header'> 296 </div> <div class='card-face question'> <div class='question-content'> Why is it important **not** to fetch data in a **Client Component**? </div> </div> <div class='card-face answer'> <div class='answer-content'> You **don't** want to** fetch data on the client** as this would *expose your database secrets* (remember, you're not using an API layer). Instead, you can **fetch the data on the server **(in a `server component`), and pass it to the `client component` as a `prop`. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='297' id='card-500125678'> <div class='header'> 297 </div> <div class='card-face question'> <div class='question-content'> Explain this code: ``` const createPageURL = (pageNumber: number | string) => { const params = new URLSearchParams(searchParams); params.set('page', pageNumber.toString()); return `${pathname}?${params.toString()}`; }; ``` </div> </div> <div class='card-face answer'> <div class='answer-content'> Here's a breakdown of what's happening: - `createPageURL` creates an instance of the current search parameters. - Then, it updates the "page" parameter to the provided page number. - Finally, it constructs the full URL using the pathname and updated search parameters. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='298' id='card-500126580'> <div class='header'> 298 </div> <div class='card-face question'> <div class='question-content'> What is an advantage of invoking a **Server Action** within a **Server Component**? </div> </div> <div class='card-face answer'> <div class='answer-content'> An advantage of invoking a Server Action within a Server Component is **progressive enhancement** - forms work even if JavaScript is disabled on the client. **Good to know:** In **HTML**, you'd pass a URL to the `action` attribute. This URL would be the destination where your form data should be submitted (usually an API endpoint). However, in **React**, the `action` attribute is considered *a special prop* - meaning React builds on top of it to allow actions to be invoked. Behind the scenes, **Server Actions** create a `POST API endpoint`. This is why you don't need to create API endpoints manually when using Server Actions. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='299' id='card-500126734'> <div class='header'> 299 </div> <div class='card-face question'> <div class='question-content'> what is **progressive enhancement**? </div> </div> <div class='card-face answer'> <div class='answer-content'> allows users to interact with the form and submit data even if the JavaScript for the form hasn't been loaded yet or if it fails to load. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='300' id='card-500128316'> <div class='header'> 300 </div> <div class='card-face question'> <div class='question-content'> Tip for working with Server Actions in forms with lots of fields: </div> </div> <div class='card-face answer'> <div class='answer-content'> **Tip:** If you're working with forms that have many fields, you may want to consider using the `entries()` method with JavaScript's `Object.fromEntries()`. For example: ``` const rawFormData = Object.fromEntries(formData.entries()) ``` </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='301' id='card-500129583'> <div class='header'> 301 </div> <div class='card-face question'> <div class='question-content'> What are **Dynamic Route Segments** and what do they allow you to do? </div> </div> <div class='card-face answer'> <div class='answer-content'> `Next.js` allows you to create **Dynamic Route Segments** when you don't know the exact segment name and want to create routes based on data. This could be blog post titles, product pages, etc. **You can create dynamic route segments by wrapping a folder's name in _square brackets_**. For example, `[id]`, `[post]` or `[slug]`. Example: These are the steps you'll take to update an invoice: 1. Create a new dynamic route segment with the invoice id. 2. Read the invoice id from the page params. 3. Fetch the specific invoice from your database. 4. Pre-populate the form with the invoice data. 5. Update the invoice data in your database. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='302' id='card-500130411'> <div class='header'> 302 </div> <div class='card-face question'> <div class='question-content'> **UUIDs** vs. **Auto-incrementing Keys** </div> </div> <div class='card-face answer'> <div class='answer-content'> We use UUIDs instead of incrementing keys (e.g., 1, 2, 3, etc.). This makes the URL longer; however, UUIDs eliminate the risk of ID collision, are globally unique, and reduce the risk of enumeration attacks - making them ideal for large databases. However, if you prefer cleaner URLs, you might prefer to use auto-incrementing keys. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='303' id='card-500132649'> <div class='header'> 303 </div> <div class='card-face question'> <div class='question-content'> Why should `redirect()` be called outside of the `try/catch` block? </div> </div> <div class='card-face answer'> <div class='answer-content'> Note how `redirect` is being called outside of the `try/catch` block. This is because `redirect` works by throwing an error, which would be caught by the catch block. To avoid this, you can call `redirect` _after_ `try/catch`. `redirect` would only be reachable if `try` is successful. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='304' id='card-500133010'> <div class='header'> 304 </div> <div class='card-face question'> <div class='question-content'> What do you need to remember about `error.tsx` files? </div> </div> <div class='card-face answer'> <div class='answer-content'> 1. `"use client"` - `error.tsx` needs to be a **Client Component.** 2. It accepts `two props`: - `error`: This object is an instance of JavaScript's native Error object. - `reset`: This is a function to reset the error boundary. When executed, the function will try to re-render the route segment. </div> </div> </div> <div class='flashcard-row thin-card is-blurrable' data-a-button-url='/sign-up?packId=21961683&returnTo=%2Fpacks%2F21961683%2Fsubscribe&source=preview-card&subject=Coding+Studying' data-card-is-blurrable data-number='305' id='card-500141076'> <div class='header'> 305 </div> <div class='card-face question'> <div class='question-content'> </div> </div> <div class='card-face answer'> <div class='answer-content'> </div> </div> </div> </div> </div> </div> <div class='flashcards-sidebar'> <div class='sidebar-header'> <div class='react-component' id='flashcards-search-bar'> <div class='placeholder market-search-bar' id='flashcards-search-bar-placeholder'></div> </div> </div> <div class='sidebar-content'> <p class='deck-subject-heading'> <a class="decks-in-subject-link" href="/packs/coding-studying-21961683"><span class="pack-name">Coding Studying</span> (14 decks) </a></p> <ul class='deck-list-items'> <a class='deck-link ' href='/flashcards/python-cheetsheet-15415457/packs/21961683'> <li class='deck-list-item'>Python Cheetsheet</li> </a> <a class='deck-link ' href='/flashcards/javascript-array-functions-15466402/packs/21961683'> <li class='deck-list-item'>JavaScript Array Functions</li> </a> <a class='deck-link ' href='/flashcards/javascript-object-functions-15466870/packs/21961683'> <li class='deck-list-item'>JavaScript Object Functions</li> </a> <a class='deck-link ' href='/flashcards/javascript-cheatsheet-15466977/packs/21961683'> <li class='deck-list-item'>JavaScript CheatSheet</li> </a> <a class='deck-link ' href='/flashcards/javascript-interview-questions-15468517/packs/21961683'> <li class='deck-list-item'>JavaScript Interview Questions</li> </a> <a class='deck-link ' href='/flashcards/mdn-docs-study-15526828/packs/21961683'> <li class='deck-list-item'>MDN docs study</li> </a> <a class='deck-link selected' href='/flashcards/nextjs-react-15530449/packs/21961683'> <li class='deck-list-item'>NextJS/React</li> </a> <a class='deck-link ' href='/flashcards/freecodecamp-js-interview-studying-15571917/packs/21961683'> <li class='deck-list-item'>FreeCodeCamp JS interview studying</li> </a> <a class='deck-link ' href='/flashcards/pythoncheatsheetorg-15574590/packs/21961683'> <li class='deck-list-item'>PythonCheatsheet.Org</li> </a> <a class='deck-link ' href='/flashcards/graphql-docs-15582995/packs/21961683'> <li class='deck-list-item'>GraphQL Docs</li> </a> <a class='deck-link ' href='/flashcards/docker-15583456/packs/21961683'> <li class='deck-list-item'>Docker</li> </a> <a class='deck-link ' href='/flashcards/chatgpt-python-questions-15584647/packs/21961683'> <li class='deck-list-item'>ChatGPT Python Questions</li> </a> <a class='deck-link ' href='/flashcards/amazon-s3-15615289/packs/21961683'> <li class='deck-list-item'>Amazon S3</li> </a> <a class='deck-link ' href='/flashcards/the-anatomy-of-the-swipe-15976040/packs/21961683'> <li class='deck-list-item'>The Anatomy of the Swipe</li> </a> </ul> </div> </div> </div> <div id='tooltip-controller'></div> <div data='{"packId":21961683,"source":"spaced-repetition-modal","subject":"Coding Studying","resources":{"deckId":15530449,"packId":21961683},"returnTo":"/packs/21961683/subscribe"}' id='spaced-repetition-modal-controller'></div> <div id='banner-controller'></div> <div id='dialog-modal-controller'></div> <div class='band band-footer'> <div class='footer-main'> <ul class='sections'> <li class='section key-links'> <p class='section-heading'> Key Links </p> <ul class='options-list'> <li class='option'> <a id="footer-pricing-link" class="option-link" href="/pricing?paywall=upgrade">Pricing</a> </li> <li class='option'> <a class="option-link" href="/companies">Corporate Training</a> </li> <li class='option'> <a class="option-link" href="/teachers">Teachers & Schools</a> </li> <li class='option'> <a class="option-link" target="_blank" rel="nofollow noopener noreferrer" href="https://itunes.apple.com/us/app/brainscape-smart-flashcards/id442415567?mt=8">iOS App</a> </li> <li class='option'> <a class="option-link" target="_blank" rel="nofollow noopener noreferrer" href="https://play.google.com/store/apps/details?id=com.brainscape.mobile.portal">Android App</a> </li> <li class='option'> <a class="option-link" target="_blank" rel="noopener" href="https://www.brainscape.com/faq">Help Center</a> </li> </ul> </li> <li class='section subjects'> <p class='section-heading'> Subjects </p> <ul class='options-list'> <li class='option'> <a class="option-link" href="/subjects/medical-nursing">Medical & Nursing</a> </li> <li class='option'> <a class="option-link" href="/subjects/law">Law Education</a> </li> <li class='option'> <a class="option-link" href="/subjects/foreign-languages">Foreign Languages</a> </li> <li class='option'> <a class="option-link" href="/subjects-directory/a">All Subjects A-Z</a> </li> <li class='option certified-classes'> <a class="option-link" href="/learn">All Certified Classes</a> </li> </ul> </li> <li class='section company'> <p class='section-heading'> Company </p> <ul class='options-list'> <li class='option'> <a class="option-link" href="/about">About Us</a> </li> <li class='option'> <a target="_blank" class="option-link" rel="nofollow noopener noreferrer" href="https://brainscape.zendesk.com/hc/en-us/articles/115002370011-Can-I-earn-money-from-my-flashcards-">Earn Money!</a> </li> <li class='option'> <a target="_blank" class="option-link" href="https://www.brainscape.com/academy">Academy</a> </li> <li class='option'> <a target="_blank" class="option-link" href="https://brainscapeshop.myspreadshop.com/all">Swag Shop</a> </li> <li class='option'> <a target="_blank" rel="nofollow noopener" class="option-link" href="/contact">Contact</a> </li> <li class='option'> <a target="_blank" rel="nofollow noopener" class="option-link" href="/terms">Terms</a> </li> <li class='option'> <a target="_blank" class="option-link" href="https://www.brainscape.com/academy/brainscape-podcasts/">Podcasts</a> </li> <li class='option'> <a target="_blank" class="option-link" href="/careers">Careers</a> </li> </ul> </li> <li class='section find-us'> <p class='section-heading'> Find Us </p> <ul class='social-media-list'> <li class='option twitter-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://twitter.com/Brainscape"><img data-src="/pks/images/shared/twitterx-af917e8b474ed7c95a19.svg" alt="twitter badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> <li class='option linkedin-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://www.linkedin.com/company/brainscape/"><img data-src="/pks/images/shared/linkedin-2f15819658f768056cef.svg" alt="linkedin badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> <li class='option facebook-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://www.facebook.com/Brainscape"><img data-src="/pks/images/shared/facebook-1598a44227eabc411188.svg" alt="facebook badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> <li class='option youtube-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://www.youtube.com/c/BrainscapeNY"><img data-src="/pks/images/shared/youtube-7f2994b2dc1891582524.svg" alt="youtube badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> <li class='option pinterest-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://www.pinterest.com/brainscape/"><img data-src="/pks/images/shared/pinterest-04f51aa292161075437b.svg" alt="pinterest badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> <li class='option tiktok-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://www.tiktok.com/@brainscapeu"><img data-src="/pks/images/shared/tiktok-644cf4608bd73fbbb24f.svg" alt="tiktok badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> <li class='option insta-badge group'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://www.instagram.com/brainscape/"><img data-src="/pks/images/shared/insta-210cc2d059ae807961d2.svg" alt="insta badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="24" height="24" /></a> </li> </ul> <div class='get-the-app'> <div class='qr-code'> <img data-src="https://www.brainscape.com/assets/cms/public-views/shared/shortio-from-homepage.png" alt="QR code" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="130" height="130" /> </div> <div class='app-badges'> <div class='badge apple-badge'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://apps.apple.com/us/app/brainscape-smart-flashcards/id442415567"><img data-src="/pks/images/shared/apple-badge-b6e4f380fb879821d601.svg" alt="apple badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="124" height="50" /></a> </div> <div class='badge android-badge'> <a rel="nofollow noopener noreferrer" target="_blank" class="option-link" href="https://play.google.com/store/apps/details?id=com.brainscape.mobile.portal&utm_source=global_co&utm_medium=prtnr&utm_content=Mar2515&utm_campaign=PartBadge&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1"><img data-src="/pks/images/shared/android-badge-a2251833dc7f6ca8879c.svg" alt="android badge" class="lazy-load" src="/pks/images/shared/placeholder-2f8e0834f3c4456dc1cc.jpg" width="124" height="50" /></a> </div> </div> </div> </li> </ul> </div> <div class='footer-blurb'> Brainscape helps you reach your goals faster, through stronger study habits. <br> © 2025 Bold Learning Solutions. <a class="option-link" href="/terms">Terms and Conditions</a> </div> </div> <script> if (typeof window.__REACT_DEVTOOLS_GLOBAL_HOOK__ === 'object') { __REACT_DEVTOOLS_GLOBAL_HOOK__.inject = function() {}; } </script> <script> window.addEventListener('load', () => { setTimeout(() => { const script = document.createElement('script'); script.src = "/pks/js/public-deck-cards-page-d0410d84e8b38e8511bf.js"; script.defer = true; document.body.appendChild(script); }, 0); }); </script> <script src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js" defer="defer"></script> <script> document.addEventListener("mainSharedready", () => { GaHelper.setGaDimension("dimension1","No"); }); </script> <script type='application/ld+json'> {"@context":"https://schema.org/","@type":"Quiz","about":{"@type":"Thing","name":"NextJS/React"},"hasPart":[{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the React Server Component Payload (RSC)?","acceptedAnswer":{"@type":"Answer","text":"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"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are three different server rendering strategies for Server Components?","acceptedAnswer":{"@type":"Answer","text":"- Static Rendering - Dynamic Rendering - Streaming"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are the benefits of Server side Rendering?","acceptedAnswer":{"@type":"Answer","text":"- Security - Caching - Performance - Initial Page Load - SEO - Streaming"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is Static Rendering (Default)","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How does Server Dynamic Rendering work?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Dynamic Routes with Cached Data","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"As a developer, do you need to choose between static and dynamic rendering in NextJS?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are dynamic functions?","acceptedAnswer":{"@type":"Answer","text":"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()` and `headers():` 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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are Client Components in NextJS?","acceptedAnswer":{"@type":"Answer","text":"Client Components allow you to write interactive UI that is prerendered on the server and can use client JavaScript to run in the browser."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How are Client Components Rendered?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How are Client Components Rendered on Full page load?","acceptedAnswer":{"@type":"Answer","text":"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: 1. React renders Server Components into a special data format called the React Server Component Payload (RSC Payload), which includes references to Client Components. 2. Next.js uses the RSC Payload and Client Component JavaScript instructions to render HTML for the route on the server. Then, on the client: 1. The HTML is used to immediately show a fast non-interactive initial preview of the route. 2. The React Server Components Payload is used to reconcile the Client and Server Component trees, and update the DOM. 3. The JavaScript instructions are used to hydrate Client Components and make their UI interactive."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is hydration?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How are Client Components Rendered on Subsequent Navigations?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are some common patterns for working with Server Components?","acceptedAnswer":{"@type":"Answer","text":"1. Sharing data between components 2. Keeping Server-only code out of the Client Environment 3. Using Third-party Packages and Providers 4. Using Context Providers"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are some common patterns for working with Client Components?","acceptedAnswer":{"@type":"Answer","text":"1. Moving Client Components Down the Tree 2. Serialization - passing props from Server to Client Components 3. Interleaving Server and Client Components - anti pattern: importing server comps into client comps - supported pattern: passing Server Comps to Client Components as props"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Explain the Server Component pattern where you Share data between components.","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Explain the Client Component pattern: Passing props from Server to Client Components (Serialization)","acceptedAnswer":{"@type":"Answer","text":"If you fetch data in a Server Component, you may want to pass data down as props to Client Components. Props passed from the Server to Client Components need to be serializable by React. If your Client Components depend on data that is not serializable, you can fetch data on the client with a third party library or on the server via a Route Handler."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Explain the Client Component pattern: Interleaving Server and Client Components.","acceptedAnswer":{"@type":"Answer","text":"When interleaving Client and Server Components, it may be helpful to visualize your UI as a tree of components. Starting with the root layout, which is a Server Component, you can then render certain subtrees of components on the client by adding the \"use client\" directive. Within those client subtrees, you can still nest Server Components or call Server Actions, however there are some things to keep in mind: - During a request-response lifecycle, your code moves from the server to the client. If you need to access data or resources on the server while on the client, you'll be making a new request to the server - not switching back and forth. - When a new request is made to the server, all Server Components are rendered first, including those nested inside Client Components. The rendered result (RSC Payload) will contain references to the locations of Client Components. Then, on the client, React uses the RSC Payload to reconcile Server and Client Components into a single tree. - Since Client Components are rendered after Server Components, you cannot import a Server Component into a Client Component module (since it would require a new request back to the server). Instead, you can pass a Server Component as props to a Client Component. See the unsupported pattern and supported pattern sections below."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are runtimes in the context of Next.js","acceptedAnswer":{"@type":"Answer","text":"In the context of Next.js, runtime refers to the set of libraries, APIs, and general functionality available to your code during execution. On the server, there are two runtimes where parts of your application code can be rendered: - The Node.js Runtime (default) has access to all Node.js APIs and compatible packages from the ecosystem. - The Edge Runtime is based on Web APIs."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the Edge Runtime in NextJS and when should you use it?","acceptedAnswer":{"@type":"Answer","text":"In Next.js, the lightweight Edge Runtime is a subset of available Node.js APIs. The Edge Runtime is ideal if you need to deliver dynamic, personalized content at low latency with small, simple functions. The Edge Runtime's speed comes from its minimal use of resources, but that can be limiting in many scenarios. For example, code executed in the Edge Runtime on Vercel cannot exceed between 1 MB and 4 MB, this limit includes imported packages, fonts and files, and will vary depending on your deployment infrastructure. In addition, the Edge Runtime does not support all Node.js APIs meaning some npm packages may not work. For example, \"Module not found: Can't resolve 'fs'\" or similar errors. We recommend using the Node.js runtime if you need to use these APIs or packages."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the Node.js Runtime in NextJS and when should you use it?","acceptedAnswer":{"@type":"Answer","text":"Using the Node.js runtime gives you access to all Node.js APIs, and all npm packages that rely on them. However, it's not as fast to start up as routes using the Edge runtime. Deploying your Next.js application to a Node.js server will require managing, scaling, and configuring your infrastructure. Alternatively, you can consider deploying your Next.js application to a serverless platform like Vercel, which will handle this for you."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"When should you use Serverless Node.js? What are the downsides to using it?","acceptedAnswer":{"@type":"Answer","text":"Serverless is ideal if you need a scalable solution that can handle more complex computational loads than the Edge Runtime. With Serverless Functions on Vercel, for example, your overall code size is 50MB including imported packages, fonts, and files. The downside compared to routes using the Edge is that it can take hundreds of milliseconds for Serverless Functions to boot up before they begin processing requests. Depending on the amount of traffic your site receives, this could be a frequent occurrence as the functions are not frequently \"warm\"."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is First Contentful Paint? (FCP)","acceptedAnswer":{"@type":"Answer","text":"First Contentful Paint (FCP) is one of six metrics tracked in the Performance section of the Lighthouse report. Each metric captures some aspect of page load speed. FCP measures how long it takes the browser to render the first piece of DOM content after a user navigates to your page. Images, non-white elements, and SVGs on your page are considered DOM content; anything inside an iframe isn't included."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is Time to Interactive (TTI)? Why is it important to measure?","acceptedAnswer":{"@type":"Answer","text":"TTI measures how long it takes a page to become fully interactive. A page is considered fully interactive when: - The page displays useful content, which is measured by the First Contentful Paint, - Event handlers are registered for most visible page elements, and - The page responds to user interactions within 50 milliseconds. Measuring TTI is important because some sites optimize content visibility at the expense of interactivity. This can create a frustrating user experience: the site appears to be ready, but when the user tries to interact with it, nothing happens. sites performing in the ninety-ninth percentile render TTI in about 2.2 seconds. If your website's TTI is 2.2 seconds, your TTI score is 99."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How do you improve your TTI score?","acceptedAnswer":{"@type":"Answer","text":"One improvement that can have a particularly big effect on TTI is deferring or removing unnecessary JavaScript work. Look for opportunities to optimize your JavaScript. In particular, consider reducing JavaScript payloads with code splitting and applying the PRPL pattern. Optimizing third-party JavaScript also yields significant improvements for some sites. These two Diagnostic audits provide additional opportunities to reduce JavaScript work: - Minimize main thread work - Reduce JavaScript execution time"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How does Streaming with Suspense interact with SEO?","acceptedAnswer":{"@type":"Answer","text":"- Next.js will wait for data fetching inside `generateMetadata` to complete before streaming UI to the client. This guarantees the first part of a streamed response includes tags. - Since streaming is server-rendered, it does not impact SEO. You can use the Rich Results Test tool from Google to see how your page appears to Google's web crawlers and view the serialized HTML (source)."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How do Status Codes work when Streaming w/ Suspense?","acceptedAnswer":{"@type":"Answer","text":"When streaming, a 200 status code will be returned to signal that the request was successful. The server can still communicate errors or issues to the client within the streamed content itself, for example, when using `redirect` or `notFound`. Since the response headers have already been sent to the client, the status code of the response cannot be updated. This does not affect SEO."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are Server Actions? How do you use them?","acceptedAnswer":{"@type":"Answer","text":"Server Actions are asynchronous functions that are executed on the server. They can be used in Server and Client Components to handle form submissions and data mutations in Next.js applications. A Server Action can be defined with the React \"use server\" directive. You can place the directive at the top of an async function to mark the function as a Server Action, or at the top of a separate file to mark all exports of that file as Server Actions."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What does it mean that Server Components support progressive enhancement by default?","acceptedAnswer":{"@type":"Answer","text":"Server Components support progressive enhancement by default, meaning the form will be submitted even if JavaScript hasn't loaded yet or is disabled."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Are Server Actions limited to elements?","acceptedAnswer":{"@type":"Answer","text":"No, Server Actions are not limited to and can be invoked from event handlers, useEffect, third-party libraries, and other form elements like ."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Which part of the NextJS architecture do Server Actions interact with?","acceptedAnswer":{"@type":"Answer","text":"Server Actions integrate with the Next.js caching and revalidation architecture. When an action is invoked, Next.js can return both the updated UI and new data in a single server roundtrip."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"True or False: actions can use multiple types of HTTP methods.","acceptedAnswer":{"@type":"Answer","text":"False. Behind the scenes, actions use the POST method, and only this HTTP method can invoke them."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What does React require of args and return values for Server Actions?","acceptedAnswer":{"@type":"Answer","text":"The arguments and return value of Server Actions must be serializable by React. See the React docs for a list of serializable arguments and values."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What does it mean that \"Server Actions are functions\"?","acceptedAnswer":{"@type":"Answer","text":"Server Actions are functions. This means they can be reused anywhere in your application."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What runtime do Server Actions use?","acceptedAnswer":{"@type":"Answer","text":"Server Actions inherit the runtime from the page or layout they are used on."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Where do Server Actions get their Route Segment Config?","acceptedAnswer":{"@type":"Answer","text":"Server Actions inherit the Route Segment Config from the page or layout they are used on, including fields like maxDuration."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Can you use `useFormStatus` in a Server Component?","acceptedAnswer":{"@type":"Answer","text":"No, `useFormStatus` is a React hook and therefore must be used in a Client Component."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Can you invoke Server Actions in elements nested in forms? If so, why would you want to?","acceptedAnswer":{"@type":"Answer","text":"You can invoke a Server Action in elements nested inside such as , , and . These elements accept the formAction prop or event handlers. This is useful in cases where you want to call multiple server actions within a form. For example, you can create a specific element for saving a post draft in addition to publishing it. See the React docs for more information."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Aside from forms, where else are Server Actions commonly invoked?","acceptedAnswer":{"@type":"Answer","text":"they can also be invoked from other parts of your code such as event handlers and useEffect."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What do you need to be aware of when self-hosting your Next.js application across multiple servers?","acceptedAnswer":{"@type":"Answer","text":"Overwriting encryption keys (advanced). When self-hosting your Next.js application across multiple servers, each server instance may end up with a different encryption key, leading to potential inconsistencies. To mitigate this, you can overwrite the encryption key using the `process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY` environment variable. Specifying this variable ensures that your encryption keys are persistent across builds, and all server instances use the same key. This is an advanced use case where consistent encryption behavior across multiple deployments is critical for your application. You should consider standard security practices such key rotation and signing. Good to know: Next.js applications deployed to Vercel automatically handle this."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What does Next.js do to prevent sensitive data from getting sent to the client in the case of Server Action closures? What are the implications of this?","acceptedAnswer":{"@type":"Answer","text":"To prevent sensitive data from being exposed to the client, Next.js automatically encrypts the closed-over variables. A new private key is generated for each action every time a Next.js application is built. This means actions can only be invoked for a specific build."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How are Server Actions protected from CSRF attacks?","acceptedAnswer":{"@type":"Answer","text":"Behind the scenes, Server Actions use the POST method, and only this HTTP method is allowed to invoke them. This prevents most CSRF vulnerabilities in modern browsers, particularly with SameSite cookies being the default. As an additional protection, Server Actions in Next.js also compare the Origin header to the Host header (or X-Forwarded-Host). If these don't match, the request will be aborted. In other words, Server Actions can only be invoked on the same host as the page that hosts it. NOTE: For large applications that use reverse proxies or multi-layered backend architectures (where the server API differs from the production domain), it's recommended to use the configuration option `serverActions.allowedOrigins` option to specify a list of safe origins. The option accepts an array of strings."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the pattern for only fetching data when it's needed?","acceptedAnswer":{"@type":"Answer","text":"If you need to use the same data (e.g. current user) in multiple components in a tree, you do not have to fetch data globally, nor forward props between components. Instead, you can use `fetch` or React `cache` in the component that needs the data without worrying about the performance implications of making multiple requests for the same data. This is possible because `fetch` requests are automatically memoized. Learn more about request memoization. Good to know: This also applies to layouts, since it's not possible to pass data between a parent layout and its children."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are the two data fetching patterns inside React components?","acceptedAnswer":{"@type":"Answer","text":"When fetching data inside React components, you need to be aware of two data fetching patterns: Parallel and Sequential. - With `sequential data fetching`, requests in a route are dependent on each other and therefore create waterfalls. There may be cases where you want this pattern because one fetch depends on the result of the other, or you want a condition to be satisfied before the next fetch to save resources. However, this behavior can also be unintentional and lead to longer loading times. - With `parallel data fetching`, requests in a route are eagerly initiated and will load data at the same time. This reduces client-server waterfalls and the total time it takes to load data."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is \"all or nothing\" data fetching?","acceptedAnswer":{"@type":"Answer","text":"An alternative approach to prevent waterfalls is to fetch data globally, at the root of your application, but this will block rendering for all route segments beneath it until the data has finished loading. This can be described as \"all or nothing\" data fetching. Either you have the entire data for your page or application, or none."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What happens to fetch requests that use await? What should you be aware of?","acceptedAnswer":{"@type":"Answer","text":"Any fetch requests with await will block rendering and data fetching for the entire tree beneath it, unless they are wrapped in a boundary or loading.js is used. Another alternative is to use parallel data fetching or the preload pattern."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are four ways of fetching data with Next.js and React?","acceptedAnswer":{"@type":"Answer","text":"1. On the server, with fetch 2. On the server, with third-party libraries 3. On the client, via a Route Handler 4. On the client, with third-party libraries."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"When are `fetch` requests NOT memoized?","acceptedAnswer":{"@type":"Answer","text":"In Route handlers, fetch requests are not memoized as Route Handlers are not part of the React component tree."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"When are `fetch` requests NOT cached?","acceptedAnswer":{"@type":"Answer","text":"In Server Actions, fetch requests are not cached (defaults cache: no-store)."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is a side effect of using Next.js `cookies` and `header` functions inside of Server Components?","acceptedAnswer":{"@type":"Answer","text":"Next.js provides helpful functions you may need when fetching data in Server Components such as cookies and headers. These will cause the route to be dynamically rendered as they rely on request time information."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the Data Cache?","acceptedAnswer":{"@type":"Answer","text":"The Data Cache is a persistent HTTP cache. Depending on your platform, the cache can scale automatically and be shared across multiple regions. Learn more about the Data Cache."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the process of Revalidating Data in relation to caching?","acceptedAnswer":{"@type":"Answer","text":"Revalidation is the process of purging the Data Cache and re-fetching the latest data. This is useful when your data changes and you want to ensure you show the latest information."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are the two ways cached data can be revalidated?","acceptedAnswer":{"@type":"Answer","text":"1. Time-based revalidation 2. On-demand revalidation"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is Time-based revalidation? When is it useful?","acceptedAnswer":{"@type":"Answer","text":"Automatically revalidate data after a certain amount of time has passed. This is useful for data that changes infrequently and freshness is not as critical."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is On-demand revalidation? When is it useful?","acceptedAnswer":{"@type":"Answer","text":"Manually revalidate data based on an event (e.g. form submission). On-demand revalidation can use a tag-based or path-based approach to revalidate groups of data at once. This is useful when you want to ensure the latest data is shown as soon as possible (e.g. when content from your headless CMS is updated)."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"In the context of time-based data revalidation, how do multiple fetch requests get handled differently by static vs dynamically rendered routes?","acceptedAnswer":{"@type":"Answer","text":"In a statically rendered route, and each has a different revalidation frequency. The lowest time will be used for all requests. For dynamically rendered routes, each fetch request will be revalidated independently."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What happens when an error gets thrown when you are attempting to revalidate data?","acceptedAnswer":{"@type":"Answer","text":"If an error is thrown while attempting to revalidate data, the last successfully generated data will continue to be served from the cache. On the next subsequent request, Next.js will retry revalidating the data."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"How can you opt out of caching for multiple `fetch` requests?","acceptedAnswer":{"@type":"Answer","text":"If you have multiple `fetch` requests in a route segment (e.g. a Layout or Page), you can configure the caching behavior of all data requests in the segment using the Segment Config Options. However, we recommend configuring the caching behavior of each `fetch` request individually. This gives you more granular control over the caching behavior."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What if you need to fetch data in a client component? What do you use? Why might you want to do this?","acceptedAnswer":{"@type":"Answer","text":"If you need to fetch data in a client component, you can call a Route Handler from the client. Route Handlers execute on the server and return the data to the client. This is useful when you don't want to expose sensitive information to the client, such as API tokens."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Do Server Components ever need the Route Handler to fetch data?","acceptedAnswer":{"@type":"Answer","text":"Since Server Components render on the server, you don't need to call a Route Handler from a Server Component to fetch data. Instead, you can fetch the data directly inside the Server Component."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Can you fetch data on the Client with third party libraries? When would you want to do this?","acceptedAnswer":{"@type":"Answer","text":"You can also fetch data on the client using a third-party library such as SWR or TanStack Query. These libraries provide their own APIs for memoizing requests, caching, revalidating, and mutating data."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"Should you ever wrap `fetch` in `use` in Client Components? why or why not?","acceptedAnswer":{"@type":"Answer","text":"use is a React function that accepts and handles a promise returned by a function. Wrapping `fetch` in `use` is currently not recommended in Client Components and may trigger multiple re-renders. Learn more about use in the React docs."}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What is the app Router? Where does it live?","acceptedAnswer":{"@type":"Answer","text":"In version 13, Next.js introduced a new App Router built on React Server Components, which supports shared layouts, nested routing, loading states, error handling, and more. The App Router works in a new directory named app. By default, components inside app are React Server Components. This is a performance optimization and allows you to easily adopt them, and you can also use Client Components. NOTE: app router takes priority over old pages router, incremental switch over allowed"}},{"@context":"https://schema.org/","@type":"Question","eduQuestionType":"Flashcard","text":"What are the roles of Folders and Files in the Next.js file-system based router?","acceptedAnswer":{"@type":"Answer","text":"- Folders are used to define routes. A route is a single path of nested folders, following the file-system hierarchy from the root folder down to a final leaf folder that includes a page.js file. See Defining Routes. - Files are used to create UI that is shown for a route segment. See special files."}}],"educationalAlignment":[{"@type":"AlignmentObject","alignmentType":"educationalSubject","targetName":"NextJS/React"}]} </script> </body> </html>