@@ -24,6 +24,27 @@ const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory";
2424export function cacheKeyForPage ( path : PagePath , userId : string ) {
2525 return `${ CACHE_KEY_BASE } /getChat?path=${ path . lang } /${ path . page } &userId=${ userId } ` ;
2626}
27+ export function cacheKeyForChat ( chatId : string ) {
28+ return `${ CACHE_KEY_BASE } /getChatOne?chatId=${ chatId } ` ;
29+ }
30+
31+ async function revalidateChat (
32+ chatId : string ,
33+ userId : string ,
34+ pagePath : string | PagePath
35+ ) {
36+ if ( typeof pagePath === "string" ) {
37+ const [ lang , page ] = pagePath . split ( "/" ) as [ LangId , PageSlug ] ;
38+ pagePath = { lang, page } ;
39+ }
40+ revalidateTag ( cacheKeyForChat ( chatId ) ) ;
41+ revalidateTag ( cacheKeyForPage ( pagePath , userId ) ) ;
42+ if ( isCloudflare ( ) ) {
43+ const cache = await caches . open ( "chatHistory" ) ;
44+ await cache . delete ( cacheKeyForChat ( chatId ) ) ;
45+ await cache . delete ( cacheKeyForPage ( pagePath , userId ) ) ;
46+ }
47+ }
2748
2849interface Context {
2950 drizzle : Awaited < ReturnType < typeof getDrizzle > > ;
@@ -105,14 +126,7 @@ export async function addChat(
105126 chatDiffs = [ ] as never [ ] ;
106127 }
107128
108- revalidateTag ( cacheKeyForPage ( path , userId ) ) ;
109- if ( isCloudflare ( ) ) {
110- const cache = await caches . open ( "chatHistory" ) ;
111- console . log (
112- `deleting cache for chatHistory/getChat for user ${ userId } and docs ${ path . lang } /${ path . page } `
113- ) ;
114- await cache . delete ( cacheKeyForPage ( path , userId ) ) ;
115- }
129+ await revalidateChat ( newChat . chatId , userId , path ) ;
116130
117131 return {
118132 ...newChat ,
@@ -145,18 +159,7 @@ export async function deleteChat(chatId: string, context: Context) {
145159 const targetSection = await drizzle . query . section . findFirst ( {
146160 where : eq ( section . sectionId , deletedChat [ 0 ] . sectionId ) ,
147161 } ) ;
148- const [ lang , page ] = ( targetSection ?. pagePath . split ( "/" ) ?? [ ] ) as [
149- LangId ,
150- PageSlug ,
151- ] ;
152- revalidateTag ( cacheKeyForPage ( { lang, page } , userId ) ) ;
153- if ( isCloudflare ( ) ) {
154- const cache = await caches . open ( "chatHistory" ) ;
155- console . log (
156- `deleting cache for chatHistory/getChat for user ${ userId } and docs ${ lang } /${ page } `
157- ) ;
158- await cache . delete ( cacheKeyForPage ( { lang, page } , userId ) ) ;
159- }
162+ await revalidateChat ( chatId , userId , targetSection ?. pagePath ?? "" ) ;
160163}
161164
162165export async function getAllChat (
0 commit comments