Skip to content

Sitemap

To enable the sitemap, set the sitemap option of the integration config to true or an object:

astro.config.mjs
1
i18n({
2
// ...
3
sitemap: true // or {}
4
})

After you run astro sync, you’ll be able to import a new function from i18n:astro/sitemap on your Astro pages:

1
---
2
import sitemap from "i18n:astro/sitemap"
3
---

This allows you to set specific params at the route level. It’s really interesting to generate i18n friendly sitemaps (with alternates):

1
import { getCollection } from "astro:content";
2
import {
3
getLocalePlaceholder,
4
getDefaultLocalePlaceholder,
5
setDynamicParams,
6
} from "i18n:astro";
7
import sitemap from "i18n:astro/sitemap";
8
import {
9
collectionFilters,
10
generateDynamicParams,
11
handleI18nSlug,
12
} from "@astrolicious/i18n/content-collections";
13
import type { GetStaticPaths } from "astro";
14
15
export const getStaticPaths = (async () => {
16
const locale = getLocalePlaceholder();
17
const defaultLocale = getDefaultLocalePlaceholder();
18
19
const posts = await getCollection("posts", (post) =>
20
collectionFilters.byLocale(post, { locale }),
21
);
22
23
return await Promise.all(
24
posts.map(async (post) => {
25
const equivalentPosts = await getCollection("posts", (p) =>
26
collectionFilters.matchingEntries(p, {
27
currentEntry: post,
28
key: "defaultLocaleVersion",
29
locale,
30
defaultLocale,
31
}),
32
);
33
34
const dynamicParams = equivalentPosts.map((entry) => {
35
const { locale, slug } = handleI18nSlug(entry.slug);
36
37
return {
38
locale,
39
params: {
40
slug,
41
},
42
};
43
});
44
45
sitemap({
46
dynamicParams,
47
});
48
49
return {
50
params: {
51
slug: handleI18nSlug(post.slug).slug,
52
},
53
props: {
54
post,
55
dynamicParams,
56
},
57
};
58
}),
59
);
60
}) satisfies GetStaticPaths;
61
62
const { post, dynamicParams } = Astro.props;
63
64
setDynamicParams(dynamicParams);