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
description: Directives are used to modify the behavior of your Next.js application.
---
The following directives are available:
\ No newline at end of file
The following directives are available:
......@@ -3,7 +3,7 @@ title: use client
description: Learn how to use the use client directive to render a component on the client.
related:
description: React documentation for use client.
links:
links:
- https://react.dev/reference/rsc/use-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:
````tsx filename="app/components/counter.tsx" highlight={1} switcher
```tsx filename="app/components/counter.tsx" highlight={1} switcher
'use client'
import { useState } from 'react'
......@@ -28,9 +28,9 @@ export default function Counter() {
</div>
)
}
````
```
````jsx filename="app/components/counter.js" highlight={1} switcher
```jsx filename="app/components/counter.js" highlight={1} switcher
'use client'
import { useState } from 'react'
......@@ -45,7 +45,7 @@ export default function Counter() {
</div>
)
}
````
```
## Nesting Client Components within Server Components
......@@ -88,8 +88,6 @@ export default function Page() {
}
```
## Reference
See the [React documentation](https://react.dev/reference/rsc/use-client) for more information on `use client`.
......@@ -3,7 +3,7 @@ title: use server
description: Learn how to use the use server directive to execute code on the server.
related:
description: React documentation for use server.
links:
links:
- https://react.dev/reference/rsc/use-server
---
......@@ -79,14 +79,13 @@ export default function MyButton() {
}
```
## 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):
```tsx filename="app/page.tsx" highlight={5} switcher
import { db } from '@/lib/db' // Your database client
export default function UserList() {
async function fetchUsers() {
'use server'
......@@ -100,14 +99,14 @@ export default function UserList() {
```jsx filename="app/page.js" highlight={5} switcher
import { db } from '@/lib/db' // Your database client
export default function UserList() {
async function fetchUsers() {
'use server'
const users = await db.user.findMany()
return users
}
return <button onClick={() => fetchUsers()}>Fetch Users</button>
}
```
......@@ -126,7 +125,10 @@ Always authenticate and authorize users before performing sensitive server-side
import { db } from '@/lib/db' // Your database client
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)
if (!user) {
throw new Error('Unauthorized')
......@@ -152,7 +154,6 @@ export async function createUser(data, token) {
}
```
## Reference
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
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"
import type { NextConfig } from 'next';
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
dynamicIO: true,
}
};
export default nextConfig;
},
}
export default nextConfig
```
```tsx filename="app/actions.ts"
import { unstable_cacheTag as cacheTag } from 'next/cache'
export async function getData() {
"use cache"
'use cache'
cacheTag('my-data')
const data = await fetch('/api/data')
return data
......@@ -62,15 +62,17 @@ export default async function submit() {
Tag your cached data by calling `cacheTag` within a cached function or component:
```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 {
type: string
}
export async function Bookings({type = 'massage'}: BookingsProps) {
"use cache"
export async function Bookings({ type = 'massage' }: BookingsProps) {
'use cache'
cacheLife('minutes')
cacheTag('bookings-data')
......@@ -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.
```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 {
type: string
}
export async function Bookings({type = 'massage'}: BookingsProps) {
export async function Bookings({ type = 'massage' }: BookingsProps) {
async function getBookingsData() {
"use cache"
'use cache'
cacheLife('minutes')
const data = await fetch(`/api/bookings?type=${encodeURIComponent(type)}`)
cacheTag('bookings-data', data.id)
......@@ -125,7 +130,7 @@ export async function updateBookings() {
- **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`.
```tsx
cacheTag(['tag-one', 'tag-two'])
```
......@@ -18,20 +18,20 @@ module.exports = {
blog: {
stale: 3600, // 1 hour
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:
```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() {
"use cache"
'use cache'
cacheLife('blog')
const data = await fetch('/api/data')
return data
......@@ -42,8 +42,8 @@ export async function getCachedData() {
The configuration object has key values with the following format:
| **Property** | **Value** | **Description** | **Requirement** |
| --- | --- | --- | --- |
| `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 |
| `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
| **Property** | **Value** | **Description** | **Requirement** |
| ------------ | --------- | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
| `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 |
| `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` |
......@@ -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:
```ts filename="next.config.ts"
import type { NextConfig } from 'next';
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
dynamicIO: true,
}
};
export default nextConfig;
},
}
export default nextConfig
```
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
## 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.
\ No newline at end of file
- While `dynamicIO` can optimize performance by ensuring fresh data fetching during runtime, it may also introduce additional latency compared to serving pre-rendered content.
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