Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the index page of my Gatsby site to be one of the dynamically generated pages?

I have a Gatsby site that queries information from a Wordpress REST API with GraphQL to dynamically create the site pages. I'd like to set my index page to be the homepage that is being created dynamically i.e home.html

I saw this post that was similar On Gatsby CMS how can i set the about page as a index page

However, they have an about.js file that corresponds to their about page, meaning they can export it as a component and use it in index or they can even just copy the contents of that file over to index.js. The homepage that I want to set as my index is being generated dynamically and using a GraphQL query that can't be used outside of the page.js template. So I don't see an easy way to copy that over to another file.

I guess my last option would be to set my server to point to the static file in public/home.html and serve that as the site root, but the person in that posting tries to deter people from doing that.

Any ideas?

Here is page.js template that generates the pages of the site:

const PageTemplate = ({ data }) => (
    <Layout>
        {<h1 dangerouslySetInnerHTML={{ __html: data.currentPage.title }} />}
        {
           renderBlocks(gatherBlocks(data.currentPage.acf.page_blocks, data))
        }
    </Layout>
);

export default PageTemplate;

export const pageQuery = graphql`
    query ($id: String!) {
        currentPage: wordpressPage(id: {eq: $id}) {
            title
            id
            parent {
                id
            }
            template
            acf {
                page_blocks {
                block_type {
                    acf_fc_layout
                    cs_title
                    cs_text
                }
                wordpress_id
                }
            }
        }
    }
`;

And here is my index page:

import React from "react"

import Layout from "../components/global/Layout"

const IndexPage = () => (
  <Layout>
    <h1>Hi people</h1>
    <p>Welcome to the Tank Gatsby site.</p>
    <p>Now go build something great.</p>
  </Layout>
)

export default IndexPage
like image 491
user2030942 Avatar asked Jan 22 '20 21:01

user2030942


People also ask

How do you make a dynamic page in The Great gatsby?

Gatsby's File System Route API lets you dynamically create new pages from data layer nodes by naming your files with a special syntax. File System Routes only work on files in the src/pages directory (or subdirectories). To create a new collection route, you name your file {nodeType. field}.

What are SRC pages in gatsby?

/src This directory will contain all of the code related to what you will see on the frontend of your site (what you see in the browser), like your site header, or a page template. “src” is a convention for “source code”.

How do I run the Great gatsby site locally?

To run your site locally, use the gatsby develop command. You can view your site in a web browser at localhost:8000 . Gatsby Cloud is an infrastructure platform specifically optimized for building, deploying, and hosting Gatsby sites.


1 Answers

I experienced the same situation today. I used the following approach to use my dynamically created page with uri '/home'(fetched from wordpress using GraphQL query) as the home page of my Gatsby site:

  1. Delete the default index.js file in your pages directory.
  2. In gatsby-node.js file, change the uri of page from '/home' to '/' just before using the CreatePage API. Here is the sample code to achieve the desired result:
// loop through WordPress pages and create a Gatsby page for each one
  pages.forEach(page => {
    if(page.uri==='/home/')
      page.uri = '/'
    actions.createPage({
      path: page.uri,
      component: require.resolve(`./src/templates/${page.template.templateName}.js`),
      context: {
        id: page.id,
      },
    })
  })

In the above code, pages refer to the pages fetched from WordPress using GraphQL.

like image 135
Isha Vohra Avatar answered Nov 15 '22 15:11

Isha Vohra