-
-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: added information on sitemap generation (#1145)
Co-authored-by: Sébastien Chopin <[email protected]>
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: Sitemap | ||
--- | ||
|
||
# Sitemap Generation | ||
|
||
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 --dev sitemap | ||
``` | ||
|
||
## Server Route | ||
|
||
We will be utilising the [server routes](https://v3.nuxtjs.org/guide/features/server-routes) available within Nuxt, and to do so you'll need to create the `server/` directory within your websites root directly. | ||
|
||
Once this is done, create a `routes/` directory inside this, and add a `sitemap.xml.ts` file, this will translate to `/sitemap.xml`. | ||
|
||
You'll need to add the following: | ||
|
||
```ts [server/routes/sitemap.xml.ts] | ||
import { serverQueryContent } from '#content/server' | ||
import { SitemapStream, streamToPromise } from 'sitemap' | ||
|
||
export default defineEventHandler(async (event) => { | ||
// Fetch all documents | ||
const docs = await serverQueryContent(event).find() | ||
const sitemap = new SitemapStream({ | ||
hostname: 'https://example.com' | ||
}) | ||
|
||
for (const doc of docs) { | ||
sitemap.write({ | ||
url: doc._path, | ||
changefreq: 'monthly' | ||
}) | ||
} | ||
sitemap.end() | ||
|
||
return streamToPromise(sitemap) | ||
}) | ||
``` | ||
|
||
Now, once users go to `https://example.com/sitemap.xml`, you'll find the generated XML file with all your pages. | ||
|
||
When using `nuxt generate`, you may want to pre-render the sitemap since the server route won't be able to run on a static hosting. | ||
|
||
You can do this using the `nitro.prerender` option in your `nuxt.config`: | ||
|
||
```ts [nuxt.config.ts] | ||
import { defineNuxtConfig } from 'nuxt' | ||
|
||
export default defineNuxtConfig({ | ||
// ... | ||
nitro: { | ||
prerender: { | ||
routes: ['/sitemap.xml'] | ||
} | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
icon: heroicons-outline:book-open |
File renamed without changes.
File renamed without changes.