question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Sitepage.context` no longer available in Gatsby v4 in generating sitemap.xml

See original GitHub issue

As documented in Here, it said we should query the allSitePage and filter it with context schema.

 plugins: [
  {
    resolve: 'gatsby-plugin-sitemap',
    ... // skipped
      query: `
          {
            site {
              siteMetadata {
                siteUrl
              }
            }
            allSitePage(filter: {context: {i18n: {routed: {eq: false}}}}) {
              edges {
                node {
                  context {
                    i18n {
                      defaultLanguage
                      languages
                      originalPath
                    }
                  }
                  path
                }
              }
            }
          }
        `,
      ... // skipped
      }
    }
  }
];

However, as mentioned by Gatsby official: Field SitePage.context is no longer available in GraphQL queries (source). If we still try to use the existing codes to generate sitemap through gatsby-plugin-sitemap, we will result in the following error:

Error executing the GraphQL query inside gatsby-plugin-sitemap:
 Field "context" is not defined by type "SitePageFilterInput".



  GraphQLError: Field "context" is not defined by type "SitePageFilterInput".

, and

 ERROR 

Error executing the GraphQL query inside gatsby-plugin-sitemap:
 Cannot query field "context" on type "SitePage".



  GraphQLError: Cannot query field "context" on type "SitePage".
  

Although the Gatsby official said we could create Sitepage.context manually as a workaround, it does not seem to be a good practice in the long term. Would there be any suggestion fix that satisfies the long-term goal? Thanks.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
bebbicommented, Aug 18, 2022

Here’s how I got the overall sitemap working again. I think the issue here is that people get stuck on the siteUrl error masking the real issue and don’t easily reach the discussion here.

3reactions
wilsonvolkercommented, Jul 13, 2022

For those facing the same issue and wish to add back the context type as a quick workaround.

  1. Open/Create gatsby-node.js in your root directory. (<PROJECT_DIR>/gatsby-node.js)
  2. Add the following code in gatsby-node-js
/**
 * Workaround for missing sitePage.context:
 * Used for generating sitemap with `gatsby-plugin-react-i18next` and `gatsby-plugin-sitemap` plugins
 * https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v3-to-v4/#field-sitepagecontext-is-no-longer-available-in-graphql-queries
 */
exports.createSchemaCustomization = ({ actions }) => {
    const { createTypes } = actions
    createTypes(`
    type SitePage implements Node {
      context: SitePageContext
    }
    type SitePageContext {
      i18n: i18nContext
    }
    type i18nContext {
        language: String,
        languages: [String],
        defaultLanguage: String,
        originalPath: String
        routed: Boolean
    }
  `)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating from v4 to v5 - Gatsby
This is a reference for upgrading your site from Gatsby 4 to Gatsby 5. Version 5 introduces the Slice API and Partial Hydration...
Read more >
How to get updated / lastmod value for static files for sitemap ...
1. Use gatsby-source-filesystem to add the static page files to your filesystem so you can query them. In gatsby-config.js add the following ...
Read more >
Gatsby.js — Sitemap and RSS Feed
Gatsby is a static web site framework that's based on React. We can use it to create static websites from external data sources...
Read more >
How to Add a Sitemap and Robots.txt to a Gatsby Project
In this video, I demonstrate adding and configuring an XML Sitemap and a robots.txt file for a Gatsby site.We're going to be bringing...
Read more >
Generate an SEO-Friendly Sitemap for your Gatsby Site
If you maintain a blog, documentation, or any other sort of writing-based Gatsby site, you know how much work goes into content creation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found