-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnext.config.mjs
More file actions
73 lines (65 loc) · 2.47 KB
/
next.config.mjs
File metadata and controls
73 lines (65 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import nextMDX from '@next/mdx';
import { recmaPlugins } from './src/mdx/recma.mjs';
import { rehypePlugins } from './src/mdx/rehype.mjs';
import { remarkPlugins } from './src/mdx/remark.mjs';
import { buildSearchIndex } from './scripts/build-search-index.mjs';
const withMDX = nextMDX({
options: {
remarkPlugins,
rehypePlugins,
recmaPlugins,
},
});
// Global flag to prevent duplicate builds across multiple webpack instances
let searchIndexBuilt = false;
// Custom webpack plugin to build search index
class SearchIndexPlugin {
apply(compiler) {
compiler.hooks.beforeCompile.tapAsync('SearchIndexPlugin', async (params, callback) => {
if (!searchIndexBuilt) {
console.log('Building search index...');
try {
await buildSearchIndex();
searchIndexBuilt = true;
console.log('Search index built successfully');
} catch (error) {
console.error('Failed to build search index:', error);
return callback(error);
}
}
callback();
});
// Also rebuild on file changes in development
if (compiler.options.mode === 'development') {
compiler.hooks.watchRun.tapAsync('SearchIndexPlugin', async (compiler, callback) => {
const changedFiles = compiler.modifiedFiles || new Set();
const mdxChanged = Array.from(changedFiles).some(file => file.endsWith('.mdx'));
if (mdxChanged) {
console.log('MDX files changed, rebuilding search index...');
try {
await buildSearchIndex();
console.log('Search index rebuilt successfully');
} catch (error) {
console.error('Failed to rebuild search index:', error);
}
}
callback();
});
}
}
}
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
outputFileTracingIncludes: {
'/**/*': ['./src/app/**/*.mdx'],
},
webpack: (config, { dev, isServer }) => {
// Only add the plugin on the server side to avoid duplicate builds
if (isServer) {
config.plugins.push(new SearchIndexPlugin());
}
return config;
},
};
export default withMDX(nextConfig);