Skip to content

Commit

Permalink
fix: get RSS channel data from blog storyblok SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
nunopolonia committed Jan 10, 2024
1 parent bb9ae29 commit a42d744
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/routes/rss.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { PREVIEW_COOKIE_KEY } from '$lib/constants';
import { getStoryblok } from '$lib/storyblok';
import type { BlogPostStoryblok } from '$types/bloks';

export const GET: RequestHandler = async ({ cookies }) => {
export const GET: RequestHandler = async () => {
const baseUrl = 'https://www.significa.co/blog/';
const storyblok = getStoryblok();

try {
const posts: BlogPostStoryblok[] = await storyblok.getAll('cdn/stories', {
version: cookies.get(PREVIEW_COOKIE_KEY) ? 'draft' : 'published',
version: 'published',
content_type: 'blog-post',
sort_by: 'first_published_at:desc'
});

const { data } = await storyblok.get('cdn/stories/blog');
const { seo_title, seo_description } = data.story.content;

const body = render(baseUrl, seo_title, seo_description, posts);

const headers = {
'Cache-Control': `max-age=0, s-max-age=600`,
'Content-Type': 'application/xml'
};

const body = render(posts);

return new Response(body, { headers });
} catch (err) {
console.error(err);
throw error(404, 'Not found');
}
};

const render = (posts: BlogPostStoryblok[]) =>
const render = (baseUrl: string, title: string, desc: string, posts: BlogPostStoryblok[]) =>
`<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://significa.co/rss.xml" rel="self" type="application/rss+xml" />
<title>Blog - Significa</title>
<link>https://www.significa.co/blog</link>
<description>We write stuff too. Pretty good stuff, may we say. About Design, UX/UI, Development, and, of course, Significa.</description>
<title>${title}</title>
<link>${baseUrl}</link>
<description>${desc}</description>
<ttl>1800</ttl>
${posts
.map(
(post) =>
`<item>
<guid>https://www.significa.co/blog/${post.slug}</guid>
<guid>${baseUrl}${post.slug}</guid>
<title>${post.name}</title>
<link>https://www.significa.co/blog/${post.slug}</link>
<link>${baseUrl}${post.slug}</link>
<description>${post?.content?.seo_description}</description>
<pubDate>${new Date(post.published_at).toUTCString()}</pubDate>
<category>${post.tag_list.join(', ')}</category>
Expand Down

0 comments on commit a42d744

Please sign in to comment.