Skip to content

Commit

Permalink
Add cache when fetching from API
Browse files Browse the repository at this point in the history
  • Loading branch information
255kb committed Apr 5, 2024
1 parent f37193c commit a9d9179
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { compareBuild as semverCompare } from 'semver';
import { ArticleList } from '../models/common.model';
import { Template, TemplateLight } from '../models/templates.model';

const cache = new Map();

export const sortByOrder = (firstTopic, secondTopic) =>
firstTopic.order > secondTopic.order
? 1
Expand Down Expand Up @@ -41,16 +43,23 @@ export const getDesktopLatestVersion = async () => {
};

export const getTemplatesList = async () => {
await delay(500);
if (cache.has('templates')) {
return cache.get('templates');
}

await delay(1000);
const listResponse = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/templates`
);

return (await listResponse.json()) as TemplateLight[];
const templates = (await listResponse.json()) as TemplateLight[];
cache.set('templates', templates);

return templates;
};

export const getTemplate = async (slug: string) => {
await delay(500);
await delay(1000);
const templateResponse = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/templates/${slug}`
);
Expand Down

0 comments on commit a9d9179

Please sign in to comment.