Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send props to index page #3410

Closed
omidahourai opened this issue Jan 4, 2018 · 5 comments
Closed

Send props to index page #3410

omidahourai opened this issue Jan 4, 2018 · 5 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@omidahourai
Copy link

Description

In gatsby-node.js, I am trying to inject data from Instagram to my index page by calling createPage(), but it doesn't seem to work unless I rename the index.js to home.js and have it create it from home.js with path of /. This works but now I have a duplicate /home/ path that I don't want to exist. Tried calling deletePage() on the home path but it throws errors so maybe I'm doing it wrong? I tried to delete it in exports.createPages, and also in exports.onPostBootstrap.

Environment

Gatsby version: ^1.9.147
Node.js version: 8.9.3
Operating System: OSX 10.13.2

File contents (if changed):

gatsby-node.js:

//const instagramData = {...}
exports.createPages = ({ graphql, boundActionCreators }) => {
    const { createPage, deletePage } = boundActionCreators
    return new Promise((resolve, reject) => {
        createPage({
            layout: 'index',
            path: '/',
            component: slash(path.resolve(`./src/pages/home.js`)),
            context: { instagramData }
        })

        graphql(ARTICLES_QUERY).then(({errors, data}) => {
            if (errors) {
                reject(errors)
            }

            const template = path.resolve(`./src/templates/article.js`)
            forEach(data.allContentfulArticle.edges, edge => {
                const { id, slug } = edge.node
                createPage({
                    layout: 'article',
                    path: `/articles/${slug}/`,
                    component: slash(template),
                    context: { id, slug },
                })
            })
            resolve()
        })
    })
}

exports.onPostBootstrap = ({ boundActionCreators }) => {
    const { deletePage } = boundActionCreators
    return new Promise((resolve, reject) => {
        deletePage({
            path: '/home/',
            component: slash(path.resolve('./src/pages/home')),
        })
        resolve()
    })
}
@KyleAMathews
Copy link
Contributor

You're supposed to query for data for a page from its component. Have you tried that? createPage is for creating pages from data e.g. markdown files.

@omidahourai
Copy link
Author

Hmm, I suppose that would make sense. But then, wouldn't Instagram need to provide a graphql API? I think they only do REST.

@dsbrianwebster
Copy link

@omidahourai obviously Kyle is right about how it is supposed to work (querying data from its component). With that said, we actually did have a scenario where I needed to build the index page via createPages from gatsby-node.js. In case you and anyone else needs to do THAT, simply placing your home component in the /templates directory instead of /pages will solve your problem with the duplicate duplicate /home/ path :)

@kakadiadarpan kakadiadarpan added the type: question or discussion Issue discussing or asking a question about Gatsby label Sep 27, 2018
@kakadiadarpan
Copy link
Contributor

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub issues, we have to clean some of the old issues as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Gatsby version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@kakadiadarpan kakadiadarpan added the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 27, 2018
@kakadiadarpan
Copy link
Contributor

This issue is being closed because there hasn't been any activity for at least 30 days. Feel free to open a new one if you still experience this problem 👍

This was referenced Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

4 participants