N
Nuxt6mo ago
Philip

sitemap module wont show dynamic routes, routes are empty

we tried to create on root ebene ( /server/api/sitemap/urls.ts ) the following
import { asSitemapUrl, defineSitemapEventHandler } from '#imports'

export default defineSitemapEventHandler(async () => {
const [pages, sub_pages, instructions] = await Promise.all([
//
$fetch("$cdnurl$/items/pages").then((p) =>
p.data.map((item) => ({
loc: item.slug,
_sitemap: "pages",
}))
),
$fetch(
"$cdnurl$/items/sub_pages?fields=translations.slug"
).then((sp) =>
sp.data.map((item) => ({
loc: `/support/${item.translations[0].slug}`,
_sitemap: "sub_pages",
}))
),
$fetch(
"$cdnurl$/items/Instructions?fields=translations.slug"
).then((s) =>
s.data.map((p) => ({
loc: `/support/${p.translations[0].slug}`,
_sitemap: "instructions",
}))
),
]);

return [...pages, ...sub_pages, ...instructions].map((p) => asSitemapUrl({
return { loc: p.url, lastmod: p.updatedAt };
}));
});
import { asSitemapUrl, defineSitemapEventHandler } from '#imports'

export default defineSitemapEventHandler(async () => {
const [pages, sub_pages, instructions] = await Promise.all([
//
$fetch("$cdnurl$/items/pages").then((p) =>
p.data.map((item) => ({
loc: item.slug,
_sitemap: "pages",
}))
),
$fetch(
"$cdnurl$/items/sub_pages?fields=translations.slug"
).then((sp) =>
sp.data.map((item) => ({
loc: `/support/${item.translations[0].slug}`,
_sitemap: "sub_pages",
}))
),
$fetch(
"$cdnurl$/items/Instructions?fields=translations.slug"
).then((s) =>
s.data.map((p) => ({
loc: `/support/${p.translations[0].slug}`,
_sitemap: "instructions",
}))
),
]);

return [...pages, ...sub_pages, ...instructions].map((p) => asSitemapUrl({
return { loc: p.url, lastmod: p.updatedAt };
}));
});
in the images you can see our nuxt config. its like we wont even get in to the urls.ts file
No description
No description
18 Replies
Philip
PhilipOP6mo ago
this is how the sitemap for "pages" looks like for example
No description
manniL
manniL6mo ago
@Philip a running reproduction would be helpful
Philip
PhilipOP6mo ago
https://support.utm-shop.de/ this is our live webpage do you want this?
Support für Sophos XGS, Central, AP, RED | UTMshop
Professionelle Unterstützung für Ihre Sophos-Produkte: Technischer Support✓ Fehlerbehebung✓ Einrichtungen✓ RMA✓
manniL
manniL6mo ago
no, a full project website (or even a full project source code) is not helpful here In my spare time, I can't go through full projects (that's actually my day job 🙈 )
manniL
manniL6mo ago
Stack Overflow
How to create a Minimal, Reproducible Example - Help Center
Stack Overflow | The World’s Largest Online Community for Developers
Philip
PhilipOP6mo ago
i totally get this, but what can i provide what makes it easier for you to reproduce? you want a small version which you can just pull and reprodurce?
manniL
manniL6mo ago
exactly
Philip
PhilipOP6mo ago
ahh i see
manniL
manniL6mo ago
ideally a stackblitz so it can run online
Philip
PhilipOP6mo ago
i will try to do that thanks
manniL
manniL6mo ago
^that's a good starting point
Philip
PhilipOP6mo ago
its just i dont really wanna publish our cdn or credentials and to reproduce everything will take ma ton of time 😦 Isnt there a better way? i get you, that you dont wanna reproduce all by yourself. But to create a new directus project cdn etc. just to reproduce this issue is a bit to much dont you think so? our project is quite big and there are some dependencies which are building on each other and i think to reproduce this case is almost like building the project again (4-5 month) a small version of it wouldnt reproduce the issue
manniL
manniL6mo ago
What other option do you have in mind? I think a small reproduction just with the sitemap module and some fake data should work, no? and if this works as expected, it is no bug in the module
Philip
PhilipOP6mo ago
So i found the solution... the docu is not accurate... instead of this /server/api/sitemap/urls.ts i use this /server/api/sitemap.ts And use the Module in the nuxt.config like in the picture... the docs are not good
No description
Philip
PhilipOP6mo ago
import { asSitemapUrl, defineSitemapEventHandler } from "#imports";

export default defineSitemapEventHandler(async () => {
const [pages, subpages, instructions] = await Promise.all([
$fetch("CDN URL/items/pages").then((resp) =>
resp.data.map((item) => {
return {
_path: item.slug,
modifiedAt: new Date(),
};
})
),
$fetch("CDN URL/items/sub_pages?fields=*.*.*").then(
(resp) =>
resp.data.map((item) => {
let url = "/support";
url += item.category
? `/${item.category}/${item.translations[0].slug}`
: `/${item.translations[0].slug}`;

return {
_path: url,
modifiedAt: new Date(),
};
})
),
$fetch("CDN URL/items/Instructions?fields=*.*.*").then(
(resp) =>
resp.data.flatMap((item) => {
var url = "/support/" + item.translations[0].slug + "/anleitungen/";

return item.translations[0].instructions.map((instruction) => {
return {
_path: url + instruction.slug,
modifiedAt: new Date(),
};
});
})
),
]);
return [...pages, ...subpages, ...instructions].map((p) =>
asSitemapUrl({
loc: p._path,
lastmod: p.modifiedAt,
})
);
});
import { asSitemapUrl, defineSitemapEventHandler } from "#imports";

export default defineSitemapEventHandler(async () => {
const [pages, subpages, instructions] = await Promise.all([
$fetch("CDN URL/items/pages").then((resp) =>
resp.data.map((item) => {
return {
_path: item.slug,
modifiedAt: new Date(),
};
})
),
$fetch("CDN URL/items/sub_pages?fields=*.*.*").then(
(resp) =>
resp.data.map((item) => {
let url = "/support";
url += item.category
? `/${item.category}/${item.translations[0].slug}`
: `/${item.translations[0].slug}`;

return {
_path: url,
modifiedAt: new Date(),
};
})
),
$fetch("CDN URL/items/Instructions?fields=*.*.*").then(
(resp) =>
resp.data.flatMap((item) => {
var url = "/support/" + item.translations[0].slug + "/anleitungen/";

return item.translations[0].instructions.map((instruction) => {
return {
_path: url + instruction.slug,
modifiedAt: new Date(),
};
});
})
),
]);
return [...pages, ...subpages, ...instructions].map((p) =>
asSitemapUrl({
loc: p._path,
lastmod: p.modifiedAt,
})
);
});
here our rewritten code for the sitemap.ts ✅
manniL
manniL6mo ago
@Philip would you consider sending a PR to improve the doc then? Wäre super 😋
Philip
PhilipOP6mo ago
ich gucke mal wie ich Zeit finde 😅 vllt habe ich doch etwas falsch gemacht und durch Zufall einen workaround gefunden. In einem anderem Projekt werde ich nochmal genau das machen was die docs sagen und sollte sich das nochmal bestätigen, dass es nicht funktioniert mit der Ordnerstruktur, dann mache ich einen PR versprochen.
Want results from more Discord servers?
Add your server