How to have sitemap with dynamic routes and keep it up to date?

kimboender1983
0
kimboender1983
commented 2 months ago

I am creating a sitemap with dynamic routes but somehow it is cached or something? The first time it will generate the sitemap with all dynamic routes just fine. But when i add pages in my backend (Storyblok) the sitemap does not update, even when i rebuild my app and start it again. cacheTime and defaults: {changefreq} has no effect also. How can this be achieved? Can anyone point me in the right direction here?

Thanks in advance!

0
NicoPennec
2.0k
NicoPennec
commented 2 months ago

By default, there is a cache time of 15min. see "cacheTime" option

Can you give me more details to reproduce? like nuxt version, build mode (ssr, spa, generate, …) sitemap-module version, sitemap config, …

0
kimboender1983
0
kimboender1983
commented 2 months ago

Hi Nico, thanks for your reply!

Nuxt version is 2.0.0
Sitemap-module version is 2.0.1
Build mode is SSR (universal)

Sitemap config:

// nuxt.config

modules: [
"@nuxtjs/sitemap",
],

sitemap: {
routes() {
return getAppRoutes();
},
path: "/sitemap.xml",
gzip: true,
exclude: ["/blog", "/about"]
},

// getAppRoutes.js

require("dotenv").config();
const axios = require("axios");
const all_links = [];

module.exports = async function getAppRoutes() {
const routes = await axios.get(
https://api.storyblok.com/v1/cdn/links?token=${process.env.STORYBLOK_TOKEN},
{ startswith: "blog/" } ); Object.keys(routes.data.links).forEach(link => { alllinks.push(${routes.data.links[link].real_path});
});

return all_links;

};

I hope this helps, been trying for ages now.

0
NicoPennec
2.0k
NicoPennec
commented 2 months ago

Your sitemap config is good, but your getAppRoutes.js seems wrong.

Try to move the all_links declaration:

require("dotenv").config();
const axios = require("axios");
// const all_links = [];

module.exports.routes = async function getAppRoutes() {
  const all_links = [];

  ...
}

and to test, force the "cacheTime" option to 1ms:

sitemap: {
  cacheTime: 1,
 ...
}
0
Informations
QuestionUnresolved
#c87 - Created 2 months ago