Create google sitemap in your sapper/svelte project

I have finished almost with my personal blog site http://roopz.club today. My last step is to create on sitemap file in website.

According to google, sitemap files can be in TXT format too. For more details visit the google site.

I decided to create on .txt with http://roopz.club/map.txt. I have followed the following steps.

<script context="module">
export async function preload({ params, query }) {
const respost = await this.fetch(`http://api.roopz.club/api/posts/`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
}
});
const postparsed = await respost.json();
return { links: postparsed};
}
</script>


<script>
export let links = [];
const fs = require('fs');
fs.writeFile('static/map.txt', '', function(){})
links.forEach(link => {
var url = 'http://roopz.club/'+link.slug+'\n';
fs.appendFile('static/map.txt', url, function (err) {
if (err) throw err;
});
});
</script>

I just read all my posts from the API and written the URL in a plain text file at location static/map.txt. Then submitted my url http://roopz.club/map.txt to the google sitemap submission page.

Enjoy!

  • Roopz © 2020