diff --git a/actions/getIdentityData.ts b/actions/getIdentityData.ts index e6275e5c..b6a68efb 100644 --- a/actions/getIdentityData.ts +++ b/actions/getIdentityData.ts @@ -6,15 +6,16 @@ import { unstable_cache } from "next/cache"; import { deduplicateByUri } from "src/utils/deduplicateRecords"; import { getAuthToken } from "src/auth"; +const getCachedIdentityData = unstable_cache( + (auth_token: string) => fetchIdentityData(auth_token), + ["identity-data"], + { revalidate: 30 }, +); + export const getIdentityData = cache(async () => { let auth_token = await getAuthToken(); if (!auth_token) return null; - - return unstable_cache( - () => fetchIdentityData(auth_token), - [`identity-${auth_token}`], - { revalidate: 30 }, - )(); + return getCachedIdentityData(auth_token); }); export async function uncachedGetIdentityData() { diff --git a/app/[leaflet_id]/page.tsx b/app/[leaflet_id]/page.tsx index 8bce6f2d..64fbbb3f 100644 --- a/app/[leaflet_id]/page.tsx +++ b/app/[leaflet_id]/page.tsx @@ -18,33 +18,35 @@ import { FontLoader, extractFontsFromFacts } from "components/FontLoader"; type LeafletData = NonNullable; +const fetchLeafletPageData = unstable_cache( + async (token_id: string) => { + let { data, error } = await supabaseServerClient.rpc( + "get_leaflet_page_data", + { p_token_id: token_id }, + ); + if (!data || error) return { data: null, error }; + + let row = Array.isArray(data) ? data[0] : data; + if (!row) return { data: null, error: null }; + + let leafletData = { + ...(row.permission_token as Record), + permission_token_rights: row.permission_token_rights || [], + leaflets_in_publications: row.leaflets_in_publications || [], + leaflets_to_documents: row.leaflets_to_documents || [], + custom_domain_routes: row.custom_domain_routes || [], + } as LeafletData; + + let facts = (row.facts || []) as unknown as Fact[]; + + return { data: leafletData, facts, error: null }; + }, + ["leaflet-page-data"], + { revalidate: 30 }, +); + const getCachedLeafletPageData = cache((token_id: string) => - unstable_cache( - async () => { - let { data, error } = await supabaseServerClient.rpc( - "get_leaflet_page_data", - { p_token_id: token_id }, - ); - if (!data || error) return { data: null, error }; - - let row = Array.isArray(data) ? data[0] : data; - if (!row) return { data: null, error: null }; - - let leafletData = { - ...(row.permission_token as Record), - permission_token_rights: row.permission_token_rights || [], - leaflets_in_publications: row.leaflets_in_publications || [], - leaflets_to_documents: row.leaflets_to_documents || [], - custom_domain_routes: row.custom_domain_routes || [], - } as LeafletData; - - let facts = (row.facts || []) as unknown as Fact[]; - - return { data: leafletData, facts, error: null }; - }, - [`leaflet-page-data-${token_id}`], - { revalidate: 30 }, - )(), + fetchLeafletPageData(token_id), ); export const preferredRegion = ["sfo1"]; diff --git a/src/utils/getPublicationMetadataFromLeafletData.ts b/src/utils/getPublicationMetadataFromLeafletData.ts index e0220520..8c42bbdb 100644 --- a/src/utils/getPublicationMetadataFromLeafletData.ts +++ b/src/utils/getPublicationMetadataFromLeafletData.ts @@ -36,18 +36,10 @@ export function getPublicationMetadataFromLeafletData( let pubData: | NonNullable | undefined - | null = - data?.leaflets_in_publications?.[0] || - data?.permission_token_rights[0].entity_sets?.permission_tokens?.find( - (p) => p.leaflets_in_publications?.length, - )?.leaflets_in_publications?.[0]; + | null = data?.leaflets_in_publications?.[0]; // If not found, check for standalone documents - let standaloneDoc = - data?.leaflets_to_documents?.[0] || - data?.permission_token_rights[0].entity_sets?.permission_tokens.find( - (p) => p.leaflets_to_documents?.length, - )?.leaflets_to_documents?.[0]; + let standaloneDoc = data?.leaflets_to_documents?.[0]; if (!pubData && standaloneDoc) { // Transform standalone document data to match the expected format pubData = {