Unverified Commit a549889b authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

docs lint fixes (#71813)

Follow-ups to #71812
parent 9499cc16
...@@ -3,4 +3,4 @@ title: Directives ...@@ -3,4 +3,4 @@ title: Directives
description: Directives are used to modify the behavior of your Next.js application. description: Directives are used to modify the behavior of your Next.js application.
--- ---
The following directives are available: The following directives are available:
\ No newline at end of file
...@@ -3,7 +3,7 @@ title: use client ...@@ -3,7 +3,7 @@ title: use client
description: Learn how to use the use client directive to render a component on the client. description: Learn how to use the use client directive to render a component on the client.
related: related:
description: React documentation for use client. description: React documentation for use client.
links: links:
- https://react.dev/reference/rsc/use-client - https://react.dev/reference/rsc/use-client
--- ---
...@@ -13,7 +13,7 @@ The `use client` directive designates a component to be rendered on the **client ...@@ -13,7 +13,7 @@ The `use client` directive designates a component to be rendered on the **client
To designate a component as a Client Component, add the `use client` directive **at the top of the file**, before any imports: To designate a component as a Client Component, add the `use client` directive **at the top of the file**, before any imports:
````tsx filename="app/components/counter.tsx" highlight={1} switcher ```tsx filename="app/components/counter.tsx" highlight={1} switcher
'use client' 'use client'
import { useState } from 'react' import { useState } from 'react'
...@@ -28,9 +28,9 @@ export default function Counter() { ...@@ -28,9 +28,9 @@ export default function Counter() {
</div> </div>
) )
} }
```` ```
````jsx filename="app/components/counter.js" highlight={1} switcher ```jsx filename="app/components/counter.js" highlight={1} switcher
'use client' 'use client'
import { useState } from 'react' import { useState } from 'react'
...@@ -45,7 +45,7 @@ export default function Counter() { ...@@ -45,7 +45,7 @@ export default function Counter() {
</div> </div>
) )
} }
```` ```
## Nesting Client Components within Server Components ## Nesting Client Components within Server Components
...@@ -88,8 +88,6 @@ export default function Page() { ...@@ -88,8 +88,6 @@ export default function Page() {
} }
``` ```
## Reference ## Reference
See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`. See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`.
...@@ -3,7 +3,7 @@ title: use server ...@@ -3,7 +3,7 @@ title: use server
description: Learn how to use the use server directive to execute code on the server. description: Learn how to use the use server directive to execute code on the server.
related: related:
description: React documentation for use server. description: React documentation for use server.
links: links:
- https://react.dev/reference/rsc/use-server - https://react.dev/reference/rsc/use-server
--- ---
...@@ -79,14 +79,13 @@ export default function MyButton() { ...@@ -79,14 +79,13 @@ export default function MyButton() {
} }
``` ```
## Using `use server` inline ## Using `use server` inline
In the following example, `use server` is used inline at the top of a function to mark it as a [Server Function](https://19.react.dev/reference/rsc/server-functions): In the following example, `use server` is used inline at the top of a function to mark it as a [Server Function](https://19.react.dev/reference/rsc/server-functions):
```tsx filename="app/page.tsx" highlight={5} switcher ```tsx filename="app/page.tsx" highlight={5} switcher
import { db } from '@/lib/db' // Your database client import { db } from '@/lib/db' // Your database client
export default function UserList() { export default function UserList() {
async function fetchUsers() { async function fetchUsers() {
'use server' 'use server'
...@@ -100,14 +99,14 @@ export default function UserList() { ...@@ -100,14 +99,14 @@ export default function UserList() {
```jsx filename="app/page.js" highlight={5} switcher ```jsx filename="app/page.js" highlight={5} switcher
import { db } from '@/lib/db' // Your database client import { db } from '@/lib/db' // Your database client
export default function UserList() { export default function UserList() {
async function fetchUsers() { async function fetchUsers() {
'use server' 'use server'
const users = await db.user.findMany() const users = await db.user.findMany()
return users return users
} }
return <button onClick={() => fetchUsers()}>Fetch Users</button> return <button onClick={() => fetchUsers()}>Fetch Users</button>
} }
``` ```
...@@ -126,7 +125,10 @@ Always authenticate and authorize users before performing sensitive server-side ...@@ -126,7 +125,10 @@ Always authenticate and authorize users before performing sensitive server-side
import { db } from '@/lib/db' // Your database client import { db } from '@/lib/db' // Your database client
import { authenticate } from '@/lib/auth' // Your authentication library import { authenticate } from '@/lib/auth' // Your authentication library
export async function createUser(data: { name: string; email: string }, token: string) { export async function createUser(
data: { name: string; email: string },
token: string
) {
const user = authenticate(token) const user = authenticate(token)
if (!user) { if (!user) {
throw new Error('Unauthorized') throw new Error('Unauthorized')
...@@ -152,7 +154,6 @@ export async function createUser(data, token) { ...@@ -152,7 +154,6 @@ export async function createUser(data, token) {
} }
``` ```
## Reference ## Reference
See the [React documentation](https://react.dev/reference/rsc/use-server) for more information on `use server`. See the [React documentation](https://react.dev/reference/rsc/use-server) for more information on `use server`.
...@@ -18,22 +18,22 @@ The `cacheTag` function allows you to tag cached data for on-demand invalidation ...@@ -18,22 +18,22 @@ The `cacheTag` function allows you to tag cached data for on-demand invalidation
To use `cacheTag`, enable the [`dynamicIO` flag](/docs/app/api-reference/next-config-js/dynamicIO) in your `next.config.js` and import `cacheTag` from `next/cache`: To use `cacheTag`, enable the [`dynamicIO` flag](/docs/app/api-reference/next-config-js/dynamicIO) in your `next.config.js` and import `cacheTag` from `next/cache`:
```ts filename="next.config.ts" ```ts filename="next.config.ts"
import type { NextConfig } from 'next'; import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
} },
}; }
export default nextConfig; export default nextConfig
``` ```
```tsx filename="app/actions.ts" ```tsx filename="app/actions.ts"
import { unstable_cacheTag as cacheTag } from 'next/cache' import { unstable_cacheTag as cacheTag } from 'next/cache'
export async function getData() { export async function getData() {
"use cache" 'use cache'
cacheTag('my-data') cacheTag('my-data')
const data = await fetch('/api/data') const data = await fetch('/api/data')
return data return data
...@@ -62,15 +62,17 @@ export default async function submit() { ...@@ -62,15 +62,17 @@ export default async function submit() {
Tag your cached data by calling `cacheTag` within a cached function or component: Tag your cached data by calling `cacheTag` within a cached function or component:
```tsx filename="app/components/bookings.tsx" ```tsx filename="app/components/bookings.tsx"
import { unstable_cacheTag as cacheTag, unstable_cacheLife as cacheLife } from 'next/cache' import {
unstable_cacheTag as cacheTag,
unstable_cacheLife as cacheLife,
} from 'next/cache'
interface BookingsProps { interface BookingsProps {
type: string type: string
} }
export async function Bookings({type = 'massage'}: BookingsProps) { export async function Bookings({ type = 'massage' }: BookingsProps) {
"use cache" 'use cache'
cacheLife('minutes') cacheLife('minutes')
cacheTag('bookings-data') cacheTag('bookings-data')
...@@ -88,15 +90,18 @@ export async function Bookings({type = 'massage'}: BookingsProps) { ...@@ -88,15 +90,18 @@ export async function Bookings({type = 'massage'}: BookingsProps) {
You can use the data returned from an async function to tag the cache entry. You can use the data returned from an async function to tag the cache entry.
```tsx filename="app/components/bookings.tsx" ```tsx filename="app/components/bookings.tsx"
import { unstable_cacheTag as cacheTag, unstable_cacheLife as cacheLife } from 'next/cache' import {
unstable_cacheTag as cacheTag,
unstable_cacheLife as cacheLife,
} from 'next/cache'
interface BookingsProps { interface BookingsProps {
type: string type: string
} }
export async function Bookings({type = 'massage'}: BookingsProps) { export async function Bookings({ type = 'massage' }: BookingsProps) {
async function getBookingsData() { async function getBookingsData() {
"use cache" 'use cache'
cacheLife('minutes') cacheLife('minutes')
const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`) const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`)
cacheTag('bookings-data', data.id) cacheTag('bookings-data', data.id)
...@@ -125,7 +130,7 @@ export async function updateBookings() { ...@@ -125,7 +130,7 @@ export async function updateBookings() {
- **Idempotent Tags**: Applying the same tag multiple times has no additional effect. - **Idempotent Tags**: Applying the same tag multiple times has no additional effect.
- **Multiple Tags**: You can assign multiple tags to a single cache entry by passing an array to `cacheTag`. - **Multiple Tags**: You can assign multiple tags to a single cache entry by passing an array to `cacheTag`.
```tsx ```tsx
cacheTag(['tag-one', 'tag-two']) cacheTag(['tag-one', 'tag-two'])
``` ```
...@@ -18,20 +18,20 @@ module.exports = { ...@@ -18,20 +18,20 @@ module.exports = {
blog: { blog: {
stale: 3600, // 1 hour stale: 3600, // 1 hour
revalidate: 900, // 15 minutes revalidate: 900, // 15 minutes
expire: 86400 // 1 day expire: 86400, // 1 day
}, },
}, },
} },
} }
``` ```
You can now use this custom `blog` configuration in your component, function or file as follows: You can now use this custom `blog` configuration in your component, function or file as follows:
```tsx filename="app/actions.ts" highlight={4,5} ```tsx filename="app/actions.ts" highlight={4,5}
import { unstable_cacheLife as cacheLife} from 'next/cache' import { unstable_cacheLife as cacheLife } from 'next/cache'
export async function getCachedData() { export async function getCachedData() {
"use cache" 'use cache'
cacheLife('blog') cacheLife('blog')
const data = await fetch('/api/data') const data = await fetch('/api/data')
return data return data
...@@ -42,8 +42,8 @@ export async function getCachedData() { ...@@ -42,8 +42,8 @@ export async function getCachedData() {
The configuration object has key values with the following format: The configuration object has key values with the following format:
| **Property** | **Value** | **Description** | **Requirement** | | **Property** | **Value** | **Description** | **Requirement** |
| --- | --- | --- | --- | | ------------ | --------- | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
| `stale` | `number` | Duration the client should cache a value without checking the server. | Optional | | `stale` | `number` | Duration the client should cache a value without checking the server. | Optional |
| `revalidate` | `number` | Frequency at which the cache should refresh on the server; stale values may be served while revalidating. | Optional | | `revalidate` | `number` | Frequency at which the cache should refresh on the server; stale values may be served while revalidating. | Optional |
| `expire` | `number` | Maximum duration for which a value can remain stale before switching to dynamic fetching; must be longer than `revalidate`. | Optional - Must be longer than `revalidate` | | `expire` | `number` | Maximum duration for which a value can remain stale before switching to dynamic fetching; must be longer than `revalidate`. | Optional - Must be longer than `revalidate` |
\ No newline at end of file
...@@ -15,15 +15,15 @@ It is expected to be used in conjunction with [`use cache`](/docs/app/api-refere ...@@ -15,15 +15,15 @@ It is expected to be used in conjunction with [`use cache`](/docs/app/api-refere
To enable the `dynamicIO` flag, set it to `true` in the `experimental` section of your `next.config.ts` file: To enable the `dynamicIO` flag, set it to `true` in the `experimental` section of your `next.config.ts` file:
```ts filename="next.config.ts" ```ts filename="next.config.ts"
import type { NextConfig } from 'next'; import type { NextConfig } from 'next'
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
} },
}; }
export default nextConfig; export default nextConfig
``` ```
When `dynamicIO` is enabled, you can use the following cache functions and configurations: When `dynamicIO` is enabled, you can use the following cache functions and configurations:
...@@ -34,4 +34,4 @@ When `dynamicIO` is enabled, you can use the following cache functions and confi ...@@ -34,4 +34,4 @@ When `dynamicIO` is enabled, you can use the following cache functions and confi
## Notes ## Notes
- While `dynamicIO` can optimize performance by ensuring fresh data fetching during runtime, it may also introduce additional latency compared to serving pre-rendered content. - While `dynamicIO` can optimize performance by ensuring fresh data fetching during runtime, it may also introduce additional latency compared to serving pre-rendered content.
\ No newline at end of file
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