Generate sitemap according prismic data in Nuxt

357 Views Asked by At

I'm guessing some of you have some experience with generating Sitemap for your project. I'm having a Nuxt ( SSR full static ) project. I'm using @nuxtjs/prismic and @nuxtjs/sitemap module.

The sitemap module does not generate dynamic pages therefor we need to asynchronously retrieve all the pages we have on the website and process the data into a format required by the sitemap module.

I'm guessing that I need to write some build module where I would have access to prismic and then generate and write the desired data format.

Help me please to understand how to import or access prismic from module level so I can write the desired logic.

UPDATE I created build module and registered it in nuxt.config.js. Successfully retrieving data from prismic query. In the code bellow In my loop I transform prismic data in to data format that is required by the sitemap module.

I pass the data to sitemap module with this.nuxt. Right now this code passes the correct data however the module overrides the static pages and the only correct data left is for the dynamic pages. It looks that I need to disable the automatic generate by the module and only to work with my data.

// ... all dynamic pages generated correctly
<url>
<loc>https://danica-dev.netlify.app/blog/arrival-of-ikea-in-ukraine</loc>
<lastmod>2021-05-01T14:00:04.000Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.3</priority>
</url>
<url>
<loc>https://danica-dev.netlify.app/en</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/kontakt</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/privacy-policy</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/blog</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/contact</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/privacy-policy</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/en/projects</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/blog</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/kontakt</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/privacy-policy</loc>
</url>
<url>
<loc>https://danica-dev.netlify.app/ru/projects</loc>
</url>

// modules/sitemap.js
import prismic from '@prismicio/client'

const apiEndpoint = 'https://danica.cdn.prismic.io/api/v2'
const client = prismic.client(apiEndpoint)

export default async function Sitemap() {
    const data = await client.query('', { pageSize: 100 })
    const pages = []

    data.results.forEach((page) => {
        switch (page.type) {
            case 'blog_post':
                pages.push({
                    url: `blog/${page.uid}`,
                    changefreq: 'weekly',
                    priority: 0.25,
                    lastmod: page.last_publication_date,
                })
                break
            case 'project_post':
                pages.push({
                    url: `projects/${page.uid}`,
                    changefreq: 'monthly',
                    priority: 0.5,
                    lastmod: page.last_publication_date,
                })
                break
      //...
            default:
                break
        }
    })

    this.nuxt.options.sitemap.routes = pages
}

0

There are 0 best solutions below