Skip to content

Commit

Permalink
Added index sitemap docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed Mar 2, 2023
1 parent 3d78e71 commit 1808008
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Sitemap: https://example.com/my-custom-sitemap-3.xml
Here's a sample script to generate index-sitemap on server side.
<details>
<summary>1. Index sitemaps (app directory)</summary>
<summary>1. Index sitemap (app directory)</summary>
Create `app/server-sitemap-index.xml/route.ts` file.
Expand Down Expand Up @@ -384,12 +384,48 @@ In this way, `next-sitemap` will manage the sitemaps for all your static pages a

### server side sitemap (getServerSideSitemap)

Here's a sample script to generate sitemaps on server side. Create `pages/server-sitemap.xml/index.tsx` page and add the following content.
Here's a sample script to generate sitemaps on server side.
```ts
// pages/server-sitemap.xml/index.tsx
<details>
<summary>1. Sitemaps (app directory)</summary>
Create `app/server-sitemap.xml/route.ts`
```ts
// app/server-sitemap.xml/route.ts
import { getServerSideSitemap } from 'next-sitemap'
export async function GET(request: Request) {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

return getServerSideSitemap([
{
loc: 'https://example.com',
lastmod: new Date().toISOString(),
// changefreq
// priority
},
{
loc: 'https://example.com/dynamic-path-2',
lastmod: new Date().toISOString(),
// changefreq
// priority
},
])
}
```
</details>
<details>
<summary>2. Sitemaps (pages directory) (legacy)</summary>
Create `pages/server-sitemap.xml/index.tsx` file.
```ts
// pages/server-sitemap.xml/index.tsx
import { getServerSideSitemapLegacy } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
Expand All @@ -411,13 +447,15 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
},
]

return getServerSideSitemap(ctx, fields)
return getServerSideSitemapLegacy(ctx, fields)
}

// Default export to prevent next.js errors
export default function Sitemap() {}
```
</details>
Now, `next.js` is serving the dynamic sitemap from `http://localhost:3000/server-sitemap.xml`.

List the dynamic sitemap page in `robotsTxtOptions.additionalSitemaps` and exclude this path from static sitemap list.
Expand Down

0 comments on commit 1808008

Please sign in to comment.