nuxt-modules / sitemap

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

Sitemap Caching – sitemap.xml doesn’t update on Netlify host unless cacheMaxAgeSeconds is set to 0 #288

Open simonhrogers opened 4 months ago

simonhrogers commented 4 months ago

Hello!

So this is a fairly simple one to describe, I’m not sure if I’m missing something?

My first instinct was to blame Netlify, but I realised if it is working perfectly with cacheMaxAgeSeconds: 0 then it must be something more complicated at play.

"@nuxtjs/sitemap": "^5.2.0", "nuxt": "3.12.1",

Here is my nuxt.config.ts setup:

  sitemap: {
    cacheMaxAgeSeconds: 0,
    sources: [
      '/api/sitemap'
    ]
  },

and here is the referenced api/sitemap function – this works perfectly as an endpoint; it updates instantly even on Netlify.

import { createClient } from '@sanity/client'
import { resolveHref } from '../../utils/resolveHref'

const client = createClient({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_DATASET,
  useCdn: false,
});

export default defineEventHandler(async () => {

  const query = `*[_type in ['page', 'exhibition', 'event']] { 
    "slug": slug.current,
    _type,
    _updatedAt,
  }`;

  const documents = await client.fetch(query);  

  const urls = documents.map(doc => ({
    url: resolveHref(doc._type, doc.slug),
    // url: doc.slug,
    lastmod: doc._updatedAt,
    changefreq: 'weekly',
    priority: 0.8
  })) || [];

  return urls;
})

Here is the hosted sitemap:

https://staging--tinworks.netlify.app/sitemap.xml

Thank you for any guidance :-)

harlan-zw commented 4 months ago

Hi, thanks for the details.

This sounds like the default behavior which is to cache the sitemap for 10 minutes (see Sitemap Caching).

Since the sitemap is hit quite often by robots we can reduce the cost of running our site by caching this data. You can usually see the cache status by inspecting the response headers, the URL you provided looks like caching is off though

If this is caching longer than 10 minutes though it sounds like a bug.

simonhrogers commented 4 months ago

Hey Harlan!

Thanks so much for your work on this and sorry for missing that detail – I had read the docs and understood that there was a cache by default.

I checked with a variety of times including unset, 10 minutes set explicitly and then 1 second / 10 seconds to be sure it wasn’t working. In the case of the 10 minute wait I did wait 20 minutes incase I was misunderstanding when the cache was being reset.

Would it be helpful if I set a cache time again and inspected the response headers?

I did also visit the cached Sitemap in private session / different browser incase there might be any issues with my browser config.

S

harlan-zw commented 4 months ago

The only catch may be that the cache is SWR so it will serve old sitemaps after 10 minutes and refresh in the background. Otherwise, it should be resetting.

Any reproduction would be useful to debug further.

harlan-zw commented 3 months ago

So if this is still an issue the only thing I think I can do on my end is set a cache time to 0 seconds by default for Netlify and add a warning if users set a cache time.

Let me know your thoughts, it's a bit strange no one else has reported this though.