Skip to content

Commit

Permalink
fix(docs): fix mdx loader cache invalidation bug on versions changes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Feb 14, 2025
1 parent a72a06e commit af272bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export async function mdxLoader(
const compilerName = getWebpackLoaderCompilerName(this);
const callback = this.async();
const options: Options = this.getOptions();
options.dependencies?.forEach(this.addDependency);
try {
const result = await loadMDXWithCaching({
resource: this.resource,
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-mdx-loader/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {ResolveMarkdownLink} from './remark/resolveMarkdownLinks';
import type {PromiseWithResolvers} from './utils';

export type Options = Partial<MDXOptions> & {
dependencies?: string[];

markdownConfig: MarkdownConfig;
staticDirs: string[];
siteDir: string;
Expand Down
33 changes: 33 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import path from 'path';
import fs from 'fs-extra';
import _ from 'lodash';
import logger from '@docusaurus/logger';
import {
Expand Down Expand Up @@ -63,6 +64,30 @@ import type {LoadContext, Plugin} from '@docusaurus/types';
import type {DocFile, FullVersion} from './types';
import type {RuleSetRule} from 'webpack';

// MDX loader is not 100% deterministic, leading to cache invalidation issue
// This permits to invalidate the MDX loader cache entries when content changes
// Problem documented here: https://github.com/facebook/docusaurus/pull/10934
// TODO this is not a perfect solution, find better?
async function createMdxLoaderDependencyFile({
dataDir,
options,
versionsMetadata,
}: {
dataDir: string;
options: PluginOptions;
versionsMetadata: VersionMetadata[];
}) {
const filePath = path.join(dataDir, '__mdx-loader-dependency.json');
// the cache is invalidated whenever this file content changes
const fileContent = {
options,
versionsMetadata,
};
await fs.ensureDir(dataDir);
await fs.writeFile(filePath, JSON.stringify(fileContent));
return filePath;
}

export default async function pluginContentDocs(
context: LoadContext,
options: PluginOptions,
Expand Down Expand Up @@ -107,6 +132,14 @@ export default async function pluginContentDocs(
return createMDXLoaderRule({
include: contentDirs,
options: {
dependencies: [
await createMdxLoaderDependencyFile({
dataDir,
options,
versionsMetadata,
}),
],

useCrossCompilerCache:
siteConfig.future.experimental_faster.mdxCrossCompilerCache,
admonitions: options.admonitions,
Expand Down

0 comments on commit af272bd

Please sign in to comment.