From 4c3de95d539e8a20d681643381dcc0dd1662ce35 Mon Sep 17 00:00:00 2001 From: belong112 Date: Tue, 26 Nov 2024 23:52:57 +0800 Subject: [PATCH] fix(ProfilePage): redirect to new user profile URL after user changing slug --- components/ProfilePage/ProfilePage.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/components/ProfilePage/ProfilePage.js b/components/ProfilePage/ProfilePage.js index 4f1c4b52..c045a385 100644 --- a/components/ProfilePage/ProfilePage.js +++ b/components/ProfilePage/ProfilePage.js @@ -88,13 +88,16 @@ function ProfilePage({ id, slug }) { // const router = useRouter(); const latestSlug = data?.GetUser?.slug; // slug may update after user edits + const userId = currentUser?.id; useEffect(() => { - if (!latestSlug) return; - const targetPath = `/user/${encodeURI(latestSlug)}`; - if (window.location.pathname !== targetPath) { + if (latestSlug === undefined) return; + const targetPath = latestSlug + ? `/user/${encodeURI(latestSlug)}` + : `/user?id=${userId}`; + if (router.asPath !== targetPath) { router.replace(targetPath); } - }, [latestSlug, router]); + }, [latestSlug, userId, router]); if (loading) { return (