refresh
refresh は、Server Action 内からクライアントルーターをリフレッシュできます。
使用方法
refresh は Server Actions 内から **のみ** 呼び出すことができます。Route Handlers、Client Components、その他のコンテキストでは使用できません。
Parameters
refresh(): void;戻り値
refresh は値を返しません。
例
app/actions.ts
'use server'
import { refresh } from 'next/cache'
import { redirect } from 'next/navigation'
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()
}Server Actions の外部で使用した場合のエラー
app/api/posts/route.ts
import { refresh } from 'next/cache'
export async function POST() {
// This will throw an error
refresh()
}役に立ちましたか?