Unverified Commit 3f2d2f69 authored by Shodipo Ayomide's avatar Shodipo Ayomide Committed by GitHub

correct expire calc & and Nested usage import in use-cache docs (#71899)

Co-authored-by: 's avatarDelba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
parent 3768aeaf
...@@ -230,15 +230,16 @@ When defining multiple caching behaviors, in the same route or component tree, i ...@@ -230,15 +230,16 @@ When defining multiple caching behaviors, in the same route or component tree, i
**Decision hierarchy for cache boundaries:** **Decision hierarchy for cache boundaries:**
1. Next.js will use the shortest cache profile found within the whole `use cache` boundary, excluding inner `use cache`directives. 1. Next.js will use the shortest cache profile found within the whole `use cache` boundary, excluding inner `use cache` directives.
2. If no cache profile exists then the shortest profile times from all inner `use cache` calls applies to this `use cache`. If there are no inner `use cache`'s then the default is used 2. If no cache profile exists then the shortest profile times from all inner `use cache` calls applies to this `use cache`. If there are no inner `use cache`'s then the default is used
3. Inner caches at two levels deep, do not affect the outer cache since they have already provided their duration to their parent. 3. Inner caches at two levels deep, do not affect the outer cache since they have already provided their duration to their parent.
For example, if you add the `use cache` directive to your page, without specifying a cache profile, the default cache profile will be applied implicitly (`cacheLife(”default”)`). If a component imported into the page also uses the `use cache` directive with its own cache profile, the outer and inner cache profiles are compared, and shortest duration set in the profiles will be applied. For example, if you add the `use cache` directive to your page, without specifying a cache profile, the default cache profile will be applied implicitly (`cacheLife(”default”)`). If a component imported into the page also uses the `use cache` directive with its own cache profile, the outer and inner cache profiles are compared, and shortest duration set in the profiles will be applied.
```tsx filename="app/components/parent.tsx" highlight={5,6,19,20} ```tsx filename="app/components/parent.tsx" highlight={5,6}
// Parent component // Parent component
import { unstable_cacheLife as cacheLife } from 'next/cache' import { unstable_cacheLife as cacheLife } from 'next/cache'
import { ChildComponent } from './child'
export async function ParentComponent() { export async function ParentComponent() {
'use cache' 'use cache'
...@@ -250,13 +251,18 @@ export async function ParentComponent() { ...@@ -250,13 +251,18 @@ export async function ParentComponent() {
</div> </div>
) )
} }
```
And in a separate file, we defined the Child component that was imported:
```tsx filename="app/components/child.tsx" highlight={4,5}
// Child component // Child component
import { unstable_cacheLife as cacheLife } from 'next/cache' import { unstable_cacheLife as cacheLife } from 'next/cache'
export async function ChildComponent() { export async function ChildComponent() {
'use cache' 'use cache'
cacheLife('hours') cacheLife('hours')
return <div>Child Content</div>
// This component's cache will respect the shorter 'hours' profile // This component's cache will respect the shorter 'hours' profile
} }
......
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