Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
simonhrogers opened this issue Jun 21, 2024 · 4 comments

Comments

@simonhrogers
Copy link

simonhrogers commented Jun 21, 2024

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants