Skip to content

Commit

Permalink
feat: Dodano sitemap.xml i robots.txt (#112)
Browse files Browse the repository at this point in the history
* Sitemap

* Add rewrite

* Add robots.txt
  • Loading branch information
Michał Miszczyszyn committed Jan 6, 2021
1 parent e17bdf0 commit 948dace
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
79 changes: 79 additions & 0 deletions api-helpers/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
getArticlesPaginationForGrid,
getArticlesPaginationForList,
getArticlesSlugs,
} from './articles';
import { closeConnection, openConnection } from './db';

type Item = {
readonly path: string;
readonly changefreq: 'daily' | 'monthly' | 'always' | 'hourly' | 'weekly' | 'yearly' | 'never';
readonly priority: number;
};

const staticItems: readonly Item[] = [
{ path: '/', changefreq: 'hourly', priority: 1 },
{ path: '/o-serwisie', changefreq: 'monthly', priority: 0.5 },
{ path: '/zglos-serwis', changefreq: 'monthly', priority: 0.5 },

{ path: '/list', changefreq: 'hourly', priority: 0.9 },
{ path: '/grid', changefreq: 'hourly', priority: 0.9 },
];

export async function getSitemap() {
try {
const prisma = await openConnection();

const [gridCursors, listCursors, articleSlugs] = await Promise.all([
getArticlesPaginationForGrid(prisma),
getArticlesPaginationForList(prisma),
getArticlesSlugs(prisma),
]);

const dynamicItems: readonly Item[] = [
...articleSlugs.map(({ slug }) => ({
path: `/artykuly/${slug}`,
changefreq: 'monthly' as const,
priority: 0.5,
})),
...gridCursors.map((cursor) => ({
path: `/grid/${cursor}`,
changefreq: 'hourly' as const,
priority: 0.4,
})),
...listCursors.map((cursor) => ({
path: `/list/${cursor}`,
changefreq: 'hourly' as const,
priority: 0.4,
})),
];

return sitemapXml([...staticItems, ...dynamicItems]);
} finally {
await closeConnection();
}
}

function itemsToXml(items: ReadonlyArray<Item>) {
return items
.map((item) =>
`
<url>
<loc>https://${process.env.NEXT_PUBLIC_URL!}${item.path}</loc>
<changefreq>${item.changefreq}</changefreq>
<priority>${item.priority}</priority>
</url>
`.trim(),
)
.join('\n');
}

function sitemapXml(items: ReadonlyArray<Item>) {
const xml = itemsToXml(items);
return `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
${xml}
</urlset>
`.trim();
}
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ config.rewrites = async () => {
source: '/feed',
destination: '/api/feed',
},
{
source: '/sitemap.xml',
destination: '/api/sitemap',
},
];
};

Expand Down
21 changes: 21 additions & 0 deletions pages/api/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Boom from '@hapi/boom';

import { withAsync } from '../../api-helpers/api-hofs';
import { getSitemap } from '../../api-helpers/sitemap';
import { REVALIDATION_TIME } from '../[displayStyle]/[cursor]';

export default withAsync(async (req, res) => {
if (req.method !== 'GET') {
throw Boom.notFound();
}

const sitemap = await getSitemap();

res.setHeader('Content-Type', 'application/xml; charset=utf-8');
// revalidate a bit less frequently than usual
res.setHeader('Cache-Control', `s-maxage=${REVALIDATION_TIME * 4}, stale-while-revalidate`);

res.send(sitemap);

return null;
});
4 changes: 4 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Disallow: /admin

Sitemap: https://polskifrontend.pl/sitemap.xml

0 comments on commit 948dace

Please sign in to comment.