refresh
refresh reruns dynamic work for the current route from a Server Action without invalidating server-side cached data.
Usage
refresh can only be called from Server Actions. Use router.refresh() in Client Components. It cannot be used in Route Handlers or other contexts.
Client cache behavior
Calling refresh clears the pages kept for back/forward navigation, but it does not invalidate other prefetched routes. A route prefetched before the Server Action can be reused until its stale time expires.
If a mutation changes cached data used by other routes, call updateTag or revalidatePath from the Server Action. These functions invalidate server-side cached data and currently clear prefetched routes from the client cache.
Parameters
refresh(): void;Returns
refresh does not return a value.
Examples
app/actions.ts
'use server'
import { refresh } from 'next/cache'
export async function createPost(formData: FormData) {
const title = formData.get('title')
const content = formData.get('content')
// Create the post in your database
const post = await db.post.create({
data: { title, content },
})
refresh()
}app/actions.js
'use server'
import { refresh } from 'next/cache'
export async function createPost(formData) {
const title = formData.get('title')
const content = formData.get('content')
// Create the post in your database
const post = await db.post.create({
data: { title, content },
})
refresh()
}Error when used outside Server Actions
app/api/posts/route.ts
import { refresh } from 'next/cache'
export async function POST() {
// This will throw an error
refresh()
}