Skip to content

Commit c63e273

Browse files
committed
TCA-590 - use swr cache for data that doesn't change over time
1 parent ef40c1f commit c63e273

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed

src-ts/tools/learn/learn-lib/data-providers/all-certifications-provider/all-certifications.provider.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import useSWR, { SWRResponse } from 'swr'
1+
import useSWR, { SWRConfiguration, SWRResponse } from 'swr'
22
import { learnUrlGet } from '../../functions'
3+
import { useSwrCache } from '../../learn-swr'
34

45
import { AllCertificationsProviderData } from './all-certifications-provider-data.model'
56

@@ -14,10 +15,12 @@ export function useGetAllCertifications(
1415

1516
const url: string = learnUrlGet(
1617
'certifications',
17-
`?providerName=${providerName}&test=true`
18+
`?providerName=${providerName}`
1819
)
20+
const swrCacheConfig: SWRConfiguration = useSwrCache(url)
1921

2022
const {data, error}: SWRResponse = useSWR(url, {
23+
...swrCacheConfig,
2124
isPaused: () => options?.enabled === false
2225
})
2326

src-ts/tools/learn/learn-lib/data-providers/courses-provider/courses.provider.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { get } from 'lodash'
2-
import useSWR, { SWRResponse } from 'swr'
2+
import useSWR, { SWRConfiguration, SWRResponse } from 'swr'
33
import { learnUrlGet } from '../../functions'
4+
import { useSwrCache } from '../../learn-swr'
45

56
import { CoursesProviderData } from './courses-provider-data.model'
67
import { LearnCourse } from './learn-course.model'
@@ -18,8 +19,9 @@ export function useGetCourses(
1819
.join('&')
1920

2021
const url: string = learnUrlGet('courses', `?${params}`)
22+
const swrCacheConfig: SWRConfiguration = useSwrCache(url)
2123

22-
const {data, error}: SWRResponse<ReadonlyArray<LearnCourse>> = useSWR(url)
24+
const {data, error}: SWRResponse<ReadonlyArray<LearnCourse>> = useSWR(url, swrCacheConfig)
2325

2426
return {
2527
course: get(data, [0]),

src-ts/tools/learn/learn-lib/data-providers/resource-provider-provider/resource-provider.provider.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { find } from 'lodash'
2-
import useSWR, { SWRResponse } from 'swr'
2+
import useSWR, { SWRConfiguration, SWRResponse } from 'swr'
33
import { learnUrlGet } from '../../functions'
4+
import { useSwrCache } from '../../learn-swr'
45

56
import { ResourceProviderData } from './resource-provider-data.model'
67
import { ResourceProvider } from './resource-provider.model'
78

89
export function useGetResourceProvider(providerName?: string): ResourceProviderData {
910

1011
const url: string = learnUrlGet('providers')
12+
const swrCacheConfig: SWRConfiguration = useSwrCache(url)
1113

12-
const {data, error}: SWRResponse<ReadonlyArray<ResourceProvider>> = useSWR(url)
14+
const {data, error}: SWRResponse<ReadonlyArray<ResourceProvider>> = useSWR(url, swrCacheConfig)
1315

1416
return {
1517
provider: find(data, {name: providerName}),

src-ts/tools/learn/learn-lib/data-providers/user-certifications-provider/user-certifications.provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export function useGetUserCertifications(
3131
const completed: ReadonlyArray<UserCertificationCompleted> = data
3232
?.filter(c => c.status === UserCertificationProgressStatus.completed)
3333
.map(c => c as UserCertificationCompleted)
34-
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()) ?? []
34+
.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()) ?? []
3535

3636
const inProgress: ReadonlyArray<UserCertificationInProgress> = data
3737
?.filter(c => c.status === UserCertificationProgressStatus.inProgress)
3838
.map(c => c as UserCertificationInProgress)
39-
.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()) ?? []
39+
.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()) ?? []
4040

4141
if (error) {
4242
errorHandle(error, 'There was an error getting your course progress.')

src-ts/tools/learn/learn-lib/learn-swr/LearnSwr.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const LearnSwr: FC<LearnSwrProps> = (props: LearnSwrProps) => {
1313
<SWRConfig
1414
value={{
1515
fetcher: (resource) => learnXhrGetAsync(resource),
16+
refreshInterval: 0,
17+
revalidateOnMount: true,
18+
revalidateOnFocus: false,
1619
}}
1720
>
1821
{props.children}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as LearnSwr } from './LearnSwr'
2+
export * from './use-swr-cache'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { mutate, SWRConfiguration } from "swr";
2+
3+
let cacheMap: any = {} as any;
4+
5+
if (typeof window !== "undefined") {
6+
try {
7+
// load all cached data from localstorage
8+
cacheMap = JSON.parse(localStorage.getItem('swr-cached') ?? '{}')
9+
} catch {}
10+
11+
// parse the loaded data, and load it into swr's in-memory cache
12+
if (cacheMap) {
13+
Object.entries(cacheMap).forEach(([key, data]) => {
14+
mutate(key, data, {revalidate: false})
15+
})
16+
}
17+
}
18+
19+
export function useSwrCache<T>(key: string): SWRConfiguration {
20+
// return handlers to store and clear localstorage data
21+
return {
22+
onError() {
23+
cacheMap[key] = undefined
24+
localStorage.setItem('swr-cached', JSON.stringify(cacheMap));
25+
},
26+
onSuccess(data: T) {
27+
cacheMap[key] = data
28+
localStorage.setItem('swr-cached', JSON.stringify(cacheMap));
29+
},
30+
}
31+
}

0 commit comments

Comments
 (0)