-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Dodano sitemap.xml i robots.txt (#112)
* Sitemap * Add rewrite * Add robots.txt
- Loading branch information
Michał Miszczyszyn
committed
Jan 6, 2021
1 parent
e17bdf0
commit 948dace
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
User-agent: * | ||
Disallow: /admin | ||
|
||
Sitemap: https://polskifrontend.pl/sitemap.xml |