nuxt-modules / sitemap

Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.
https://nuxtseo.com/sitemap
327 stars 29 forks source link

Error prerendering `sitemap.xml` #109

Closed flexchar closed 1 year ago

flexchar commented 1 year ago

Describe the bug

I'm trying to deploy Nuxt 3 as a fully static site (I don't want SSR on functions as they slow down) on Netlify.

I can see an error in the logs:

11:25:59 PM: [success] [nitro] Generated public dist
11:25:59 PM: [info] [nitro] Initializing prerenderer
11:26:02 PM: [info] [nitro] Prerendering 37 initial routes with crawler
11:26:02 PM: [log] [nitro]   ├─ sitemap.xml (undefinedms) (TypeError: Failed to parse URL from sitemap.xml)
11:26:02 PM: [log] [nitro]   ├─ /200.html (299ms)
11:26:02 PM: [log] [nitro]   ├─ /404.html (298ms)

Relevant parts from my Nuxt config

routeRules: {
    '/': { static: true },
    '/blog/**': { static: true },
    '/sitemap.xml': { static: true },
},
modules: [
    '@nuxtjs/tailwindcss',
    'nuxt-icons',
    'nuxt-simple-sitemap',
],
nitro: {
    prerender: {
        crawlLinks: true,
        routes: ['/', 'sitemap.xml'],
    },
    static: true,
},
hooks: {
    async 'nitro:config'(nitroConfig) {
        if (nitroConfig.dev) {
            return;
        }
        const dynamicPosts = (await getBlogPages()).map(p => `/posts/${p.slug}`);

        nitroConfig.prerender?.routes?.push(...dynamicPosts);
    },
},
sitemap: {
    // provide dynamic URLs to be included
    urls: async () => {
        const blogPages = await getBlogPages();
        return blogPages.map(page => ({
            loc: `/posts/${page.slug}`,
            lastmod: new Date(page.created_at),
            changefreq: 'daily',
            priority: 0.8,
        }));
    },
}

I've been playing a lot with this but doesn't seem like it wanna work nicely. I'm also having tough time understanding all the possible rendering options so I might have confused myself. But it seems that this time it's Nitro that struggles to prerender Sitemap file. Any ideas? :)

Reproduction

No response

System / Nuxt Info

- Operating System: Darwin
- Node Version:     v20.4.0
- Nuxt Version:     3.6.5
- Nitro Version:    2.5.2
- Package Manager:  npm@9.7.2
- Builder:          vite
- User Config:      site, devtools, routeRules, modules, nitro, hooks, sitemap
- Runtime Modules:  @nuxtjs/tailwindcss@6.8.0, nuxt-icons@3.2.1, @nuxt/content@2.7.2, nuxt-simple-sitemap@3.1.3
- Build Modules:    -
flexchar commented 1 year ago

Okay. After long tinkering I found that updating my nuxt.config.ts to allowed me to render proper sitemap:

routeRules: {
    '/': { static: true },
    '/blog/**': { static: true },
   '/sitemap.xml': {
            swr: 3600, // this to keep it no older than one hour while having cached version
            headers: { 'Content-Type': 'application/xml' }, // adding headers made it work in browser
        },
},
modules: [
    '@nuxtjs/tailwindcss',
    'nuxt-icons',
    'nuxt-simple-sitemap',
],
nitro: {
    prerender: {
        crawlLinks: true,
        routes: ['/'], // removing from here solved the error but nothing else
    }
},
hooks: {
    async 'nitro:config'(nitroConfig) {
        if (nitroConfig.dev) {
            return;
        }
        const dynamicPosts = (await getBlogPages()).map(p => `/posts/${p.slug}`);

        nitroConfig.prerender?.routes?.push(...dynamicPosts);
    },
},
sitemap: {
    // provide dynamic URLs to be included
    urls: async () => {
        const blogPages = await getBlogPages();
        return blogPages.map(page => ({
            loc: `/posts/${page.slug}`,
            lastmod: new Date(page.created_at),
            changefreq: 'daily',
            priority: 0.8,
        }));
    },
}

I settle with netlify-edge preset for now.

I had multiple issues where before if I didn't prerender it, I'd be able to see it however all updated at dates would be the current time instead of real timestamps of articles.

harlan-zw commented 1 year ago

Thanks, I'll keep this open to add the route rules for the sitemap