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

Serializer: more detailed docs / examples / confirmation it works #150

Closed
andrewdever opened this issue Mar 17, 2021 · 3 comments
Closed

Comments

@andrewdever
Copy link

andrewdever commented Mar 17, 2021

*I currently don't use ghost.org–but want to use this plugin for gatsby in general–so I'm posting feedback here.

I can't seem to get serializer working.

In gatsby-config.js I have: (NB: the rest of the plugin settings are fine, including the query for allBlogPost)

                mapping: {
                    allBlogpost: {
                        sitemap: `blogposts`,
                        serializer: (allBlogpost) => {
                            return allBlogpost.edges.map(edge=>{
                                return {
                                    url: 'dummy-url.com',
                                    changefreq: 'weekly',
                                    lastmod: edge.node.lastmod,
                                    priority: 0.8,
                                }
                            })
                        }
                    }
                },

sitemap-blogposts.xml output: (NB: file was generated but no priority, changefreq, i.e. not serializer didn't impact output)

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url>
        <loc>http://localhost:8000/blog/example-blog-post</loc>
        <lastmod>2021-03-17T18:14:51.012Z</lastmod>
    </url>
</urlset>

1/ Is there an issue with my serializer code? Example/s in the docs would be tres helpful!
2/ Does Serializer only work for ghost.org queries?

@andrewdever
Copy link
Author

andrewdever commented Mar 19, 2021

Further details and an update–

My goal is to add changefreq and priority to entries in the generated sitemap.

I realized my last code was giving errors (cannot read property __typeface of undefined, etc.). I've updated my code in gatsby-config.js for the plugin as follows:

{
            resolve: `gatsby-plugin-advanced-sitemap`,
            options: {
                query: `
                {
                    allBlogpost {
                        edges {
                            node {
                                id
                                slug: uid
                                updated_at: last_publication_date(formatString: "YYYY-MM-DD")
                            }
                        }
                    }
                }`,
                output: "/custom-sitemap.xml",
                mapping: {
                    allBlogpost: {
                        sitemap: `blog-posts`,
                        prefix: 'blog/',
                        // Custom Serializer
                        serializer: (edges) => {
                            const sitemapEntries = []
                            edges.forEach((edge) => {
                                edge.node.changefreq = 'weekly'
                                edge.node.priority = 0.8
                                sitemapEntries.push(edge)
                            })
                            return sitemapEntries
                        }
                    },
                },
                // etc. code redacted
            }
        },

I noticed to access the node object–where slug and updated_at variables are–I had to use edge.node.

sitemapEntries outputs:

[
  [Object: null prototype] {
    node: [Object: null prototype] {
      id: '511e9e6d-241f-5d92-a031-ebe62950210d',
      slug: 'another-blog-post',
      updated_at: '2021-03-18',
      changefreq: 'weekly',
      priority: 0.8
    }
  },
  [Object: null prototype] {
    node: [Object: null prototype] {
      id: '93aa0024-c068-5765-b4f0-402e1b8c6f20',
      slug: 'a-blogpost',
      updated_at: '2021-03-18',
      changefreq: 'weekly',
      priority: 0.8
    }
  },
  [Object: null prototype] {
    node: [Object: null prototype] {
      id: '95312c66-c0f1-5b97-a8f9-1c6df24ea3bd',
      slug: 'example-blog-post',
      updated_at: '2021-03-17',
      changefreq: 'weekly',
      priority: 0.8
    }
  }
]

So it seems the output of my serializer is valid; however, changefreq and priority aren't showing in the outputted sitemap file!

NB: I also tried edge.changefreq instead of edge.node.changefreq to no effect in outputted sitemap.

I couldn't find any documentation, examples, or solutions online. Would be great to have one as changefreq and priority I assume would be the major use case for the serializer for most people using the plugin.

@kloh-fr
Copy link

kloh-fr commented May 7, 2022

Thank you @andrewdever for your research shared here, you saved my day!

I was facing a similar issue with slugs coming from a WordPress source (issue just linked above) and the edge.node thing made the work. 😄

@Viktor19931
Copy link

any chance to add images ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants