Gatsby: Warn when creating multiple pages with the same path

Created on 11 Jun 2018  路  10Comments  路  Source: gatsbyjs/gatsby

Edit: see feature description at https://github.com/gatsbyjs/gatsby/issues/5836#issuecomment-396903475

Description

I have two page templates which get filled with markdown content. On one of the templates I get an error during gatsby build that props.data is undefined. In develop it just works.

This is the query in the page template:

export const query = graphql`
    query LegalQuery($slug: String!) {
        markdownRemark(frontmatter: {slug: {eq: $slug}}) {
            id
            frontmatter {
              title
              slug
              headerImage
            }
            html
        }
    }
`;

Console error:

  30 |
  31 | function LegalPage(props) {
> 32 |   const html = props.data.markdownRemark.html;
     |                           ^
  33 |   const meta = props.data.markdownRemark.frontmatter;
  34 |
  35 |   return (


  WebpackError: Cannot read property 'markdownRemark' of undefined

Environment

System:
OS: macOS High Sierra 10.13.5
CPU: x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Shell: 5.3 - /bin/zsh
Binaries:
Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
Yarn: 1.3.2 - /usr/local/bin/yarn
npm: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
Browsers:
Chrome: 67.0.3396.79
Firefox: 59.0.2
Safari: 11.1.1
npmPackages:
gatsby: ^1.9.270 => 1.9.270
gatsby-image: ^1.0.52 => 1.0.52
gatsby-link: ^1.6.44 => 1.6.44
gatsby-plugin-google-analytics: ^1.0.31 => 1.0.31
gatsby-plugin-netlify: ^1.0.21 => 1.0.21
gatsby-plugin-react-helmet: ^2.0.11 => 2.0.11
gatsby-plugin-styled-components: ^2.0.11 => 2.0.11
gatsby-plugin-typography: ^1.7.18 => 1.7.18
gatsby-source-filesystem: ^1.5.38 => 1.5.38
gatsby-transformer-excel: ^1.0.8 => 1.0.8
gatsby-transformer-json: ^1.0.19 => 1.0.19
gatsby-transformer-remark: ^1.7.42 => 1.7.42
npmGlobalPackages:
gatsby-cli: 1.1.58

File contents (if changed)

gatsby-config.js:

module.exports = {
  plugins: [
    'gatsby-plugin-react-helmet',
    'gatsby-plugin-styled-components',
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'config',
        path: `${__dirname}/config/`,
      },
    },
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'pages',
        path: `${__dirname}/src/pages/`,
      },
    },
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'data',
        path: `${__dirname}/data/`,
      },
    },
    'gatsby-transformer-remark',
    'gatsby-transformer-json',
    {
      resolve: `gatsby-transformer-excel`,
      options: {
        rawOutput: false,
      }
    },
    {
      resolve: 'gatsby-plugin-typography',
      options: {
        pathToConfigModule: 'src/utils/typography.js'
      }
    },
    'gatsby-plugin-netlify'
  ],
};

gatsby-node.js:

exports.createPages = ({ graphql, boundActionCreators }) => {
  const { createPage } = boundActionCreators;
  return new Promise((resolve, reject) => {
    graphql(`
      {
        allMarkdownRemark {
          edges {
            node {
              id
              frontmatter {
                slug
                type
              }
            }
          }
        }
      }
    `).then(result => {
      if (result.errors) {
        reject(new Error(result.errors));
      }
      result.data.allMarkdownRemark.edges.forEach(({ node }) => {
        createPage({
          path: node.frontmatter.slug,
          component: path.resolve(`./src/templates/${node.frontmatter.type}/index.js`),
          context: {
            slug: node.frontmatter.slug,
            type: node.frontmatter.type,
          },
        });
      });
      resolve()
    })
  })
};
help wanted stale?

Most helpful comment

Oh no! I'm glad you found the issue at least - a warning to avoid this for people in the future sounds like a good idea.

I'm going to re-open this and mark it as "help wanted" so we can try and save people from your pain :)

All 10 comments

I found a weird behavior of the graphql queries..
When I run

export const query = graphql`
    query LegalQuery($slug: String!) {
        legal: markdownRemark(frontmatter: {slug: {eq: $slug}}) {
            frontmatter {
              title
              slug
              headerImage
            }
            html
        }
    }
`;

I can access the data via props.data.legal.html. After a restart of the develop process, I get an error, that data is undefined. Then I can change the query to (removed the "legal" naming):

export const query = graphql`
    query LegalQuery($slug: String!) {
        markdownRemark(frontmatter: {slug: {eq: $slug}}) {
            frontmatter {
              title
              slug
              headerImage
            }
            html
        }
    }
`;

and can access the data via props.data.markdownRemark.html

I don't understand why it works sometimes and why not..

That does sound unusual... Are you able to create a small repo that demonstrates this error?

Yeah, I'm trying. My sample repo works so far. Currently adding more and more from my project to find out when it breaks

Funny story.. Unter pages I had a file called "imprint.js". I tried to generate a new page /imprint from a markdown file. This caused all the mess. As soon as I removed the .js file, the build succeeds.

Two days for this. fml

Maybe there should be a warning, if a stupid user tries to build multiple pages with the same path.

Oh no! I'm glad you found the issue at least - a warning to avoid this for people in the future sounds like a good idea.

I'm going to re-open this and mark it as "help wanted" so we can try and save people from your pain :)

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

I removed the stale label--this seems like something that's still valuable and that would be beneficial!

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.

Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

Thanks again for being part of the Gatsby community!

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Was this page helpful?
0 / 5 - 0 ratings

Related issues

signalwerk picture signalwerk  路  3Comments

rossPatton picture rossPatton  路  3Comments

ferMartz picture ferMartz  路  3Comments

magicly picture magicly  路  3Comments

jimfilippou picture jimfilippou  路  3Comments