Gatsby MDX 页面在 src/pages 子目录中未完全渲染的解决方案

本文旨在解决 Gatsby 项目中使用 MDX 文件时,当 MDX 文件位于 `src/pages` 目录的子目录中,构建后部分页面出现未完全渲染(缺少布局和样式)的问题。通过分析 `gatsby-plugin-page-creator` 插件的影响,提供了一种移除该插件的解决方案,从而避免页面重复创建并修复渲染问题。

在使用 Gatsby 构建网站时,你可能会遇到这样的问题:当 MDX 文件位于 src/pages 目录的子目录中时,使用 gatsby build 构建后,某些页面仅渲染页面主体内容,而缺少布局组件的包装和样式。这通常发生在具有特定目录结构的 Gatsby 项目中,例如:

src/pages/
  --project/
    --contact.md
    --outputs.md
    --project.md
    --sources.md
  --software/
    --apps.md
    --frontend.md
    --system.md

以下将介绍问题的原因以及如何解决它。

问题分析

问题通常与 Gatsby 的路由机制有关。当 gatsby-plugin-page-creator 插件与 gatsby-plugin-mdx 插件同时使用时,可能会导致页面重复创建,从而引发渲染问题。具体来说,gatsby-plugin-page-creator 插件会根据 src/pages 目录下的文件结构自动创建页面,而 gatsby-plugin-mdx 插件也会根据 MDX 文件创建页面。当两者创建的页面路由冲突时,就可能导致页面渲染不完整。

一个典型的警告信息如下:

warn Non-deterministic routing danger: Attempting to create page: "/project/contact/", but page
"/project/contact" already exists

这个警告表明 Gatsby 尝试创建重复的页面路由。

解决方案

解决此问题的关键是移除 gatsby-plugin-page-creator 插件。如果你的项目同时使用了 gatsby-plugin-page-creator 和 gatsby-plugin-mdx 插件,并且遇到了页面渲染不完整的问题,那么移除 gatsby-plugin-page-creator 插件通常可以解决问题。

  1. 从 gatsby-config.js 中移除 gatsby-plugin-page-creator 插件。

    打开 gatsby-config.js 文件,找到 plugins 数组,移除 gatsby-plugin-page-creator 相关的配置项。例如:

    module.exports = {
      plugins: [
        // ...其他插件
        // {
        //   resolve: `gatsby-plugin-page-creator`, // 移除此插件
        //   options: {
        //     path: `${__dirname}/src/pages`,
        //   },
        // },
        // ...其他插件
      ],
    };
  2. 确保 MDX 文件使用标准扩展名。

    最初,你可能使用了非标准的 MDX 文件扩展名(例如 .mdx),这可能导致需要 gatsby-plugin-page-creator 插件来正确解析这些文件。现在,确保你的 MDX 文件使用标准的 .md 或 .mdx 扩展名。

  3. 清理 Gatsby 缓存。

    在移除插件后,建议清理 Gatsby 缓存,以确保所有更改都生效。运行以下命令:

    gatsby clean
  4. 重新构建项目。

    使用以下命令重新构建项目:

    gatsby build

    然后,检查构建后的网站,确认页面是否已正确渲染。

示例代码

以下是一个典型的 gatsby-config.js 文件的示例,展示了如何移除 gatsby-plugin-page-creator 插件:

module.exports = {
  siteMetadata: {
    title: `My Gatsby Site`,
    description: `A simple Gatsby site.`,
    author: `You`,
  },
  plugins: [
    `gatsby-plugin-image`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `pages`,
        path: `${__dirname}/src/pages`,
      },
    },
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        extensions: [`.md`, `.mdx`, `.markdown`],
        gatsbyRemarkPlugins: [
          {
            resolve: `gatsby-remark-images`,
            options: {
              maxWidth: 1024,
            },
          },
        ],
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
}

注意事项

  • 在移除 gatsby-plugin-page-creator 插件之前,请确保你了解该插件的作用以及移除后可能产生的影响。
  • 如果你的项目依赖于 gatsby-plugin-page-creator 插件的其他功能,你可能需要寻找替代方案来实现这些功能。
  • 在修改 gatsby-config.js 文件后,务必清理 Gatsby 缓存并重新构建项目,以确保所有更改都生效。

总结

当 Gatsby 项目中使用 MDX 文件,并且 MDX 文件位于 src/pages 目录的子目录中时,可能会出现页面渲染不完整的问题。这通常是由于 gatsby-plugin-page-creator 插件与 gatsby-plugin-mdx 插件冲突导致的。通过移除 gatsby-plugin-page-creator 插件,并确保 MDX 文件使用标准扩展名,可以解决此问题。在修改配置后,务必清理 Gatsby 缓存并重新构建项目。