Newskit Render - Sitemap package
npm install @newskit-render/sitemapA package used for the generation of different types of sitemaps. You can see an example here: @newskit-render/core
```
npm install @newskit-render/sitemap
or
``
yarn add @newskit-render/sitemap
Under your /pages/api folder create sitemap.ts and news-sitemap.ts files.
#### genericSitemap
sitemapXml - generic function which is needed by Next.js to resolve page route
genericSitemap - this function will handle the logic for sitemap.xml and sitemap.xml?query=1 routes. It will receive the following parameters:
context (required) - Next.js context object (only needs res and query from context but its easier to destructor as below)
publisher, domain, firstArticleDate, publicationName (required): Required properties for correctly building the sitemap.xml.
The _domain_ is the domain of your website. The _firstArticleDate_ is the date of the first article you have published, this will set from when the generic sitemap will produce query sitemaps. _publisher_ is the name of the publisher in the NewsKit API, this string will be used for fetching the articles for the correct publication. _publicationName_ is the name of the publication, used inside the sitemap tag.
CustomStaticPageCollection (optional): Provides the possibility to manually add a list of static pages, that do not exist in an API. The format of the static page is in the example.
Below an example.
`
import { NextApiRequest, NextApiResponse } from 'next'
import { genericSitemap, PublisherGroup, CustomStaticPage } from '@newskit-render/sitemap'
const defaultCustomStaticPagesCollection: CustomStaticPage[] = [
{
channel: 'test-custom-page',
},
]
const handler = async (req: NextApiRequest, res: NextApiResponse) =>
genericSitemap({
res,
query: req.query,
publisher: process.env.PUBLISHER as PublisherGroup,
domain: new URL(process.env.SITE_HOST).host,
firstArticleDate: process.env.SITEMAP_FIRST_PUBLICATION_DATE,
publicationName: process.env.SITEMAP_PUBLICATION_NAME,
customStaticPageCollection: defaultCustomStaticPagesCollection,
})
export default handler
`
You will find the env vars in /helm/value-{env}.yaml
example values are:
``
publisher: 'VIRGIN',
domain: 'virginradio.co.uk',
firstArticleDate: '2016-3-1',
publicationName: 'Virgin Radio',
#### newsSitemap
newsSitemap - this function will handle the logic for news-sitemap.xml.ts route. It will receive the following parameters:
context (required) - Next.js context object (only needs res from context but its easier to destructor as below)
publisher, domain, publicationName (required): Required properties for correctly building the sitemap.xml.
The _domain_ is the domain of your website. _publisher_ is the name of the publisher in the NewsKit API, this string will be used for fetching the articles for the correct publication. _publicationName_ is the name of the publication, used inside the sitemap tag.
Below an example.
`
import { NextApiRequest, NextApiResponse } from 'next'
import { newsSitemap, PublisherGroup } from '@newskit-render/sitemap'
const handler = async (req: NextApiRequest, res: NextApiResponse) =>
newsSitemap({
res,
publisher: process.env.PUBLISHER as PublisherGroup,
domain: new URL(process.env.SITE_HOST).host,
publicationName: process.env.SITEMAP_PUBLICATION_NAME,
})
export default handler
`
You will also need to add this to the next.config.js file:
```
module.exports = {
async rewrites() {
return [
{
source: '/sitemap.xml',
destination: '/api/sitemap'
},
{
source: '/news-sitemap.xml',
destination: '/api/news-sitemap'
}
];
}
}