Skip to content

Commit

Permalink
MDX: Change cache path of manifest files
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Sep 7, 2024
1 parent 5581733 commit f21c871
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-crabs-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---

Change cache path of manifest files
4 changes: 2 additions & 2 deletions packages/mdx/src/loader-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { findCollectionId } from '@/utils/find-collection';
import { getConfigHash, loadConfigCached } from '@/config/cached';
import { buildMDX } from '@/utils/build-mdx';
import { getDefaultMDXOptions, type TransformContext } from '@/config';
import { getKey } from '@/map/manifest';
import { getManifestEntryPath } from '@/map/manifest';
import { formatError } from '@/utils/format-error';
import { getGitTimestamp } from './utils/git-timestamp';

Expand Down Expand Up @@ -154,7 +154,7 @@ export default async function loader(
if (config.global?.generateManifest) {
await fs.mkdir('.next/cache/fumadocs', { recursive: true });
await fs.writeFile(
path.resolve('.next/cache/fumadocs', `${getKey(filePath)}.json`),
getManifestEntryPath(filePath),
JSON.stringify({
path: filePath,
data: file.data,
Expand Down
14 changes: 8 additions & 6 deletions packages/mdx/src/map/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';
import { type LoadedConfig } from '@/config/load';
import type { MetaFile } from '@/loader-mdx';
Expand All @@ -11,8 +10,13 @@ export interface Manifest {
})[];
}

export function getKey(key: string): string {
return createHash('sha256').update(key).digest('hex');
export function getManifestEntryPath(originalPath: string): string {
const toName = path
.relative(process.cwd(), originalPath)
.replaceAll(`..${path.sep}`, '-')
.replaceAll(path.sep, '_');

return path.resolve('.next/cache/fumadocs', `${toName}.json`);
}

export function writeManifest(to: string, config: LoadedConfig): void {
Expand All @@ -24,9 +28,7 @@ export function writeManifest(to: string, config: LoadedConfig): void {
if (type === 'meta') continue;

try {
const content = fs.readFileSync(
path.resolve('.next/cache/fumadocs', `${getKey(file)}.json`),
);
const content = fs.readFileSync(getManifestEntryPath(file));
const meta = JSON.parse(content.toString()) as MetaFile;

output.files.push({
Expand Down

0 comments on commit f21c871

Please sign in to comment.