NuxtN
Nuxt11mo ago
5 replies
DamianId

Nuxt 3 - generating dynamic sitemap xml for storyblock

Hello!
I installed sitemap module from nuxt modules and I have problem with dynamic generating sitemap for articles. I dont see URL's from dynamic generation on sitemap.xml.

I have 5 static routes, and X dynamic routes.

I added this code into nuxt config but IDE provide me information that routes is not used:

    routes: async () => {
      try {
        const { useStoryblokApi } = await import('@storyblok/vue');
        const storyblokApi = useStoryblokApi();

        const version = process.env.NODE_ENV === 'development' ? 'draft' : 'published';
        console.log(`[Sitemap] Fetching Storyblok articles with version: ${version}`);

        const { data } = await storyblokApi.get('cdn/stories', {
          version: version,
          starts_with: 'articles/',
          per_page: 100
        });

        if (data && data.stories && data.stories.length > 0) {
          console.log(`[Sitemap] Found ${data.stories.length} articles`);

          return data.stories.map(story => ({
            url: `/articles/${story.slug}`,
            lastmod: story.published_at || story.created_at,
            changefreq: 'monthly',
            priority: 0.8
          }));
        } else {
          console.log('[Sitemap] No articles found in Storyblok');
          return [];
        }
      } catch (error) {
        console.error('[Sitemap] Error generating dynamic sitemap entries:', error);
        return [];
      }
    }
Was this page helpful?