close

Conventional route

What is it?

Rspress uses file system routing, and the file path of the page is simply mapped to the routing path, which makes the routing of the entire project very intuitive.

For example, if there is a file named foo.md in the docs directory, the routing path for that file will be /foo.

Mapping rules

Rspress automatically scans the root directory and all subdirectories, and maps file paths to route paths. For example, if you have the following file structure:

docs
foo
bar.md
index.md
zoo.md
index.md

Then bar.md will be routed to /foo/bar and /foo/index.md will be routed to /foo/.

The specific mapping rules are as follows:

filepathroute pathcleanUrl: false path
index.md//index.html
/zoo.md/zoo/zoo.html
/foo/index.md/foo//foo/index.html
/foo/bar.md/foo/bar/foo/bar.html
Warning

Do not have files and folders with the same name, which will cause routing conflicts. For example, the following file structure is not allowed:

docs
foo
index.md
foo.md

TSX routing

In conventional routing, in addition to .md(x) files, you can also use .tsx files as route components. By default, a component is exported in .tsx, and this component will be automatically registered in the route. For example:

foo.tsx
export default () => {
  return <div>foo</div>;
};

Of course, if you want to customize the layout, you can add an export to declare the layout type. For example:

foo.tsx
export const frontmatter = {
  // Declare layout type
  // The custom layout here will not have a sidebar
  pageType: 'custom',
};

For detailed meanings of each pageType, please refer to the API documentation.

Custom behavior

If you want to customize the routing behavior, you can use the route field in the configuration file. For example:

rspress.config.ts
import { defineConfig } from '@rspress/core';

export default defineConfig({
  route: {
    // These files will be registered as routes (supports glob pattern)
    include: ['other-dir/**/*'],
    // These files will not be registered as routes (supports glob pattern)
    exclude: ['components/**/*', 'fragments/**/*'],
  },
});

Exclude files starting with _ by default

In the docs directory, MDX fragments or React components need to be excluded from routing through the route.exclude configuration. For convenience, files starting with "_" are excluded by default via route.excludeConvention.

You can also place components in adjacent directories outside the docs directory, for example:

docs
_button.mdx
index.mdx
components
button.tsx
docs/index.mdx
docs/_button.mdx
components/button.tsx
import ButtonFragment from './_button.mdx';
import Button from '../../components/button';

<ButtonFragment />
<Button />

It will be rendered as:

button

This is text from MDX

Best practices

We recommend that you place documentation files in the docs directory to make your project more clear. For non-documentation content, such as custom components, util functions, etc., they can be maintained outside the docs directory. If placed in the docs directory, you need to use route.exclude together.

Tip

If you want to place custom components or document fragments in the docs directory, you need to use route.exclude together, otherwise these files will be automatically registered as routes, which may cause some unexpected issues.

Here is a best practice file structure that includes MDX fragments and React components, custom themes, conventional routing, and internationalization:

docs
components# React components used in documentation
Example.tsx
zh
fragments# zh document fragments
example.mdx
index.mdx
en
fragments# en document fragments
example.mdx
index.mdx
theme
components# React components for theme
DocFooter.tsx
index.tsx
rspress.config.ts
import { defineConfig } from '@rspress/core';

export default defineConfig({
  route: {
    exclude: ['*/components/**/*', '*/fragments/**/*'], // Files in these directories won't be registered as routes
  },
  lang: 'en',
  locales: [
    {
      lang: 'zh',
      label: '中文',
    },
    {
      lang: 'en',
      label: 'English',
    },
  ],
});
tsconfig.json
{
  "compilerOptions": {
    "lib": ["DOM", "ESNext"],
    "jsx": "react-jsx",
    "moduleResolution": "bundler",
    "paths": {
      "i18n": ["./i18n.json"],
      "@theme": ["./theme/index.tsx"]
    }
  },
  "include": ["docs", "theme", "rspress.config.ts"],
  "mdx": {
    "checkMdx": true
  }
}