Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/accountMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { authClient } from "@/lib/auth-client";
import { usePathname } from "next/navigation";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";

export function AutoAnonymousLogin() {
Expand All @@ -18,6 +18,7 @@
export function AccountMenu() {
const { data: session, isPending } = authClient.useSession();
const pathname = usePathname();
const router = useRouter();

const signout = () => {
if (
Expand All @@ -27,7 +28,7 @@
) {
authClient.signOut({
fetchOptions: {
onSuccess: () => window.location.reload(),
onSuccess: () => router.refresh(),
},
});
}
Expand All @@ -36,7 +37,7 @@
if (window.confirm("チャット履歴は削除され、アクセスできなくなります。")) {
authClient.signOut({
fetchOptions: {
onSuccess: () => window.location.reload(),
onSuccess: () => router.refresh(),
},
});
}
Expand All @@ -51,7 +52,7 @@
<div className="dropdown dropdown-end">
<label tabIndex={0} className="btn btn-ghost btn-circle avatar">
<div className="w-8 h-8 rounded-full">
<img

Check warning on line 55 in app/accountMenu.tsx

View workflow job for this annotation

GitHub Actions / lint (22.x)

Using `<img>` could result in slower LCP and higher bandwidth. Consider using `<Image />` from `next/image` or a custom image loader to automatically optimize images. This may incur additional usage or cost from your provider. See: https://nextjs.org/docs/messages/no-img-element
src={
session.user?.image ??
`https://avatar.vercel.sh/${session.user?.name}`
Expand Down Expand Up @@ -102,7 +103,7 @@
onClick={() =>
authClient.signIn.social({
provider: "github",
callbackURL: pathname,
callbackURL: `/clear-cache?redirect=${encodeURIComponent(pathname)}`,
})
}
>
Expand All @@ -114,7 +115,7 @@
onClick={() =>
authClient.signIn.social({
provider: "google",
callbackURL: pathname,
callbackURL: `/clear-cache?redirect=${encodeURIComponent(pathname)}`,
})
}
>
Expand Down
29 changes: 29 additions & 0 deletions app/actions/clearUserCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server";

import { initContext, cacheKeyForPage } from "@/lib/chatHistory";
import { updateTag } from "next/cache";
import { getPagesList } from "@/lib/docs";
import { isCloudflare } from "@/lib/detectCloudflare";

export async function clearUserCacheAction() {
const ctx = await initContext();
if (!ctx.userId) return;

const pagesList = await getPagesList();
for (const lang of pagesList) {
for (const page of lang.pages) {
updateTag(cacheKeyForPage({ lang: lang.id, page: page.slug }, ctx.userId));
}
}

if (isCloudflare()) {
const cache = await caches.open("chatHistory");
for (const lang of pagesList) {
for (const page of lang.pages) {
await cache.delete(
cacheKeyForPage({ lang: lang.id, page: page.slug }, ctx.userId)
);
}
}
}
}
31 changes: 31 additions & 0 deletions app/clear-cache/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import { clearUserCacheAction } from "@/actions/clearUserCache";
import { useRouter, useSearchParams } from "next/navigation";
import { Suspense, useEffect } from "react";

function ClearCacheContent() {
const router = useRouter();
const searchParams = useSearchParams();

useEffect(() => {
const redirectParam = searchParams.get("redirect") ?? "/";
// Only allow relative redirects to prevent open redirect attacks
const redirect = redirectParam.startsWith("/") ? redirectParam : "/";
clearUserCacheAction()
.catch(() => {})
.finally(() => {
router.replace(redirect);
});
}, [router, searchParams]);

return null;
}

export default function ClearCachePage() {
return (
<Suspense>
<ClearCacheContent />
</Suspense>
);
}
20 changes: 1 addition & 19 deletions app/lib/chatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { and, asc, eq, exists } from "drizzle-orm";
import { Auth } from "better-auth";
import { updateTag } from "next/cache";
import { isCloudflare } from "./detectCloudflare";
import { getPagesList, LangId, PagePath, PageSlug, SectionId } from "./docs";
import { LangId, PagePath, PageSlug, SectionId } from "./docs";

export interface CreateChatMessage {
role: "user" | "ai" | "error";
Expand Down Expand Up @@ -279,22 +279,4 @@ export async function migrateChatUser(oldUserId: string, newUserId: string) {
.update(chat)
.set({ userId: newUserId })
.where(eq(chat.userId, oldUserId));
const pagesList = await getPagesList();
for (const lang of pagesList) {
for (const page of lang.pages) {
updateTag(
cacheKeyForPage({ lang: lang.id, page: page.slug }, newUserId)
);
}
}
if (isCloudflare()) {
const cache = await caches.open("chatHistory");
for (const lang of pagesList) {
for (const page of lang.pages) {
await cache.delete(
cacheKeyForPage({ lang: lang.id, page: page.slug }, newUserId)
);
}
}
}
}
Loading