gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.27k stars 10.31k forks source link

gatsby-plugin-mdx: undefined behaviour when using `createPage` with `...__contentFilePath=...` #38443

Open aaaeka opened 1 year ago

aaaeka commented 1 year ago

Preliminary Checks

Description

When creating creating a page in gatsby-node.js in createPages using createPage action, if the component key in the action parameter includes ...__contentFilePath=... or is simply a url to a .mdx file, the page appears in the onCreatePage API.

For example, creating an MDX content page like this:

    actions.createPage({
      path: node.frontmatter.slug, // Slug is '/trial/'
      component: `${postTemplate}?__contentFilePath=${node.internal.contentFilePath}`,
      context: {
        id: node.id,
      },
    });

Leads to the page appearing in onCreatePage:

// Example code of how the page appears
exports.onCreatePage = async ({ page }) => {
  console.log('Created pages: ', page.path);
}

npm run start output:

...
success building schema - 0.153s
Created pages:  /trial/
success createPages - 0.027s
Created pages:  /dev-404-page/
Created pages:  /404.html
Created pages:  /404/
Created pages:  /
success createPagesStatefully - 0.054s
...

This leads to difficult to find bugs, especially if in onCreatePage the action deletePage is used, then no page is created, without telling the user why.

Reproduction Link

https://github.com/aaaeka/gatsby-create-page-bug

Steps to Reproduce

  1. Add a ...__contentFilePath=... to the createPage action;
  2. Have a onCreatePage export to see if the page appears.

Expected Result

The created page should not go through onCreatePage.

Actual Result

The created page goes through onCreatePage.

Environment

System:
    OS: Linux 5.4 Ubuntu 20.04.6 LTS (Focal Fossa)
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  Languages:
    Python: 2.7.18 - /usr/bin/python
  Browsers:
    Chrome: 115.0.5790.170
  npmPackages:
    gatsby: ^5.11.0 => 5.11.0
    gatsby-plugin-mdx: ^5.11.0 => 5.11.0

Config Flags

No response

mustaquenadim commented 4 months ago

I was using simple markdown (gatsby-transformer-remark) for writing content. But now I want to use advanced markdown (gatsby-plugin-mdx). So I decided to migrate my gatsby app from version 3 to version 5. I successfully migrated the gatsby app. But when trying to migrate remark to mdx, I'm facing critical issues like fileAbsolutePath word doesn't exist, not getting body content of the .md file with children props and not sure what to use for styling markdown content after removing dangerouslySetInnerHTML.

Also facing this issue

Invalid left-hand side in prefix operation. (1:2)

> 1 | ---
    |   ^
  2 | title: Understanding Inline, Inline-Block, and Block Elements in HTML
  3 | description: Unlock the secrets of inline, inline-block, and block elements in HTML
  4 | date: 2023-07-02