An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(cache): move revalidateTag to Route Handler context
Yoshiki Miurac1f24780138f
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex 785c4909..ed9cd374 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -1,4 +1,5 @@import { cookies } from 'next/headers'+import { revalidateTag } from 'next/cache'import { getCurrentUserId } from '@/lib/auth/get-current-user'import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'@@ -103,6 +104,13 @@ export async function POST(req: Request) {searchMode})+ // Invalidate the cache for this specific chat after creating the response+ // This ensures the next load will get fresh data+ if (chatId) {+ revalidateTag(`chat-${chatId}`)+ revalidateTag('chat')+ }+const totalTime = performance.now() - startTimeperfLog(`Total API route time: ${totalTime.toFixed(2)}ms`)perfLog(`=== Summary ===`)diff --git a/lib/streaming/helpers/persist-stream-results.ts b/lib/streaming/helpers/persist-stream-results.tsindex d72f8a32..392b31c9 100644--- a/lib/streaming/helpers/persist-stream-results.ts+++ b/lib/streaming/helpers/persist-stream-results.ts@@ -1,4 +1,3 @@-import { revalidateTag } from 'next/cache'import { UIMessage } from 'ai'import { upsertMessage } from '@/lib/actions/chat'@@ -34,8 +33,6 @@ export async function persistStreamResults(try {await upsertMessage(chatId, responseMessage, userId)perfTime('upsertMessage (AI response) completed', saveStart)- // Invalidate cache after saving message- revalidateTag(`chat-${chatId}`)} catch (error) {console.error('Error saving message:', error)try {@@ -44,8 +41,6 @@ export async function persistStreamResults('save message')perfTime('upsertMessage (AI response) completed after retry', saveStart)- // Invalidate cache after saving message- revalidateTag(`chat-${chatId}`)} catch (retryError) {console.error('Failed to save after retries:', retryError)// Don't throw here to avoid breaking the stream@@ -56,8 +51,6 @@ export async function persistStreamResults(if (chatTitle && chatTitle !== DEFAULT_CHAT_TITLE) {try {await updateChatTitle(chatId, chatTitle, userId)- // Invalidate cache after updating title- revalidateTag(`chat-${chatId}`)} catch (error) {console.error('Error updating title:', error)// Don't throw here as title update is not critical