Skip to content

Commit a6dd48d

Browse files
committed
improve README.md
Signed-off-by: Dmitriy Nevzorov <jimmy.lugat@gmail.com>
1 parent 92135ce commit a6dd48d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,66 @@ export const query = graphql`
278278
`;
279279
```
280280

281+
## How to add `sitemap.xml` for all language specific pages
282+
283+
You can use [gatsby-plugin-sitemap](https://www.gatsbyjs.org/packages/gatsby-plugin-sitemap/) to automatically generate a sitemap during build time. You need to customize `query` to fetch only original pages and then `serialize` data to build a sitemap. Here is an example:
284+
285+
```javascript
286+
// In your gatsby-config.js
287+
plugins: [
288+
{
289+
resolve: 'gatsby-plugin-sitemap',
290+
options: {
291+
exclude: ['/**/404', '/**/404.html', '/**/preview', '/**/preview/post'],
292+
query: `
293+
{
294+
site {
295+
siteMetadata {
296+
siteUrl
297+
}
298+
}
299+
allSitePage(filter: {context: {i18n: {routed: {eq: false}}}}) {
300+
edges {
301+
node {
302+
context {
303+
i18n {
304+
defaultLanguage
305+
languages
306+
originalPath
307+
}
308+
}
309+
path
310+
}
311+
}
312+
}
313+
}
314+
`,
315+
serialize: ({site, allSitePage}) => {
316+
return allSitePage.edges.map((edge) => {
317+
const {languages, originalPath, defaultLanguage} = edge.node.context.i18n;
318+
const {siteUrl} = site.siteMetadata;
319+
const url = siteUrl + originalPath;
320+
const links = [
321+
{lang: defaultLanguage, url},
322+
{lang: 'x-default', url}
323+
];
324+
languages.forEach((lang) => {
325+
if (lang === defaultLanguage) return;
326+
links.push({lang, url: `${siteUrl}/${lang}${originalPath}`});
327+
});
328+
return {
329+
url,
330+
changefreq: 'daily',
331+
priority: originalPath === '/' ? 1.0 : 0.7,
332+
links
333+
};
334+
});
335+
}
336+
}
337+
}
338+
];
339+
```
340+
281341
## How to extract translations from pages
282342

283343
You can use [babel-plugin-i18next-extract](https://i18next-extract.netlify.app) automatically extract translations inside `t` function and `Trans` component from you pages and save them in JSON.

0 commit comments

Comments
 (0)