From 99eb0bfcaebbe3436b819aac82c7060881845fdd Mon Sep 17 00:00:00 2001 From: Barbara Honhoff Date: Fri, 22 Sep 2023 11:33:54 +0200 Subject: [PATCH 1/5] Revalidate '/communities' after new community is created --- .../src/app/[locale]/communities/page.tsx | 3 +++ .../src/app/[locale]/navigation.tsx | 10 ++++++++- .../src/app/[locale]/revalidate/page.tsx | 21 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 apps/researcher/src/app/[locale]/revalidate/page.tsx diff --git a/apps/researcher/src/app/[locale]/communities/page.tsx b/apps/researcher/src/app/[locale]/communities/page.tsx index bf45280cb..d0a5fe26e 100644 --- a/apps/researcher/src/app/[locale]/communities/page.tsx +++ b/apps/researcher/src/app/[locale]/communities/page.tsx @@ -5,6 +5,9 @@ import CommunityCard from './community-card'; import {ClientListStore} from '@colonial-collections/list-store'; import {Paginator, SearchField, OrderSelector} from 'ui/list'; +// 1 day = 60*60*24 = 86400 +export const revalidate = 86400; + interface Props { params: { locale: string; diff --git a/apps/researcher/src/app/[locale]/navigation.tsx b/apps/researcher/src/app/[locale]/navigation.tsx index cb8606086..82802b834 100644 --- a/apps/researcher/src/app/[locale]/navigation.tsx +++ b/apps/researcher/src/app/[locale]/navigation.tsx @@ -187,7 +187,15 @@ export default function Navigation({locales}: Props) { ); })} - + + `/revalidate/?path=/communities&redirect=/communities/${organization.slug}` + } + afterLeaveOrganizationUrl="/communities" + afterSelectOrganizationUrl={organization => + `/communities/${organization.slug}` + } + /> diff --git a/apps/researcher/src/app/[locale]/revalidate/page.tsx b/apps/researcher/src/app/[locale]/revalidate/page.tsx new file mode 100644 index 000000000..d186dac7e --- /dev/null +++ b/apps/researcher/src/app/[locale]/revalidate/page.tsx @@ -0,0 +1,21 @@ +import {redirect} from 'next/navigation'; +import {revalidatePath} from 'next/cache'; + +interface Props { + searchParams?: { + path?: string; + redirect?: string; + }; +} + +export default async function RevalidatePaths({searchParams = {}}: Props) { + if (searchParams.path) { + revalidatePath(searchParams.path); + } + + if (!searchParams.redirect) { + return redirect('/'); + } + + redirect(searchParams.redirect); +} From a1b50e6b74290827333d069fde09e03be7efc77c Mon Sep 17 00:00:00 2001 From: Barbara Honhoff Date: Fri, 22 Sep 2023 12:33:06 +0200 Subject: [PATCH 2/5] Try to find deleted communities, and revalidate lists --- .../src/app/[locale]/communities/[slug]/page.tsx | 4 ++++ .../src/app/[locale]/communities/community-card.tsx | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx index 139fdf3d8..6affb179e 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx @@ -6,6 +6,7 @@ import {JoinCommunityButton, EditCommunityButton} from './buttons'; import {getMemberships, getCommunityBySlug, isAdmin} from '@/lib/community'; import ErrorMessage from '@/components/error-message'; import {ClerkAPIResponseError} from '@clerk/shared'; +import {revalidatePath} from 'next/cache'; interface Props { params: { @@ -22,6 +23,9 @@ export default async function CommunityPage({params}: Props) { community = await getCommunityBySlug(params.slug); } catch (err) { if ((err as ClerkAPIResponseError).status === 404) { + // This could be a sign that the community has been deleted. + // In that case, we should revalidate the communities page. + revalidatePath('/communities'); return ; } return ; diff --git a/apps/researcher/src/app/[locale]/communities/community-card.tsx b/apps/researcher/src/app/[locale]/communities/community-card.tsx index 3d2ffe0de..6fc750c07 100644 --- a/apps/researcher/src/app/[locale]/communities/community-card.tsx +++ b/apps/researcher/src/app/[locale]/communities/community-card.tsx @@ -4,6 +4,7 @@ import {useTranslations} from 'next-intl'; import Link from 'next-intl/link'; import Image from 'next/image'; import {Suspense} from 'react'; +import {revalidatePath} from 'next/cache'; interface MembershipCountProps { communityId: string; @@ -12,7 +13,15 @@ interface MembershipCountProps { async function MembershipCount({communityId, locale}: MembershipCountProps) { const t = await getTranslator(locale, 'Communities'); - const memberships = await getMemberships(communityId); + + let memberships = []; + try { + memberships = await getMemberships(communityId); + } catch (error) { + // This could be a sign that the community has been deleted. + // In that case, we should revalidate the communities page. + revalidatePath('/communities'); + } return t.rich('membershipCount', { count: memberships.length, From 0e937ed3feef5384598d7a39aa273f2a60026074 Mon Sep 17 00:00:00 2001 From: Barbara Honhoff Date: Fri, 22 Sep 2023 12:42:20 +0200 Subject: [PATCH 3/5] Revalidate correct path --- .../researcher/src/app/[locale]/communities/[slug]/page.tsx | 6 +++--- .../src/app/[locale]/communities/community-card.tsx | 6 +++--- apps/researcher/src/app/[locale]/navigation.tsx | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx index 6affb179e..9b96d0d26 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx @@ -23,9 +23,9 @@ export default async function CommunityPage({params}: Props) { community = await getCommunityBySlug(params.slug); } catch (err) { if ((err as ClerkAPIResponseError).status === 404) { - // This could be a sign that the community has been deleted. - // In that case, we should revalidate the communities page. - revalidatePath('/communities'); + // This could be a sign of a deleted community in the cache. + // So, revalidate the communities page. + revalidatePath('/[locale]/communities'); return ; } return ; diff --git a/apps/researcher/src/app/[locale]/communities/community-card.tsx b/apps/researcher/src/app/[locale]/communities/community-card.tsx index 6fc750c07..73070582c 100644 --- a/apps/researcher/src/app/[locale]/communities/community-card.tsx +++ b/apps/researcher/src/app/[locale]/communities/community-card.tsx @@ -18,9 +18,9 @@ async function MembershipCount({communityId, locale}: MembershipCountProps) { try { memberships = await getMemberships(communityId); } catch (error) { - // This could be a sign that the community has been deleted. - // In that case, we should revalidate the communities page. - revalidatePath('/communities'); + // This could be a sign of a deleted community in the cache. + // So, revalidate the communities page. + revalidatePath('/[locale]/communities'); } return t.rich('membershipCount', { diff --git a/apps/researcher/src/app/[locale]/navigation.tsx b/apps/researcher/src/app/[locale]/navigation.tsx index 82802b834..5d73bb82a 100644 --- a/apps/researcher/src/app/[locale]/navigation.tsx +++ b/apps/researcher/src/app/[locale]/navigation.tsx @@ -189,7 +189,7 @@ export default function Navigation({locales}: Props) { - `/revalidate/?path=/communities&redirect=/communities/${organization.slug}` + `/revalidate/?path=/[locale]/communities&redirect=/communities/${organization.slug}` } afterLeaveOrganizationUrl="/communities" afterSelectOrganizationUrl={organization => From 764f298828c66242bb3c2e5f0317adbecf957b74 Mon Sep 17 00:00:00 2001 From: Barbara Honhoff Date: Fri, 22 Sep 2023 12:58:39 +0200 Subject: [PATCH 4/5] Add type 'page' to `revalidatePath` --- apps/researcher/src/app/[locale]/communities/[slug]/page.tsx | 2 +- apps/researcher/src/app/[locale]/communities/community-card.tsx | 2 +- apps/researcher/src/app/[locale]/revalidate/page.tsx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx index 9b96d0d26..7284ae092 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx @@ -25,7 +25,7 @@ export default async function CommunityPage({params}: Props) { if ((err as ClerkAPIResponseError).status === 404) { // This could be a sign of a deleted community in the cache. // So, revalidate the communities page. - revalidatePath('/[locale]/communities'); + revalidatePath('/[locale]/communities', 'page'); return ; } return ; diff --git a/apps/researcher/src/app/[locale]/communities/community-card.tsx b/apps/researcher/src/app/[locale]/communities/community-card.tsx index 73070582c..dae56e998 100644 --- a/apps/researcher/src/app/[locale]/communities/community-card.tsx +++ b/apps/researcher/src/app/[locale]/communities/community-card.tsx @@ -20,7 +20,7 @@ async function MembershipCount({communityId, locale}: MembershipCountProps) { } catch (error) { // This could be a sign of a deleted community in the cache. // So, revalidate the communities page. - revalidatePath('/[locale]/communities'); + revalidatePath('/[locale]/communities', 'page'); } return t.rich('membershipCount', { diff --git a/apps/researcher/src/app/[locale]/revalidate/page.tsx b/apps/researcher/src/app/[locale]/revalidate/page.tsx index d186dac7e..66e013a59 100644 --- a/apps/researcher/src/app/[locale]/revalidate/page.tsx +++ b/apps/researcher/src/app/[locale]/revalidate/page.tsx @@ -10,7 +10,7 @@ interface Props { export default async function RevalidatePaths({searchParams = {}}: Props) { if (searchParams.path) { - revalidatePath(searchParams.path); + revalidatePath(searchParams.path, 'page'); } if (!searchParams.redirect) { From 58472ae19a71b402c28f2139de7e9701d2cd6b57 Mon Sep 17 00:00:00 2001 From: Barbara Honhoff Date: Fri, 22 Sep 2023 14:14:01 +0200 Subject: [PATCH 5/5] Small changes to the revalidate community list logic --- .../src/app/[locale]/communities/[slug]/page.tsx | 3 ++- .../src/app/[locale]/communities/community-card.tsx | 12 ++++++++---- apps/researcher/src/app/[locale]/revalidate/page.tsx | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx index 7284ae092..a9c50e9ef 100644 --- a/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx +++ b/apps/researcher/src/app/[locale]/communities/[slug]/page.tsx @@ -22,7 +22,8 @@ export default async function CommunityPage({params}: Props) { try { community = await getCommunityBySlug(params.slug); } catch (err) { - if ((err as ClerkAPIResponseError).status === 404) { + const errorStatus = (err as ClerkAPIResponseError).status; + if (errorStatus === 404 || errorStatus === 410) { // This could be a sign of a deleted community in the cache. // So, revalidate the communities page. revalidatePath('/[locale]/communities', 'page'); diff --git a/apps/researcher/src/app/[locale]/communities/community-card.tsx b/apps/researcher/src/app/[locale]/communities/community-card.tsx index dae56e998..ea389d55f 100644 --- a/apps/researcher/src/app/[locale]/communities/community-card.tsx +++ b/apps/researcher/src/app/[locale]/communities/community-card.tsx @@ -5,6 +5,7 @@ import Link from 'next-intl/link'; import Image from 'next/image'; import {Suspense} from 'react'; import {revalidatePath} from 'next/cache'; +import {ClerkAPIResponseError} from '@clerk/shared'; interface MembershipCountProps { communityId: string; @@ -17,10 +18,13 @@ async function MembershipCount({communityId, locale}: MembershipCountProps) { let memberships = []; try { memberships = await getMemberships(communityId); - } catch (error) { - // This could be a sign of a deleted community in the cache. - // So, revalidate the communities page. - revalidatePath('/[locale]/communities', 'page'); + } catch (err) { + const errorStatus = (err as ClerkAPIResponseError).status; + if (errorStatus === 404 || errorStatus === 410) { + // This could be a sign of a deleted community in the cache. + // So, revalidate the communities page. + revalidatePath('/[locale]/communities', 'page'); + } } return t.rich('membershipCount', { diff --git a/apps/researcher/src/app/[locale]/revalidate/page.tsx b/apps/researcher/src/app/[locale]/revalidate/page.tsx index 66e013a59..d18baa17a 100644 --- a/apps/researcher/src/app/[locale]/revalidate/page.tsx +++ b/apps/researcher/src/app/[locale]/revalidate/page.tsx @@ -2,13 +2,13 @@ import {redirect} from 'next/navigation'; import {revalidatePath} from 'next/cache'; interface Props { - searchParams?: { + searchParams: { path?: string; redirect?: string; }; } -export default async function RevalidatePaths({searchParams = {}}: Props) { +export default function RevalidatePath({searchParams}: Props) { if (searchParams.path) { revalidatePath(searchParams.path, 'page'); }