@newskit-render/sitemap
v0.35.0
Published
Newskit Render - Sitemap package
Downloads
8
Readme
@newskit-render/sitemap
A package used for the generation of different types of sitemaps. You can see an example here: @newskit-render/core
Getting started
Add it as a dependency:
npm install @newskit-render/sitemap
or
yarn add @newskit-render/sitemap
Create sitemap pages
Under your /pages/api
folder create sitemap.ts
and news-sitemap.ts
files.
Implementation
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 <news:name>${publicationName}</news:name>
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 <news:name>${publicationName}</news:name>
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'
}
];
}
}