Generate sitemap according prismic data in Nuxt

Hello there :v:
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.

What are you using for deployment?

If you are using Netlify, the easiest way is to just use the Netlify Sitemap Plugin.

Best
Aleks

In nuxt.config.js add:

const Prismic = require("@prismicio/client");

And in sitemap configuration block:

sitemaps: [
      {
        path: "/sitemap-pages.xml",
        routes: async () => {
          const pages = await getData("page");
          return [...pages.map((i) => `/${i.uid}`)];
        },
      },
    ],

In your case names maybe different, but I hope you've got the idea :slight_smile:

2 Likes

I did as you suggest in your code and it works partially.
All of my static pages are probably rewritten and are missing data, however dynamic pages are filled correctly. Here how my sitemap looks

@martingjorceski, did you know that you can create the route for your Nuxt project using a Route Resolver?

With @nuxtjs/prismic you can add a routes option in the prismic.apiOptions configuration object and pass a similar object as you are doing with the other plugin.

This thread has been closed due to inactivity. Flag to reopen.

Project details:

  • "@nuxtjs/prismic": "^1.3.1"
  • "nuxt": "^2.15.7"
  • "typescript": "^4.3.5"
  • "@nuxtjs/sitemap": "^2.4.0"

I'm using the repeatable type in Prismic for pages such as policy and blog pages. I want to make sure the routes are generated in my Nuxt project and appear in the sitemap. The sitemap works without any of the below code but it doesn't show the policy routes.

// nuxt.config.js
generate: {
  async routes () {
    let generatedRoutes = []

    // Policy pages
    let policies = await $prismic.api.query(
      $prismic.predicates.at('document.type','policy')
    )
    policies = policies.results.map(page => page.uid)
    policies.forEach(policyUid => {
      generatedRoutes.push(`/policies/${policyUid}`)
    });

    return generatedRoutes
  }
}

But I'm getting an error when accessing /sitemap.xml, I get a $prismic is not defined error. I felt adding const { $prismic } = require('@nuxtjs/prismic') would have resolved this:

// nuxt.config.js
generate: {
  async routes () {
    const { $prismic } = require('@nuxtjs/prismic')
    let generatedRoutes = []

    // Policy pages
    let policies = await $prismic.api.query(
      $prismic.predicates.at('document.type','policy')
    )
    policies = policies.results.map(page => page.uid)
    policies.forEach(policyUid => {
      generatedRoutes.push(`/policies/${policyUid}`)
    });

    return generatedRoutes
  }
}

But the error is now Cannot read property api of undefined. Any ideas of getting around this?

Hi Jack,

Thanks for reaching out; for me, this issue can be because you are supplying an async function 'async routes ()' and not returning a promise such as what is mentioned in the docs:

Please let me know if that doesn't fix the issue to try to dig deeper.

Best,
Fares

Another point here could be that you need to instantiate the Prismic client where you can't use the $prismic directly; you might need to use asyncData to client API instance in Nuxt such as

meanwhile I'm going to do some research and I will come back to you if I get an info.

Hi Fares, they don't seem to do the trick. Thank you for helping. It appears the Nuxt config doesn't recognise the $prismic object. The way around this would be to install @prismicio/client and use Prismic.client() to query the content rather than $prismic.api.query() from @nuxtjs/prismic. This seems more of a @nuxtjs/prismic issue, but the GitHub issues tab directed me here for this kind of general query.

Many thanks,
Jack

Hey Everyone,

Since all your static routes are already generated for you by Nuxt and it's crawler, you can just take this information and create your sitemap from this information.

@tbenniks explains how to do this in his blog post and youtube video:

Thanks.

1 Like

Thank you for the response. This is my bad, it looked as though the issue was with the sitemap module but it actually seems to be an issue with the Nuxt prismic module. See bug - $prismic object not recognised in nuxt.config.(js|ts) · Issue #146 · nuxt-community/prismic-module · GitHub

Yeah, I don't know if it's a bug as it's just not designed to work that way. I can see you got a response in the thread too.

Anyway, the best way to create your sitemap is to use the already generated routes as described above.

Happy coding. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.