You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A handful of PRs to sync w/ next patch release
---------
Co-authored-by: Delba de Oliveira <[email protected]>
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: Hanzala Sohrab <[email protected]>
Copy file name to clipboardExpand all lines: docs/01-app/01-getting-started/05-server-and-client-components.mdx
+185Lines changed: 185 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -412,6 +412,191 @@ Your Server Component will now be able to directly render your provider, and all
412
412
413
413
> **Good to know**: You should render providers as deep as possible in the tree – notice how `ThemeProvider` only wraps `{children}` instead of the entire `<html>` document. This makes it easier for Next.js to optimize the static parts of your Server Components.
414
414
415
+
### Sharing data with context and React.cache
416
+
417
+
You can share fetched data across both Server and Client Components by combining [`React.cache`](https://react.dev/reference/react/cache) with context providers.
418
+
419
+
Create a cached function that fetches data:
420
+
421
+
```ts filename="app/lib/user.ts" switcher
422
+
import { cache } from'react'
423
+
424
+
exportconst getUser =cache(async () => {
425
+
const res =awaitfetch('https://api.example.com/user')
Server Components can also call `getUser()` directly:
577
+
578
+
```tsx filename="app/dashboard/page.tsx" switcher
579
+
import { getUser } from'../lib/user'
580
+
581
+
exportdefaultasyncfunction DashboardPage() {
582
+
const user =awaitgetUser() // Cached - same request, no duplicate fetch
583
+
return <h1>Dashboard for {user.name}</h1>
584
+
}
585
+
```
586
+
587
+
```jsx filename="app/dashboard/page.js" switcher
588
+
import { getUser } from'../lib/user'
589
+
590
+
exportdefaultasyncfunctionDashboardPage() {
591
+
constuser=awaitgetUser() // Cached - same request, no duplicate fetch
592
+
return<h1>Dashboard for {user.name}</h1>
593
+
}
594
+
```
595
+
596
+
Since `getUser` is wrapped with `React.cache`, multiple calls within the same request return the same memoized result, whether called directly in Server Components or resolved via context in Client Components.
597
+
598
+
> **Good to know**: `React.cache` is scoped to the current request only. Each request gets its own memoization scope with no sharing between requests.
599
+
415
600
### Third-party components
416
601
417
602
When using a third-party component that relies on client-only features, you can wrap it in a Client Component to ensure it works as expected.
Copy file name to clipboardExpand all lines: docs/01-app/01-getting-started/06-cache-components.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,6 +194,10 @@ Use [`connection()`](/docs/app/api-reference/functions/connection) if you need t
194
194
195
195
> **Good to know**: Runtime data cannot be cached with `use cache` because it requires request context. Components that access runtime APIs must always be wrapped in `<Suspense>`. However, you can extract values from runtime data and pass them as arguments to cached functions. See the [with runtime data](#with-runtime-data) section for an example.
196
196
197
+
One approach for reading runtime data like cookies without blocking the static shell is to pass a promise to a client context provider. See [Sharing data with context and React.cache](/docs/app/getting-started/server-and-client-components#sharing-data-with-context-and-reactcache) for an example.
198
+
199
+
> **Good to know:**`React.cache` operates in an isolated scope inside `use cache` boundaries. See [React.cache isolation](/docs/app/api-reference/directives/use-cache#reactcache-isolation) for more information.
200
+
197
201
### Non-deterministic operations
198
202
199
203
Operations like `Math.random()`, `Date.now()`, or `crypto.randomUUID()` produce different values each time they execute. To ensure these run at request time (generating unique values per request), Cache Components requires you to explicitly signal this intent by calling these operations after dynamic or runtime data access.
Copy file name to clipboardExpand all lines: docs/01-app/01-getting-started/08-updating-data.mdx
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: Updating Data
3
-
description: Learn how to mutate data using Server Functions.
3
+
description: Learn how to mutate data using Server Functions and Server Actions in Next.js.
4
4
related:
5
5
title: API Reference
6
6
description: Learn more about the features mentioned in this page by reading the API Reference.
@@ -18,6 +18,8 @@ A **Server Function** is an asynchronous function that runs on the server. They
18
18
19
19
In an `action` or mutation context, they are also called **Server Actions**.
20
20
21
+
> **Good to know:** A Server Action is a Server Function used in a specific way (for handling form submissions and mutations). Server Function is the broader term.
22
+
21
23
By convention, a Server Action is an async function used with [`startTransition`](https://react.dev/reference/react/startTransition). This happens automatically when the function is:
You can `get`, `set`, and `delete` cookies inside a Server Action using the [`cookies`](/docs/app/api-reference/functions/cookies) API.
429
431
430
-
When you [set or delete](/docs/app/api-reference/functions/cookies#understanding-cookie-behavior-in-server-actions) a cookie in a Server Action, Next.js re-renders the current page and its layouts on the server so the **UI reflects the new cookie value**.
432
+
When you [set or delete](/docs/app/api-reference/functions/cookies#understanding-cookie-behavior-in-server-functions) a cookie in a Server Action, Next.js re-renders the current page and its layouts on the server so the **UI reflects the new cookie value**.
431
433
432
434
> **Good to know**: The server update applies to the current React tree, re-rendering, mounting, or unmounting components, as needed. Client state is preserved for re-rendered components, and effects re-run if their dependencies changed.
|[`redirect`](#redirect-function)| Redirect user after a mutation or event | Server Components, Server Actions, Route Handlers | 307 (Temporary) or 303 (Server Action) |
20
-
|[`permanentRedirect`](#permanentredirect-function)| Redirect user after a mutation or event | Server Components, Server Actions, Route Handlers | 308 (Permanent) |
21
-
|[`useRouter`](#userouter-hook)| Perform a client-side navigation | Event Handlers in Client Components | N/A |
22
-
|[`redirects` in `next.config.js`](#redirects-in-nextconfigjs)| Redirect an incoming request based on a path |`next.config.js` file | 307 (Temporary) or 308 (Permanent) |
23
-
|[`NextResponse.redirect`](#nextresponseredirect-in-proxy)| Redirect an incoming request based on a condition | Proxy | Any |
|[`redirect`](#redirect-function)| Redirect user after a mutation or event | Server Components, Server Functions, Route Handlers | 307 (Temporary) or 303 (Server Action) |
20
+
|[`permanentRedirect`](#permanentredirect-function)| Redirect user after a mutation or event | Server Components, Server Functions, Route Handlers | 308 (Permanent) |
21
+
|[`useRouter`](#userouter-hook)| Perform a client-side navigation | Event Handlers in Client Components | N/A |
22
+
|[`redirects` in `next.config.js`](#redirects-in-nextconfigjs)| Redirect an incoming request based on a path |`next.config.js` file | 307 (Temporary) or 308 (Permanent) |
23
+
|[`NextResponse.redirect`](#nextresponseredirect-in-proxy)| Redirect an incoming request based on a condition | Proxy | Any |
24
24
25
25
</AppOnly>
26
26
@@ -38,7 +38,7 @@ There are a few ways you can handle redirects in Next.js. This page will go thro
38
38
39
39
## `redirect` function
40
40
41
-
The `redirect` function allows you to redirect the user to another URL. You can call `redirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Actions](/docs/app/getting-started/updating-data).
41
+
The `redirect` function allows you to redirect the user to another URL. You can call `redirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Functions](/docs/app/getting-started/updating-data).
42
42
43
43
`redirect` is often used after a mutation or event. For example, creating a post:
44
44
@@ -90,7 +90,7 @@ See the [`redirect` API reference](/docs/app/api-reference/functions/redirect) f
90
90
91
91
## `permanentRedirect` function
92
92
93
-
The `permanentRedirect` function allows you to **permanently** redirect the user to another URL. You can call `permanentRedirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Actions](/docs/app/getting-started/updating-data).
93
+
The `permanentRedirect` function allows you to **permanently** redirect the user to another URL. You can call `permanentRedirect` in [Server Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Functions](/docs/app/getting-started/updating-data).
94
94
95
95
`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:
Copy file name to clipboardExpand all lines: docs/01-app/02-guides/scripts.mdx
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -127,7 +127,7 @@ Refer to the [`next/script`](/docs/app/api-reference/components/script#strategy)
127
127
128
128
> **Warning:** The `worker` strategy is not yet stable and does not yet work with the App Router. Use with caution.
129
129
130
-
Scripts that use the `worker` strategy are offloaded and executed in a web worker with [Partytown](https://partytown.builder.io/). This can improve the performance of your site by dedicating the main thread to the rest of your application code.
130
+
Scripts that use the `worker` strategy are offloaded and executed in a web worker with [Partytown](https://partytown.qwik.dev/). This can improve the performance of your site by dedicating the main thread to the rest of your application code.
131
131
132
132
This strategy is still experimental and can only be used if the `nextScriptWorkers` flag is enabled in `next.config.js`:
133
133
@@ -145,7 +145,7 @@ Then, run `next` (normally `npm run dev` or `yarn dev`) and Next.js will guide y
145
145
npm run dev
146
146
```
147
147
148
-
You'll see instructions like these: Please install Partytown by running `npm install @builder.io/partytown`
148
+
You'll see instructions like these: Please install Partytown by running `npm install @qwik.dev/partytown`
149
149
150
150
Once setup is complete, defining `strategy="worker"` will automatically instantiate Partytown in your application and offload the script to a web worker.
151
151
@@ -173,7 +173,7 @@ export default function Home() {
173
173
}
174
174
```
175
175
176
-
There are a number of trade-offs that need to be considered when loading a third-party script in a web worker. Please see Partytown's [tradeoffs](https://partytown.builder.io/trade-offs) documentation for more information.
176
+
There are a number of trade-offs that need to be considered when loading a third-party script in a web worker. Please see Partytown's [tradeoffs](https://partytown.qwik.dev/trade-offs) documentation for more information.
177
177
178
178
<PagesOnly>
179
179
@@ -218,7 +218,7 @@ In order to modify Partytown's configuration, the following conditions must be m
218
218
219
219
> **Note**: If you are using an [asset prefix](/docs/pages/api-reference/config/next-config-js/assetPrefix) and would like to modify Partytown's default configuration, you must include it as part of the `lib` path.
220
220
221
-
Take a look at Partytown's [configuration options](https://partytown.builder.io/configuration) to see the full list of other properties that can be added.
221
+
Take a look at Partytown's [configuration options](https://partytown.qwik.dev/configuration) to see the full list of other properties that can be added.
Cached functions execute in an isolated environment. The following constraints ensure cache behavior remains predictable and secure.
193
+
194
+
### Runtime APIs
195
+
192
196
Cached functions and components **cannot** directly access runtime APIs like `cookies()`, `headers()`, or `searchParams`. Instead, read these values outside the cached scope and pass them as arguments.
193
197
194
198
### Runtime caching considerations
@@ -206,6 +210,34 @@ If the default in-memory cache isn't enough, consider **[`use cache: remote`](/d
206
210
207
211
Very rarely, for compliance requirements or when you can't refactor your code to pass runtime data as arguments to a `use cache` scope, you might need [`use cache: private`](/docs/app/api-reference/directives/use-cache-private).
208
212
213
+
### React.cache isolation
214
+
215
+
[`React.cache`](https://react.dev/reference/react/cache) operates in an isolated scope inside `use cache` boundaries. Values stored via `React.cache` outside a `use cache` function are not visible inside it.
216
+
217
+
This means you cannot use `React.cache` to pass data into a `use cache` scope:
218
+
219
+
```tsx
220
+
import { cache } from'react'
221
+
222
+
const store =cache(() => ({ current: nullasstring|null }))
223
+
224
+
function Parent() {
225
+
const shared =store()
226
+
shared.current='value from parent'
227
+
return <Child />
228
+
}
229
+
230
+
asyncfunction Child() {
231
+
'use cache'
232
+
const shared =store()
233
+
// shared.current is null, not 'value from parent'
234
+
// use cache has its own isolated React.cache scope
235
+
return <div>{shared.current}</div>
236
+
}
237
+
```
238
+
239
+
This isolation ensures cached functions have predictable, self-contained behavior. To pass data into a `use cache` scope, use function arguments instead.
240
+
209
241
## `use cache` at runtime
210
242
211
243
On the **server**, cache entries are stored in-memory and respect the `revalidate` and `expire` times from your `cacheLife` configuration. You can customize the cache storage by configuring [`cacheHandlers`](/docs/app/api-reference/config/next-config-js/cacheHandlers) in your `next.config.js` file.
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/04-functions/after.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: API Reference for the after function.
5
5
6
6
`after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics.
7
7
8
-
It can be used in [Server Components](/docs/app/getting-started/server-and-client-components) (including [`generateMetadata`](/docs/app/api-reference/functions/generate-metadata)), [Server Actions](/docs/app/getting-started/updating-data), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Proxy](/docs/app/api-reference/file-conventions/proxy).
8
+
It can be used in [Server Components](/docs/app/getting-started/server-and-client-components) (including [`generateMetadata`](/docs/app/api-reference/functions/generate-metadata)), [Server Functions](/docs/app/getting-started/updating-data), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Proxy](/docs/app/api-reference/file-conventions/proxy).
9
9
10
10
The function accepts a callback that will be executed after the response (or prerender) is finished:
11
11
@@ -59,7 +59,7 @@ export default function Layout({ children }) {
59
59
60
60
### With request APIs
61
61
62
-
You can use request APIs such as [`cookies`](/docs/app/api-reference/functions/cookies) and [`headers`](/docs/app/api-reference/functions/headers) inside `after` in [Server Actions](/docs/app/getting-started/updating-data) and [Route Handlers](/docs/app/api-reference/file-conventions/route). This is useful for logging activity after a mutation. For example:
62
+
You can use request APIs such as [`cookies`](/docs/app/api-reference/functions/cookies) and [`headers`](/docs/app/api-reference/functions/headers) inside `after` in [Server Functions](/docs/app/getting-started/updating-data) and [Route Handlers](/docs/app/api-reference/file-conventions/route). This is useful for logging activity after a mutation. For example:
0 commit comments