From dcfc4b452dfe5dd4183d86805f758e9e3c5f0273 Mon Sep 17 00:00:00 2001 From: Claudio Wunder Date: Thu, 30 Nov 2023 19:34:59 +0100 Subject: [PATCH] chore: update caches --- app/[locale]/[[...path]]/page.tsx | 4 ++-- next.dynamic.mjs | 38 ++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/[locale]/[[...path]]/page.tsx b/app/[locale]/[[...path]]/page.tsx index 0668a9a5bd981..fea31f53c0565 100644 --- a/app/[locale]/[[...path]]/page.tsx +++ b/app/[locale]/[[...path]]/page.tsx @@ -5,7 +5,7 @@ import type { FC } from 'react'; import { setClientContext } from '@/client-context'; import { MDXRenderer } from '@/components/mdxRenderer'; import { WithLayout } from '@/components/withLayout'; -import { ENABLE_STATIC_EXPORT, IS_DEVELOPMENT } from '@/next.constants.mjs'; +import { ENABLE_STATIC_EXPORT } from '@/next.constants.mjs'; import { DEFAULT_VIEWPORT } from '@/next.dynamic.constants.mjs'; import { dynamicRouter } from '@/next.dynamic.mjs'; import { availableLocaleCodes, defaultLocale } from '@/next.locales.mjs'; @@ -122,6 +122,6 @@ export const dynamicParams = true; // Enforces that this route is used as static rendering // Except whenever on the Development mode as we want instant-refresh when making changes // @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic -export const dynamic = IS_DEVELOPMENT ? 'force-dynamic' : 'error'; +export const dynamic = 'error'; export default getPage; diff --git a/next.dynamic.mjs b/next.dynamic.mjs index 16135d6899787..cee5be95e82cf 100644 --- a/next.dynamic.mjs +++ b/next.dynamic.mjs @@ -8,7 +8,12 @@ import matter from 'gray-matter'; import { cache } from 'react'; import { VFile } from 'vfile'; -import { MD_EXTENSION_REGEX, BASE_URL, BASE_PATH } from './next.constants.mjs'; +import { + MD_EXTENSION_REGEX, + BASE_URL, + BASE_PATH, + IS_DEVELOPMENT, +} from './next.constants.mjs'; import { DYNAMIC_ROUTES_IGNORES, DYNAMIC_ROUTES_REWRITES, @@ -47,8 +52,25 @@ const mapPathToRoute = (locale = defaultLocale.code, path = '') => ({ path: path.split(sep), }); +// Provides an in-memory Map that lasts the whole build process +// and disabled when on development mode (stubbed) +const createCachedMarkdownCache = () => { + if (IS_DEVELOPMENT) { + return { + has: () => false, + set: () => {}, + get: () => null, + }; + } + + return new Map(); +}; + const getDynamicRouter = async () => { - const cachedMarkdownFiles = new Map(); + // Creates a Cache System that is disabled during development mode + const cachedMarkdownFiles = createCachedMarkdownCache(); + + // Keeps the map of pathnames to filenames const pathnameToFilename = new Map(); const websitePages = await getMarkdownFiles( @@ -131,7 +153,7 @@ const getDynamicRouter = async () => { // We then attempt to retrieve the source version of the file as there is no localised version // of the file and we set it on the cache to prevent future checks of the same locale for this file - const { source: fileContent } = getMarkdownFile( + const { source: fileContent } = _getMarkdownFile( defaultLocale.code, pathname ); @@ -217,13 +239,13 @@ const getDynamicRouter = async () => { }); return { - getRoutesByLanguage, - getMarkdownFile, - getMDXContent, - getPathname, + mapPathToRoute, shouldIgnoreRoute, + getPathname, getRouteRewrite, - mapPathToRoute, + getRoutesByLanguage, + getMDXContent, + getMarkdownFile, getPageMetadata, }; };