diff options
| author | trisusilo <tri.susilo@altama.co.id> | 2024-10-08 07:58:27 +0000 |
|---|---|---|
| committer | trisusilo <tri.susilo@altama.co.id> | 2024-10-08 07:58:27 +0000 |
| commit | baf62d2196ca0d168ab370c07feb5b2415dcf19b (patch) | |
| tree | 0184463ded590abe8e3f6d79036ef7dc33245e2a /src-migrate/modules | |
| parent | b5d65791b662f4827f5a420f62e32f71f5252cff (diff) | |
| parent | f09a72f90c049b5e1a8478f737062a24d9c7a3df (diff) | |
Merged in CR/optimize_performance_2 (pull request #350)
cache localstorage
Diffstat (limited to 'src-migrate/modules')
| -rw-r--r-- | src-migrate/modules/page-content/index.tsx | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src-migrate/modules/page-content/index.tsx b/src-migrate/modules/page-content/index.tsx index edecb855..3423ca8b 100644 --- a/src-migrate/modules/page-content/index.tsx +++ b/src-migrate/modules/page-content/index.tsx @@ -1,4 +1,4 @@ -import { useMemo } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import { useQuery } from 'react-query'; import { PageContentProps } from '~/types/pageContent'; import { getPageContent } from '~/services/pageContent'; @@ -8,18 +8,38 @@ type Props = { }; const PageContent = ({ path }: Props) => { + const [localData, setData] = useState<PageContentProps>(); + const [shouldFetch, setShouldFetch] = useState(false); + + useEffect(() => { + const localData = localStorage.getItem(`page-content:${path}`); + if (localData) { + setData(JSON.parse(localData)); + }else{ + setShouldFetch(true); + } + },[]) + const { data, isLoading } = useQuery<PageContentProps>( `page-content:${path}`, - async () => await getPageContent({ path }) + async () => await getPageContent({ path }), { + enabled: shouldFetch, + onSuccess: (data) => { + if (data) { + localStorage.setItem(`page-content:${path}`, JSON.stringify(data)); + setData(data); + } + }, + } ); const parsedContent = useMemo<string>(() => { - if (!data) return ''; - return data.content.replaceAll( + if (!localData) return ''; + return localData.content.replaceAll( 'src="/web/image', `src="${process.env.NEXT_PUBLIC_ODOO_API_HOST}/web/image` ); - }, [data]); + }, [localData]); if (isLoading) return <PageContentSkeleton />; return <div dangerouslySetInnerHTML={{ __html: parsedContent || '' }}></div>; |
