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

docs: added information on sitemap generation #1145

Merged
merged 6 commits into from May 31, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/content/3.guide/2.displaying/5.sitemaps.md
@@ -0,0 +1,36 @@
---
title: Sitemaps
---

A sitemap file is useful for helping Google to better index your website, ensuring that the content you write can be visible on search results.

This can be created utilising the `sitemap` library, so you'll need to install that which can be done like so:
```bash
yarn add sitemap
Atinux marked this conversation as resolved.
Show resolved Hide resolved
```

## Server Route
We will be utilising the server routes available within Nuxt 3, and to do so you'll need to create the `server/` folder within your websites root directly.

Once this is done, create a `routes/` folder inside this, and add a `sitemap.xml.ts` file, this corresponds to `https://example.com/sitemap.xml`.

You'll need to add the following:

```yaml [server/routes/sitemap.xml.ts]
import {serverQueryContent} from '#content/server';
import {SitemapStream, streamToPromise} from 'sitemap';

export default defineEventHandler(async (event) => {
const articles = await serverQueryContent(event).find();

const sitemap = new SitemapStream({ hostname: 'https://example.com' });
articles.forEach((article) => sitemap.write({ url: article._path, changefreq: 'monthly' }));
sitemap.end();

let data = await streamToPromise(sitemap);
return data;
});

```

Now, once users go to `https://example.com/sitemap.xml`, you'll find the generated XML file with all your blog posts.