@@ -9,6 +9,7 @@ import { Auth } from "better-auth";
99import { revalidateTag , unstable_cacheLife } from "next/cache" ;
1010import { isCloudflare } from "./detectCloudflare" ;
1111import { unstable_cacheTag } from "next/cache" ;
12+ import { PagePath , SectionId } from "./docs" ;
1213
1314export interface CreateChatMessage {
1415 role : "user" | "ai" | "error" ;
@@ -17,6 +18,9 @@ export interface CreateChatMessage {
1718
1819// cacheに使うキーで、実際のURLではない
1920const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory" ;
21+ function cacheKeyForPage ( path : PagePath , userId : string ) {
22+ return `${ CACHE_KEY_BASE } /getChat?path=${ path . lang } /${ path . page } &userId=${ userId } ` ;
23+ }
2024
2125interface Context {
2226 drizzle : Awaited < ReturnType < typeof getDrizzle > > ;
@@ -50,7 +54,7 @@ export async function initContext(ctx?: Partial<Context>): Promise<Context> {
5054}
5155
5256export async function addChat (
53- sectionId : string ,
57+ sectionId : SectionId ,
5458 messages : CreateChatMessage [ ] ,
5559 context ?: Partial < Context >
5660) {
@@ -77,15 +81,13 @@ export async function addChat(
7781 )
7882 . returning ( ) ;
7983
80- revalidateTag ( ` ${ CACHE_KEY_BASE } /getChat?docsId= ${ docsId } & userId= ${ userId } ` ) ;
84+ revalidateTag ( cacheKeyForPage ( { } , userId ) ) ;
8185 if ( isCloudflare ( ) ) {
8286 const cache = await caches . open ( "chatHistory" ) ;
8387 console . log (
84- `deleting cache for chatHistory/getChat for user ${ userId } and docs ${ docsId } `
85- ) ;
86- await cache . delete (
87- `${ CACHE_KEY_BASE } /getChat?docsId=${ docsId } &userId=${ userId } `
88+ `deleting cache for chatHistory/getChat for user ${ userId } and docs ${ lang } /${ page } `
8889 ) ;
90+ await cache . delete ( cacheKeyForPage ( { } , userId ) ) ;
8991 }
9092
9193 return {
@@ -97,7 +99,7 @@ export async function addChat(
9799export type ChatWithMessages = Awaited < ReturnType < typeof addChat > > ;
98100
99101export async function getChat (
100- docsId : string ,
102+ path : PagePath ,
101103 context ?: Partial < Context >
102104) : Promise < ChatWithMessages [ ] > {
103105 const { drizzle, userId } = await initContext ( context ) ;
@@ -118,35 +120,30 @@ export async function getChat(
118120 if ( isCloudflare ( ) ) {
119121 const cache = await caches . open ( "chatHistory" ) ;
120122 await cache . put (
121- ` ${ CACHE_KEY_BASE } /getChat?docsId= ${ docsId } & userId= ${ userId } ` ,
123+ cacheKeyForPage ( path , userId ) ,
122124 new Response ( JSON . stringify ( chats ) , {
123125 headers : { "Cache-Control" : "max-age=86400, s-maxage=86400" } ,
124126 } )
125127 ) ;
126128 }
127129 return chats ;
128130}
129- export async function getChatFromCache ( docsId : string , context : Context ) {
131+ export async function getChatFromCache ( path : PagePath , context : Context ) {
130132 "use cache" ;
131133 unstable_cacheLife ( "days" ) ;
132134
133135 // cacheされる関数の中でheader()にはアクセスできない。
134136 // なので外でinitContext()を呼んだものを引数に渡す必要がある。
135137 // しかし、drizzleオブジェクトは外から渡せないのでgetChatの中で改めてinitContext()を呼んでdrizzleだけ再初期化している
136138 const { auth, userId } = context ;
137- unstable_cacheTag (
138- `${ CACHE_KEY_BASE } /getChat?docsId=${ docsId } &userId=${ userId } `
139- ) ;
140-
141139 if ( ! userId ) {
142140 return [ ] ;
143141 }
142+ unstable_cacheTag ( cacheKeyForPage ( path , userId ) ) ;
144143
145144 if ( isCloudflare ( ) ) {
146145 const cache = await caches . open ( "chatHistory" ) ;
147- const cachedResponse = await cache . match (
148- `${ CACHE_KEY_BASE } /getChat?docsId=${ docsId } &userId=${ userId } `
149- ) ;
146+ const cachedResponse = await cache . match ( cacheKeyForPage ( path , userId ) ) ;
150147 if ( cachedResponse ) {
151148 console . log ( "Cache hit for chatHistory/getChat" ) ;
152149 const data = ( await cachedResponse . json ( ) ) as ChatWithMessages [ ] ;
@@ -155,7 +152,7 @@ export async function getChatFromCache(docsId: string, context: Context) {
155152 console . log ( "Cache miss for chatHistory/getChat" ) ;
156153 }
157154 }
158- return await getChat ( docsId , { auth, userId } ) ;
155+ return await getChat ( path , { auth, userId } ) ;
159156}
160157
161158export async function migrateChatUser ( oldUserId : string , newUserId : string ) {
0 commit comments