Skip to content

Commit

Permalink
Fix UrlReplaceMap codec, simplify populate and cache the result
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobottaro committed Jan 13, 2025
1 parent 7deef83 commit 0a41c24
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-kangaroos-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-website": patch
---

Fix UrlReplaceMap codec, simplify populate and cache the result
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@/helpers/metadata.helpers';
import GitBookTemplate from '@/components/templates/GitBookTemplate/GitBookTemplate';
import { productPageToBreadcrumbs } from '@/helpers/breadcrumbs.helpers';
import { getGuidesProps, getUrlReplaceMapProps } from '@/lib/cmsApi';
import { getGuidesProps, getCachedUrlReplaceMapProps } from '@/lib/cmsApi';
import { generateStructuredDataScripts } from '@/helpers/generateStructuredDataScripts.helpers';
import {
breadcrumbItemByProduct,
Expand Down Expand Up @@ -77,7 +77,7 @@ const Page = async ({ params }: { params: Params }) => {
params?.productSlug,
params?.productGuidePage ?? ['']
);
const urlReplaceMap = await getUrlReplaceMapProps();
const urlReplaceMap = await getCachedUrlReplaceMapProps();
const { product, page, guide, version, versions, source, bannerLinks, seo } =
guideProps;
const props: ProductGuidePageProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getSolutionDetail, getSolutionSubPaths } from '@/lib/api';
import GitBookTemplate from '@/components/templates/GitBookTemplate/GitBookTemplate';
import { pageToBreadcrumbs } from '@/helpers/breadcrumbs.helpers';
import { ParseContentConfig } from 'gitbook-docs/parseContent';
import { getSolutionsProps, getUrlReplaceMapProps } from '@/lib/cmsApi';
import { getSolutionsProps, getCachedUrlReplaceMapProps } from '@/lib/cmsApi';
import { SolutionTemplateProps } from '@/components/templates/SolutionTemplate/SolutionTemplate';
import { generateStructuredDataScripts } from '@/helpers/generateStructuredDataScripts.helpers';
import { getItemFromPaths } from '@/helpers/structuredData.helpers';
Expand Down Expand Up @@ -60,7 +60,7 @@ const Page = async ({ params }: { params: Params }) => {
params?.solutionSubPathSlugs
);

const urlReplaceMap = await getUrlReplaceMapProps();
const urlReplaceMap = await getCachedUrlReplaceMapProps();
if (!solutionProps) {
return null;
}
Expand Down
21 changes: 19 additions & 2 deletions apps/nextjs-website/src/lib/cmsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ import { makeOverviewsProps } from '@/lib/strapi/makeProps/makeOverviews';
import { fetchTutorialListPages } from './strapi/fetches/fetchTutorialListPages';
import { makeTutorialListPagesProps } from './strapi/makeProps/makeTutorialListPages';
import { fetchUrlReplaceMap } from './strapi/fetches/fetchUrlReplaceMap';
import build from 'next/dist/build';
import { makeUrlReplaceMap } from './strapi/makeProps/makeUrlReplaceMap';
import {
makeUrlReplaceMap,
UrlReplaceMap,
} from './strapi/makeProps/makeUrlReplaceMap';

// a BuildEnv instance ready to be used
const buildEnv = pipe(
Expand Down Expand Up @@ -152,6 +154,21 @@ export const getUrlReplaceMapProps = async () => {
return staticUrlReplaceMap;
};

// eslint-disable-next-line functional/no-let
let cachedUrlReplaceMapProps: UrlReplaceMap = {}; // We need to use any[] because of the type issue makeGuide derived type are not statically defined
// eslint-disable-next-line functional/no-let
let areUrlReplaceMapCached = false;

export const getCachedUrlReplaceMapProps = async () => {
if (!areUrlReplaceMapCached) {
// eslint-disable-next-line functional/no-expression-statements
cachedUrlReplaceMapProps = await getUrlReplaceMapProps();
// eslint-disable-next-line functional/no-expression-statements
areUrlReplaceMapCached = true;
}
return cachedUrlReplaceMapProps;
};

export const getApiDataListPagesProps = async () => {
const {
config: { FETCH_FROM_STRAPI: fetchFromStrapi },
Expand Down
16 changes: 14 additions & 2 deletions apps/nextjs-website/src/lib/strapi/codecs/UrlReplaceMapCodec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import * as t from 'io-ts/lib';
import { GuideCodec } from './GuidesCodec';
import { NullToUndefinedCodec } from './NullToUndefinedCodec';

const CustomGuideCodec = t.strict({
attributes: t.strict({
title: t.string,
slug: t.string,
product: t.strict({
data: t.strict({
attributes: t.strict({
slug: t.string,
}),
}),
}),
}),
});
const UrlToGuideCodec = t.strict({
id: t.number,
url: t.string,
subPath: t.union([NullToUndefinedCodec, t.string]),
guide: t.strict({
data: t.union([NullToUndefinedCodec, GuideCodec]),
data: t.union([NullToUndefinedCodec, CustomGuideCodec]),
}),
});

Expand Down
12 changes: 1 addition & 11 deletions apps/nextjs-website/src/lib/strapi/fetches/fetchUrlReplaceMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ const makeStrapiUrlReplaceMapPopulate = () =>
urlToGuide: {
populate: {
guide: {
populate: [
'image',
'mobileImage',
'listItems',
'versions',
'bannerLinks.icon',
'seo',
'seo.metaSocial.image',
'product.logo',
'product.bannerLinks.icon',
],
populate: ['product'],
},
},
},
Expand Down

0 comments on commit 0a41c24

Please sign in to comment.