Back to Next Js

refresh

docs/01-app/03-api-reference/04-functions/refresh.mdx

16.2.51.3 KB
Original Source

refresh allows you to refresh the client router from within a Server Action.

Usage

refresh can only be called from within Server Actions. It cannot be used in Route Handlers, Client Components, or any other context.

Parameters

tsx
refresh(): void;

Returns

refresh does not return a value.

Examples

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()
}
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

ts
import { refresh } from 'next/cache'

export async function POST() {
  // This will throw an error
  refresh()
}