Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar authored Mar 8, 2023
2 parents a0e8041 + 6911a3f commit b24143f
Show file tree
Hide file tree
Showing 43 changed files with 2,961 additions and 2,100 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow warns and then closes issues and PRs that have had no activity for a specified amount of time.
#
# You can adjust the behavior by modifying this file.
# For more information, see:
# https://github.com/actions/stale
name: Mark stale issues and pull requests

on:
schedule:
- cron: '28 4 * * *'

jobs:
stale:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write

steps:
- uses: actions/stale@v7
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'Closing this issue due to inactivity.'
stale-pr-message: 'Closing this PR due to inactivity. '
stale-issue-label: 'no-issue-activity'
stale-pr-label: 'no-pr-activity'
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
node: ['16', '17', '18', '19']
node: ['16', '18']
runs-on: ${{ matrix.platform }}
steps:
- name: Github Checkout
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ junit.xml
tsconfig.tsbuildinfo
**/public
**/public
.idea
141 changes: 131 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

## Table of contents

- Getting started
- [Getting started](#getting-started)
- [Installation](#installation)
- [Create config file](#create-config-file)
- [Building sitemaps](#building-sitemaps)
- [Custom config file](#custom-config-file)
- [Building sitemaps with pnpm](#building-sitemaps-with-pnpm)
- [Index sitemaps](#index-sitemaps-optional)
- [Splitting large sitemap into multiple files](#splitting-large-sitemap-into-multiple-files)
- [Configuration Options](#configuration-options)
Expand Down Expand Up @@ -59,6 +61,8 @@ Add next-sitemap as your postbuild script
}
```

#### Custom config file

You can also use a custom config file instead of `next-sitemap.config.js`. Just pass `--config <your-config-file>.js` to build command (Example: [custom-config-file](https://github.com/iamvishnusankar/next-sitemap/tree/master/examples/custom-config-file))

```json
Expand All @@ -68,6 +72,15 @@ You can also use a custom config file instead of `next-sitemap.config.js`. Just
}
```

#### Building sitemaps with pnpm

When using pnpm you need to create a `.npmrc` file in the root of your project if you want to use a postbuild step:

```
//.npmrc
enable-pre-post-scripts=true
```

## Index sitemaps (Optional)

📣 From `next-sitemap` v2.x onwards, `sitemap.xml` will be [Index Sitemap](https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps). It will contain urls of all other generated sitemap endpoints.
Expand Down Expand Up @@ -192,6 +205,40 @@ module.exports = {
}
```
## Google News, image and video sitemap
Url set can contain additional sitemaps defined by google. These are
[Google News sitemap](https://developers.google.com/search/docs/advanced/sitemaps/news-sitemap),
[image sitemap](https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps) or
[video sitemap](https://developers.google.com/search/docs/advanced/sitemaps/video-sitemaps).
You can add the values for these sitemaps by updating entry in `transform` function or adding it with
`additionalPaths`. You have to return a sitemap entry in both cases, so it's the best place for updating
the output. This example will add an image and news tag to each entry but IRL you would of course use it with
some condition or within `additionalPaths` result.
```js
/** @type {import('next-sitemap').IConfig} */
const config = {
transform: async (config, path) => {
return {
loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
images: [{ loc: 'https://example.com/image.jpg' }],
news: {
title: 'Article 1',
publicationName: 'Google Scholar',
publicationLanguage: 'en',
date: new Date(),
},
}
},
}

export default config
```
## Full configuration example
Here's an example `next-sitemap.config.js` configuration with all options
Expand Down Expand Up @@ -284,24 +331,54 @@ Sitemap: https://example.com/my-custom-sitemap-3.xml
`next-sitemap` now provides two APIs to generate server side sitemaps. This will help to dynamically generate `index-sitemap`(s) and `sitemap`(s) by sourcing data from CMS or custom source.
- `getServerSideSitemapIndex`: Generates index sitemaps based on urls provided and returns `application/xml` response.
- `getServerSideSitemapIndex`: Generates index sitemaps based on urls provided and returns `application/xml` response. Supports next13+ route.{ts,js} file.
- To continue using inside pages directory, import `getServerSideSitemapIndexLegacy` instead.
- `getServerSideSitemap`: Generates sitemap based on field entires and returns `application/xml` response.
- `getServerSideSitemap`: Generates sitemap based on field entires and returns `application/xml` response. Supports next13+ route.{ts,js} file.
- To continue using inside pages directory, import `getServerSideSitemapLegacy` instead.
### Server side index-sitemaps (getServerSideSitemapIndex)
Here's a sample script to generate index-sitemap on server side. Create `pages/server-sitemap-index.xml/index.tsx` page and add the following content.
Here's a sample script to generate index-sitemap on server side.
<details>
<summary>1. Index sitemap (app directory)</summary>
Create `app/server-sitemap-index.xml/route.ts` file.
```ts
// pages/server-sitemap-index.xml/index.tsx
// app/server-sitemap-index.xml/route.ts
import { getServerSideSitemapIndex } from 'next-sitemap'

export async function GET(request: Request) {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

return getServerSideSitemapIndex([
'https://example.com/path-1.xml',
'https://example.com/path-2.xml',
])
}
```
</details>
<details>
<summary>2. Index sitemap (pages directory) (legacy)</summary>
Create `pages/server-sitemap-index.xml/index.tsx` file.
```ts
// pages/server-sitemap-index.xml/index.tsx
import { getServerSideSitemapIndexLegacy } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

return getServerSideSitemapIndex(ctx, [
return getServerSideSitemapIndexLegacy(ctx, [
'https://example.com/path-1.xml',
'https://example.com/path-2.xml',
])
Expand All @@ -311,6 +388,10 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
export default function SitemapIndex() {}
```
</details>
#### Exclude server index sitemap from robots.txt
Now, `next.js` is serving the dynamic index-sitemap from `http://localhost:3000/server-sitemap-index.xml`.

List the dynamic sitemap page in `robotsTxtOptions.additionalSitemaps` and exclude this path from static sitemap list.
Expand All @@ -333,14 +414,52 @@ module.exports = {

In this way, `next-sitemap` will manage the sitemaps for all your static pages and your dynamic `index-sitemap` will be listed on robots.txt.

---

### 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` file.
```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 @@ -362,13 +481,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
2 changes: 1 addition & 1 deletion azure-pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 3.1$(rev:.r)
name: 4.0$(rev:.r)
trigger:
branches:
include:
Expand Down
15 changes: 15 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## 4.0.x

v4.0.x added support for next13.2+ `appDir` via [Custom Route Handlers](https://nextjs.org/blog/next-13-2#custom-route-handlers)

#### API Changes

Generating dynamic/server-side sitemaps

- `getServerSideSitemapIndex`: Generates index sitemaps based on urls provided and returns application/xml response. Supports next13+ route.{ts,js} file.

- To continue using inside pages directory, import `getServerSideSitemapIndexLegacy` instead.

- `getServerSideSitemap`: Generates sitemap based on field entires and returns application/xml response. Supports next13+ route.{ts,js} file.

- To continue using inside pages directory, import `getServerSideSitemapLegacy` instead.
6 changes: 3 additions & 3 deletions examples/amp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"postbuild": "next-sitemap"
},
"dependencies": {
"@types/react-dom": "^18.0.10",
"next": "^13.1.1",
"@types/react-dom": "^18.0.11",
"next": "^13.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react": "^18.0.28",
"next-sitemap": "*"
}
}
12 changes: 12 additions & 0 deletions examples/app-dir/app/(sitemaps)/server-sitemap-index.xml/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { getServerSideSitemapIndex } from 'next-sitemap'

export async function GET(request: Request) {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

return getServerSideSitemapIndex([
'https://example.com/path-1.xml',
'https://example.com/path-2.xml',
])
}
22 changes: 22 additions & 0 deletions examples/app-dir/app/(sitemaps)/server-sitemap.xml/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
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
},
])
}
10 changes: 6 additions & 4 deletions examples/app-dir/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"postbuild": "next-sitemap"
},
"dependencies": {
"@types/react-dom": "^18.0.10",
"next": "^13.1.1",
"@types/react-dom": "^18.0.11",
"next": "^13.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.25",
"next-sitemap": "*"
"@corex/workspace": "^4.0.37",
"@types/react": "^18.0.27",
"next-sitemap": "*",
"turbo": "^1.8.3"
}
}
8 changes: 4 additions & 4 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "with-next-sitemap",
"name": "basic",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
Expand All @@ -11,13 +11,13 @@
"postbuild": "next-sitemap"
},
"dependencies": {
"@types/react-dom": "^18.0.10",
"next": "^13.1.1",
"@types/react-dom": "^18.0.11",
"next": "^13.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react": "^18.0.27",
"next-sitemap": "*"
}
}
4 changes: 2 additions & 2 deletions examples/basic/pages/server-sitemap-index.xml/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-empty-function */
import { getServerSideSitemapIndex } from 'next-sitemap'
import { getServerSideSitemapIndexLegacy } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
// Method to source urls from cms
// const urls = await fetch('https//example.com/api')

return getServerSideSitemapIndex(ctx, [
return getServerSideSitemapIndexLegacy(ctx, [
'https://example.com/path-1.xml',
'https://example.com/path-2.xml',
])
Expand Down
Loading

0 comments on commit b24143f

Please sign in to comment.