Skip to content

Commit

Permalink
chore: update caches
Browse files Browse the repository at this point in the history
  • Loading branch information
ovflowd committed Nov 30, 2023
1 parent 12c2f00 commit dcfc4b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/[locale]/[[...path]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
38 changes: 30 additions & 8 deletions next.dynamic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -217,13 +239,13 @@ const getDynamicRouter = async () => {
});

return {
getRoutesByLanguage,
getMarkdownFile,
getMDXContent,
getPathname,
mapPathToRoute,
shouldIgnoreRoute,
getPathname,
getRouteRewrite,
mapPathToRoute,
getRoutesByLanguage,
getMDXContent,
getMarkdownFile,
getPageMetadata,
};
};
Expand Down

0 comments on commit dcfc4b4

Please sign in to comment.