Programmatically creating pages in Gatsby - query returning all pages

I followed this outline for dynamically creating pages in Gatsby using the uid as the query parameter.

However, all docs of that type are being returned, and data.prismic.allServices_pages.edges.slice(0,1).pop(); returns the data from the first page in the array rather than the doc with the corresponding uid.

Here’s a sample of the code:
gatsby-config.js

pages: [
            { // (optional, builds pages dynamically)
            type: 'Services_page',         // TypeName from prismic - capitalize
            match: '/services/:uid',  // Pages will be generated under this pattern
            path: '/services',        // Placeholder page for unpublished documents
            component: require.resolve('./src/templates/services-template.js'),
            }
      ],

Graphql query in services-template.js

  const data = useStaticQuery(graphql`
  query ServicesPageQuery($uid: String) {
      prismic {
        allServices_pages(uid: $uid) {
          edges {
            node {
              headline
              hero_background_imageSharp {
                childImageSharp {
                  fluid {
                    base64
                    tracedSVG
                    srcWebp
                    srcSetWebp
                    originalImg
                    originalName
                  }
                }
              }
              section_intro
              services_details {
                services_detail_body
                services_detail_heading
                services_detail_image
                services_detail_imageSharp {
                  childImageSharp {
                    fluid {
                      base64
                      tracedSVG
                      srcWebp
                      srcSetWebp
                      originalImg
                      originalName
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  `)

When I log data on different service page urls, it returns the data for all service pages.

I figured out the issue - I needed to use a Page Query rather than a Static Query hook.

1 Like

Hi Joel,
Happy to hear that, and thanks for your contribution!

1 Like