Skip to content

チャット時のキャッシュとページ遷移を修正#202

Merged
na-trium-144 merged 4 commits intomainfrom
fix-chat-revalidation
Mar 20, 2026
Merged

チャット時のキャッシュとページ遷移を修正#202
na-trium-144 merged 4 commits intomainfrom
fix-chat-revalidation

Conversation

@na-trium-144
Copy link
Contributor

  • nextjsのキャッシュのrevalidateはRouteHandlerではなくServerActionから呼ばないと正しく動作しないらしい。 Using revalidateTag() inside dynamic route handler is unstable vercel/next.js#69064
    そのためlib/以下の関数では直接revalidateChatを呼ばず、ServerActionの関数から呼ぶようにする。
  • 特にdev環境でチャット作成時のページ遷移が遅いので、完了するまで後続の処理を待機する。

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 20, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
my-code aae1407 Commit Preview URL

Branch Preview URL
Mar 20 2026, 10:26 AM

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Next.js のキャッシュ再検証(revalidateTag)が Route Handler 内では期待通り動かない問題に対応するため、チャット関連のキャッシュ再検証呼び出しを Server Action 経由に寄せ、あわせてチャット作成時のページ遷移完了まで待機するようにして体感の遅さを改善するPRです。

Changes:

  • app/lib/chatHistory.ts 内から revalidateChat 呼び出しを外し、Server Action 側から呼ぶ設計へ移行
  • revalidateChatAction(Server Action)を新設し、クライアントからキャッシュ再検証を実行可能に
  • チャット作成時の router.push を「遷移完了まで await」できるようにして、後続処理を待機

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
app/lib/chatHistory.ts lib内部の revalidate 呼び出しを廃止し、再検証関数を外部から呼べるように調整
app/actions/revalidateChat.ts chat のキャッシュ再検証を行う Server Action を追加
app/actions/deleteChat.ts delete 後の再検証を Server Action 側に移動
app/(docs)/@docs/[lang]/[pageId]/chatForm.tsx ストリーミング中に Server Action で再検証し、push完了まで待機するよう変更
Comments suppressed due to low confidence (1)

app/lib/chatHistory.ts:43

  • revalidateChat accepts pagePath as string | PagePath, but when given a string it blindly splits on / without validating the result. Since callers can pass unexpected values (e.g. empty string), this can silently generate invalid cache keys (e.g. containing undefined). Consider validating the string form (or requiring PagePath only) and throwing on invalid input to avoid revalidating the wrong tags.
export async function revalidateChat(
  chatId: string,
  userId: string,
  pagePath: string | PagePath
) {
  if (typeof pagePath === "string") {
    const [lang, page] = pagePath.split("/") as [LangId, PageSlug];
    pagePath = { lang, page };
  }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
na-trium-144 and others added 2 commits March 20, 2026 19:21
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@na-trium-144 na-trium-144 force-pushed the fix-chat-revalidation branch from 1d16cee to aae1407 Compare March 20, 2026 10:23
@na-trium-144 na-trium-144 merged commit 13c9538 into main Mar 20, 2026
8 checks passed
@na-trium-144 na-trium-144 deleted the fix-chat-revalidation branch March 20, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants