Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foundation Site RSS #68

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/foundation/app/(Site)/blog/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { getInitialSiteDataForSSR } from '@/lib/sanity/sanity-server';
import type { Metadata, ResolvingMetadata } from 'next';
import { generateSanityMetadata } from '@session/sanity-cms/lib/metadata';
import { client } from '@/lib/sanity/sanity.client';
import { generateRssFeed } from '@/lib/rss';

export async function generateMetadata(_: object, parent: ResolvingMetadata): Promise<Metadata> {
const { settings } = await getInitialSiteDataForSSR();

await generateRssFeed();

return settings.blogSeo
? await generateSanityMetadata(client, {
seo: settings.blogSeo,
Expand Down
7 changes: 7 additions & 0 deletions apps/foundation/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRevalidateHandler } from '@session/sanity-cms/api/revalidate';
import { SANITY_SCHEMA_URL } from '@/lib/constants';
import { generateRssFeed } from '@/lib/rss';

const SANITY_REVALIDATE_SECRET = process.env.SANITY_REVALIDATE_SECRET!;
if (!SANITY_REVALIDATE_SECRET) {
Expand All @@ -12,4 +13,10 @@ export const { POST } = createRevalidateHandler({
page: SANITY_SCHEMA_URL.PAGE,
post: SANITY_SCHEMA_URL.POST,
},
rssGenerators: [
{
_type: 'post',
generateRssFeed,
},
],
});
2 changes: 1 addition & 1 deletion apps/foundation/app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
priority: 1,
},
...pages.map((page) => ({
url: `${BASE_URL}${page.slug.current}`,
url: `${BASE_URL}/${page.slug.current}`,
lastModified: page._updatedAt,
changeFrequency: 'weekly' as const,
priority: 0.5,
Expand Down
2 changes: 1 addition & 1 deletion apps/foundation/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Social, SocialLink } from '@session/ui/components/SocialLinkList';

export const BASE_URL = `https://session.foundation/`;
export const BASE_URL = `https://session.foundation`;

export const SOCIALS = {
[Social.Github]: { name: Social.Github, link: 'https://github.com/oxen-io/websites' },
Expand Down
73 changes: 73 additions & 0 deletions apps/foundation/lib/rss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fs from 'fs';
import { generateXMLFromObject } from '@session/sanity-cms/lib/xml';
import { getSiteSettings } from '@session/sanity-cms/queries/getSiteSettings';
import { client } from '@/lib/sanity/sanity.client';
import { BASE_URL, SANITY_SCHEMA_URL } from '@/lib/constants';
import { getPostsWithMetadata } from '@session/sanity-cms/queries/getPosts';

export async function generateRssFeed() {
const posts = await getPostsWithMetadata({ client });

posts.sort((a, b) => {
const dateA = new Date(a.date ?? a._updatedAt);
const dateB = new Date(b.date ?? b._updatedAt);

return dateB.getTime() - dateA.getTime();
});

const date = new Date();

const settings = await getSiteSettings({ client });

const copyright = settings?.copyright;

const title =
settings?.blogSeo?.metaTitle ??
settings?.blogSeo?.openGraph?.title ??
settings?.seo?.metaTitle ??
settings?.seo?.openGraph?.title;

const json = {
rss: {
'@version': '2.0',
'@xmlns:atom': 'http://www.w3.org/2005/Atom',
'@xmlns:content': 'http://purl.org/rss/1.0/modules/content/',
'@xmlns:dc': 'http://purl.org/dc/elements/1.1/',
channel: {
title,
description: 'RSS feed for Session Technology Foundation updates.',
link: BASE_URL,
image: {
url: `${BASE_URL}/images/logo.svg`,
title: `Session Technology Foundation updates`,
link: BASE_URL,
},
generator: 'mini-xml for Node.js',
lastBuildDate: date,
'atom:link': {
'@href': `${BASE_URL}/rss.xml`,
'@rel': 'self',
'@type': 'application/rss+xml',
},
copyright,
item: posts.map((post) => {
const url = `${BASE_URL}${SANITY_SCHEMA_URL.POST}${post.slug.current}`;
return {
title: post.title.trim(),
description: post.summary.trim(),
link: url,
guid: {
'@isPermaLink': 'true',
'#text': url,
},
pubDate: new Date(post.date ?? post._updatedAt),
};
}),
},
},
};

const feed = generateXMLFromObject(json, { pretty: true, indentSpaces: 2 });

fs.writeFileSync('./public/rss.xml', feed);
}
17 changes: 17 additions & 0 deletions apps/foundation/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ const nextConfig = {
images: {
remotePatterns: [{ hostname: 'cdn.sanity.io' }],
},
async rewrites() {
return [
...[
'/rss',
'/feed',
'/feed.xml',
'/blog.xml',
'/blog/rss',
'/blog/feed',
'/blog/rss.xml',
'/blog/feed.xml',
].map((source) => ({
source,
destination: '/rss.xml',
})),
];
},
};

export default withNextIntl(withPlaiceholder(nextConfig));
Loading