Skip to content

Commit

Permalink
add sitemap.xml and feed.xml
Browse files Browse the repository at this point in the history
sitemap depends on internal sapper realization, which is bad, but works till we need to add another langs
  • Loading branch information
nosovk committed Feb 8, 2021
1 parent ba24afb commit beedbbe
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 153 deletions.
27 changes: 27 additions & 0 deletions src/routes/feed.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import posts from './blog/_posts.js';

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} )
const render = (posts) => `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>mailcheck.co</title>
<link>https://www.mailcheck.co/blog</link>
<description>Posts from Mailcheck.co</description>
${posts.map(post => `
<item>
<title>${post.data.title}</title>
<link>https://www.mailcheck.co/blog/${post.data.slug}</link>
<description><![CDATA[${post.data.snippet}]]></description>
<pubDate>${new Date(post.data.date.split('.').reverse().toString()).toISOString()}</pubDate>
</item>
`).join('')}
</channel>
</rss>`;

export function get(req, res, next) {

res.setHeader('Cache-Control', `max-age=0, s-max-age=${600}`); // 10 minutes
res.setHeader('Content-Type', 'application/rss+xml');
const feed = render(sortedPosts);
res.end(feed);
}
8 changes: 8 additions & 0 deletions src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<script context="module">
export function preload({ params, query }) {
return this.fetch(`feed.xml`).then(() => {
return this.fetch('sitemap.xml')
});
}
</script>

<script lang="typescript">
import Seo from "../components/seo.svelte"
import EmailsVerify from "../components/EmailsVerify.svelte";
Expand Down
34 changes: 34 additions & 0 deletions src/routes/sitemap.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {routes} from '@sapper/internal/manifest-client.mjs';

const render = (pages, categories, posts) => `<?xml version="1.0" encoding="UTF-8" ?>
<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">
${pages.map(page => `
<url><loc>https://www.mailcheck.co/${page}</loc></url>
`).join('')}
${categories.map(cat => `
<url><loc>https://www.mailcheck.co/blog/c/${cat}/</loc></url>
`).join('')}
${posts.map(post => `
<url><loc>https://www.mailcheck.co/${post}/</loc></url>
`).join('')}
</urlset>
`;

export function get(req, res, next) {

res.setHeader('Cache-Control', `max-age=0, s-max-age=${600}`); // 10 minutes
res.setHeader('Content-Type', 'application/rss+xml');

let data = []
routes.forEach(el => {
data.push( el.pattern.source.toString().replace(/\\/g, '').split('/').filter(el => /[a-zA-Z-]/.exec(el)).join('/'))
})
data = [...new Set(data)]

const pages = data.filter(url=>url.search('/')===-1);
const posts = data.filter(url=>url.search('/')>0);


const feed = render(pages, [], posts);
res.end(feed);
}
153 changes: 0 additions & 153 deletions static/sitemap.xml

This file was deleted.

0 comments on commit beedbbe

Please sign in to comment.