Skip to content

Commit beedbbe

Browse files
committed
add sitemap.xml and feed.xml
sitemap depends on internal sapper realization, which is bad, but works till we need to add another langs
1 parent ba24afb commit beedbbe

File tree

4 files changed

+69
-153
lines changed

4 files changed

+69
-153
lines changed

src/routes/feed.xml.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import posts from './blog/_posts.js';
2+
3+
const sortedPosts = posts.sort( (a, b) => {return new Date(a.data.date.split('.').reverse().toString())<new Date(b.data.date.split('.').reverse().toString())? 1 : -1} )
4+
const render = (posts) => `<?xml version="1.0" encoding="UTF-8" ?>
5+
<rss version="2.0">
6+
<channel>
7+
<title>mailcheck.co</title>
8+
<link>https://www.mailcheck.co/blog</link>
9+
<description>Posts from Mailcheck.co</description>
10+
${posts.map(post => `
11+
<item>
12+
<title>${post.data.title}</title>
13+
<link>https://www.mailcheck.co/blog/${post.data.slug}</link>
14+
<description><![CDATA[${post.data.snippet}]]></description>
15+
<pubDate>${new Date(post.data.date.split('.').reverse().toString()).toISOString()}</pubDate>
16+
</item>
17+
`).join('')}
18+
</channel>
19+
</rss>`;
20+
21+
export function get(req, res, next) {
22+
23+
res.setHeader('Cache-Control', `max-age=0, s-max-age=${600}`); // 10 minutes
24+
res.setHeader('Content-Type', 'application/rss+xml');
25+
const feed = render(sortedPosts);
26+
res.end(feed);
27+
}

src/routes/index.svelte

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
<script context="module">
2+
export function preload({ params, query }) {
3+
return this.fetch(`feed.xml`).then(() => {
4+
return this.fetch('sitemap.xml')
5+
});
6+
}
7+
</script>
8+
19
<script lang="typescript">
210
import Seo from "../components/seo.svelte"
311
import EmailsVerify from "../components/EmailsVerify.svelte";

src/routes/sitemap.xml.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {routes} from '@sapper/internal/manifest-client.mjs';
2+
3+
const render = (pages, categories, posts) => `<?xml version="1.0" encoding="UTF-8" ?>
4+
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
5+
${pages.map(page => `
6+
<url><loc>https://www.mailcheck.co/${page}</loc></url>
7+
`).join('')}
8+
${categories.map(cat => `
9+
<url><loc>https://www.mailcheck.co/blog/c/${cat}/</loc></url>
10+
`).join('')}
11+
${posts.map(post => `
12+
<url><loc>https://www.mailcheck.co/${post}/</loc></url>
13+
`).join('')}
14+
</urlset>
15+
`;
16+
17+
export function get(req, res, next) {
18+
19+
res.setHeader('Cache-Control', `max-age=0, s-max-age=${600}`); // 10 minutes
20+
res.setHeader('Content-Type', 'application/rss+xml');
21+
22+
let data = []
23+
routes.forEach(el => {
24+
data.push( el.pattern.source.toString().replace(/\\/g, '').split('/').filter(el => /[a-zA-Z-]/.exec(el)).join('/'))
25+
})
26+
data = [...new Set(data)]
27+
28+
const pages = data.filter(url=>url.search('/')===-1);
29+
const posts = data.filter(url=>url.search('/')>0);
30+
31+
32+
const feed = render(pages, [], posts);
33+
res.end(feed);
34+
}

static/sitemap.xml

-153
This file was deleted.

0 commit comments

Comments
 (0)