---
title: "MDX Configuration Preset Loading"
description: "The execution flow from createMDX through applyMdxPreset bridges Next.js configuration integration with Fumadocs' content management engine. When initializing MDX for a Next.js project, the core fr..."
last_updated: "2026-10-12T07:53:59.756147+00:00"
canonical_url: "https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/how-it-works/mdx-configuration-preset-loading"
---


## Overview

The execution flow from `createMDX` through `applyMdxPreset` bridges Next.js configuration integration with Fumadocs' content management engine. When initializing MDX for a Next.js project, the core framework compiles user-defined source configurations, builds collections, and automatically configures remark and rehype processor options. This ensures that markdown and MDX files are parsed correctly across different build environments (bundler vs. runtime).


## Execution Flow

### Step 1: createMDX

The process begins when `createMDX` is invoked with optional configuration options (`CreateMDXOptions`). It sets up the Next.js core environment using `createNextCore` and `applyDefaults`, and ensures that initialization occurs only once by checking the `_FUMADOCS_MDX` environment variable. It returns a curried function that injects custom Webpack and Turbopack rules for `.mdx`, `.md`, `.json`, and `.yaml` files into the Next.js configuration.


### Step 2: init

Invoked asynchronously during setup, the `init` function manages core initialization and coordinates the development server lifecycle. It defines internal helper routines (`initOrReload` and `devServer`) to load configurations and watch file changes during local development.


### Step 3: devServer

If running in a development environment, `devServer` sets up a file watcher using `chokidar`. It monitors the configuration file and all registered collection directories. When the configuration file changes, the watcher safely closes itself, reloads the configuration, restarts the development server routine, and re-initializes the server watcher.


### Step 4: initOrReload

The `initOrReload` function orchestrates core startup by calling `core.init` with the result of loading the configuration file. Once initialized, it instructs the core to emit generated artifacts (such as index files or type definitions) with writing enabled (`write: true`).


### Step 5: loadConfig

The `loadConfig` function handles compiling and importing the source configuration file. When the `build` flag is set to true, it triggers `compileConfig` using `esbuild` to bundle the source configuration into a portable ESM module (`.mjs`). It then appends a timestamp query parameter to bust module cache before dynamically importing the compiled config and passing the raw exports to `buildConfig`.


### Step 6: buildConfig

The `buildConfig` function parses raw configuration exports, mapping collection objects and global settings. It constructs collection instances and exposes a `getMDXOptions` method. When MDX options are requested for a specific collection or environment, it evaluates global or collection-level options and delegates preset generation to `applyMdxPreset`.


### Step 7: applyMdxPreset

Finally, `applyMdxPreset` takes the user's MDX options and returns an asynchronous function that resolves processor options for the specified build environment (`bundler` or `runtime`). If the preset is set to `'minimal'`, it returns the options unchanged. Otherwise, it loads `fumadocs-core/mdx-plugins` and configures standard remark plugins (such as `remarkGfm`, `remarkHeading`, `remarkImage`, and `remarkStructure`) and rehype plugins (such as `rehypeCode` and `rehypeToc`), adjusting options based on whether it is targeting a bundler or runtime environment.


---

## Architecture Diagrams

### Sequence Diagram

```mermaid
sequenceDiagram
    participant Next as Next.js Config
    participant Index as packages/mdx/next
    participant Core as Core Engine
    participant Loader as loadConfig
    participant Builder as buildConfig
    participant Preset as applyMdxPreset

    Next->>Index: createMDX(options)
    Index->>Index: init(isDev, core)
    Index->>Core: initOrReload()
    Core->>Loader: loadConfig(core, true)
    Loader->>Loader: compileConfig(core)
    Loader->>Builder: buildConfig(loaded, cwd)
    Builder->>Preset: applyMdxPreset(options)
    Preset-->>Builder: ProcessorOptions
    Builder-->>Loader: LoadedConfig
    Loader-->>Core: Resolved config
    Core-->>Index: Initialized
```


### Flowchart

```mermaid
flowchart TD
    A[createMDX] --> B[init]
    B --> C{isDev?}
    C -->|Yes| D[devServer & initOrReload]
    C -->|No| E[initOrReload]
    D --> F[loadConfig]
    E --> F[loadConfig]
    F --> G[compileConfig with esbuild]
    G --> H[buildConfig]
    H --> I[applyMdxPreset]
    I --> J[Return ProcessorOptions]
```


---

## Key Observations

- **Cross-Module Boundaries:** The flow bridges Next.js integration hooks (`packages/mdx/src/next/index.ts`) with configuration building (`packages/mdx/src/config/load-from-file.ts` and `build.ts`) and unified plugin management (`packages/mdx/src/config/preset.ts`).
- **Error Handling:** Configuration compilation checks `transformed.errors.length` and throws an error if `esbuild` fails. Dynamic imports and file watcher logic ensure that updates to config files gracefully restart the development environment.
- **Caching & Dynamic Reloading:** `loadConfig` invalidates import caches by appending a millisecond timestamp hash query parameter to the file URL, ensuring fresh configuration loads during development reloads.


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/e8872a72-1909-479c-b585-dc74c6e26726/llms.txt) for all pages in this wiki.
