Unverified Commit 6130a493 authored by Joseph's avatar Joseph Committed by GitHub

docs: scope React cache() guidance to the render pass (#98741)

Reimpl of: https://github.com/vercel/next.js/pull/98579
parent bdbf63ae
......@@ -1139,7 +1139,9 @@ For example, create a separate file for your DAL that includes a `verifySession(
```tsx filename="app/lib/dal.ts" switcher
import 'server-only'
import { cache } from 'react'
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { decrypt } from '@/app/lib/session'
export const verifySession = cache(async () => {
......@@ -1157,7 +1159,9 @@ export const verifySession = cache(async () => {
```js filename="app/lib/dal.js" switcher
import 'server-only'
import { cache } from 'react'
import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { decrypt } from '@/app/lib/session'
export const verifySession = cache(async () => {
......@@ -1172,7 +1176,9 @@ export const verifySession = cache(async () => {
})
```
You can then invoke the `verifySession()` function in your data requests, Server Actions, Route Handlers:
You can then invoke the `verifySession()` function in your data requests, Server Actions, and Route Handlers:
> **Good to know**: React's [`cache`](https://react.dev/reference/react/cache) deduplicates within a React render pass. Server Actions and Route Handlers are not part of a render pass, so the wrapper does nothing and every call runs the function.
```tsx filename="app/lib/dal.ts" switcher
export const getUser = cache(async () => {
......@@ -1316,7 +1322,7 @@ import { verifySession } from '@/app/lib/dal'
export default async function Dashboard() {
const session = await verifySession()
const userRole = session?.user?.role // Assuming 'role' is part of the session object
const userRole = session?.role // Assuming 'role' is part of the session object
if (userRole === 'admin') {
return <AdminDashboard />
......@@ -1333,7 +1339,7 @@ import { verifySession } from '@/app/lib/dal'
export default async function Dashboard() {
const session = await verifySession()
const userRole = session?.user?.role // Assuming 'role' is part of the session object
const userRole = session?.role // Assuming 'role' is part of the session object
if (userRole === 'admin') {
return <AdminDashboard />
......@@ -1416,7 +1422,7 @@ import { verifySession } from '@/app/lib/dal'
export default async function AdminActions() {
const session = await verifySession()
const userRole = session?.user?.role
const userRole = session?.role
if (userRole !== 'admin') {
return null
......@@ -1436,7 +1442,7 @@ import { verifySession } from '@/app/lib/dal'
export default async function AdminActions() {
const session = await verifySession()
const userRole = session?.user?.role
const userRole = session?.role
if (userRole !== 'admin') {
return null
......@@ -1470,7 +1476,7 @@ import { verifySession } from '@/app/lib/dal'
export async function serverAction(formData: FormData) {
const session = await verifySession()
const userRole = session?.user?.role
const userRole = session?.role
// Return early if user is not authorized to perform the action
if (userRole !== 'admin') {
......@@ -1487,7 +1493,7 @@ import { verifySession } from '@/app/lib/dal'
export async function serverAction() {
const session = await verifySession()
const userRole = session.user.role
const userRole = session?.role
// Return early if user is not authorized to perform the action
if (userRole !== 'admin') {
......@@ -1518,7 +1524,7 @@ export async function GET() {
}
// Check if the user has the 'admin' role
if (session.user.role !== 'admin') {
if (session?.role !== 'admin') {
// User is authenticated but does not have the right permissions
return new Response(null, { status: 403 })
}
......@@ -1541,7 +1547,7 @@ export async function GET() {
}
// Check if the user has the 'admin' role
if (session.user.role !== 'admin') {
if (session?.role !== 'admin') {
// User is authenticated but does not have the right permissions
return new Response(null, { status: 403 })
}
......
......@@ -52,7 +52,7 @@ export default function Layout({ children }) {
## Good to know
- `after` will be executed even if the response didn't complete successfully. Including when an error is thrown or when `notFound` or `redirect` is called.
- You can use React `cache` to deduplicate functions called inside `after`.
- In Server Components, you can use React [`cache`](https://react.dev/reference/react/cache) to deduplicate functions called inside `after`. The callback reuses values already computed during the render. See [Reusing data with `React.cache`](/docs/app/getting-started/fetching-data#reusing-data-with-reactcache).
- `after` can be nested inside other `after` calls, for example, you can create utility functions that wrap `after` calls to add additional functionality.
## Examples
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment