aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/pages/atom.xml.ts22
-rw-r--r--src/pages/atom.xml.tsx24
-rw-r--r--src/pages/feed.json.ts22
-rw-r--r--src/pages/feed.json.tsx24
-rw-r--r--src/pages/feed.xml.ts22
-rw-r--r--src/pages/feed.xml.tsx24
-rw-r--r--src/utils/helpers/rss.test.ts35
-rw-r--r--src/utils/helpers/rss.ts31
8 files changed, 125 insertions, 79 deletions
diff --git a/src/pages/atom.xml.ts b/src/pages/atom.xml.ts
new file mode 100644
index 0000000..21e2e96
--- /dev/null
+++ b/src/pages/atom.xml.ts
@@ -0,0 +1,22 @@
+import type { GetServerSideProps } from 'next';
+import { generateFeed } from '../utils/helpers';
+
+const Feed = () => null;
+
+export const getServerSideProps: GetServerSideProps = async ({ res }) => {
+ const feed = await generateFeed();
+
+ res.setHeader(
+ 'Cache-Control',
+ 'public, s-maxage=600, stale-while-revalidate=59'
+ );
+ res.setHeader('Content-Type', 'text/xml');
+ res.write(feed.atom1());
+ res.end();
+
+ return {
+ props: {},
+ };
+};
+
+export default Feed;
diff --git a/src/pages/atom.xml.tsx b/src/pages/atom.xml.tsx
deleted file mode 100644
index 4515fdd..0000000
--- a/src/pages/atom.xml.tsx
+++ /dev/null
@@ -1,24 +0,0 @@
-import { GetServerSideProps } from 'next';
-import { generateFeed } from '../utils/helpers';
-
-const Feed = () => null;
-
-export const getServerSideProps: GetServerSideProps = async ({ res }) => {
- const feed = await generateFeed();
-
- if (res) {
- res.setHeader(
- 'Cache-Control',
- 'public, s-maxage=600, stale-while-revalidate=59'
- );
- res.setHeader('Content-Type', 'text/xml');
- res.write(`${feed.atom1()}`);
- res.end();
- }
-
- return {
- props: {},
- };
-};
-
-export default Feed;
diff --git a/src/pages/feed.json.ts b/src/pages/feed.json.ts
new file mode 100644
index 0000000..e155022
--- /dev/null
+++ b/src/pages/feed.json.ts
@@ -0,0 +1,22 @@
+import type { GetServerSideProps } from 'next';
+import { generateFeed } from '../utils/helpers';
+
+const Feed = () => null;
+
+export const getServerSideProps: GetServerSideProps = async ({ res }) => {
+ const feed = await generateFeed();
+
+ res.setHeader(
+ 'Cache-Control',
+ 'public, s-maxage=600, stale-while-revalidate=59'
+ );
+ res.setHeader('Content-Type', 'application/json');
+ res.write(feed.json1());
+ res.end();
+
+ return {
+ props: {},
+ };
+};
+
+export default Feed;
diff --git a/src/pages/feed.json.tsx b/src/pages/feed.json.tsx
deleted file mode 100644
index 7c77e19..0000000
--- a/src/pages/feed.json.tsx
+++ /dev/null
@@ -1,24 +0,0 @@
-import { GetServerSideProps } from 'next';
-import { generateFeed } from '../utils/helpers';
-
-const Feed = () => null;
-
-export const getServerSideProps: GetServerSideProps = async ({ res }) => {
- const feed = await generateFeed();
-
- if (res) {
- res.setHeader(
- 'Cache-Control',
- 'public, s-maxage=600, stale-while-revalidate=59'
- );
- res.setHeader('Content-Type', 'application/json');
- res.write(`${feed.json1()}`);
- res.end();
- }
-
- return {
- props: {},
- };
-};
-
-export default Feed;
diff --git a/src/pages/feed.xml.ts b/src/pages/feed.xml.ts
new file mode 100644
index 0000000..2defaa7
--- /dev/null
+++ b/src/pages/feed.xml.ts
@@ -0,0 +1,22 @@
+import type { GetServerSideProps } from 'next';
+import { generateFeed } from '../utils/helpers';
+
+const Feed = () => null;
+
+export const getServerSideProps: GetServerSideProps = async ({ res }) => {
+ const feed = await generateFeed();
+
+ res.setHeader(
+ 'Cache-Control',
+ 'public, s-maxage=600, stale-while-revalidate=59'
+ );
+ res.setHeader('Content-Type', 'text/xml');
+ res.write(feed.rss2());
+ res.end();
+
+ return {
+ props: {},
+ };
+};
+
+export default Feed;
diff --git a/src/pages/feed.xml.tsx b/src/pages/feed.xml.tsx
deleted file mode 100644
index 60dcb21..0000000
--- a/src/pages/feed.xml.tsx
+++ /dev/null
@@ -1,24 +0,0 @@
-import { GetServerSideProps } from 'next';
-import { generateFeed } from '../utils/helpers';
-
-const Feed = () => null;
-
-export const getServerSideProps: GetServerSideProps = async ({ res }) => {
- const feed = await generateFeed();
-
- if (res) {
- res.setHeader(
- 'Cache-Control',
- 'public, s-maxage=600, stale-while-revalidate=59'
- );
- res.setHeader('Content-Type', 'text/xml');
- res.write(`${feed.rss2()}`);
- res.end();
- }
-
- return {
- props: {},
- };
-};
-
-export default Feed;
diff --git a/src/utils/helpers/rss.test.ts b/src/utils/helpers/rss.test.ts
new file mode 100644
index 0000000..b04f6d3
--- /dev/null
+++ b/src/utils/helpers/rss.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from '@jest/globals';
+import { CONFIG } from '../config';
+import { ROUTES } from '../constants';
+import { generateFeed } from './rss';
+
+describe('generate-feed', () => {
+ /* eslint-disable max-statements */
+ it('generates a rss feed', async () => {
+ const feed = await generateFeed();
+
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
+ expect.assertions(10);
+
+ expect(feed.options.author).toStrictEqual({
+ name: CONFIG.name,
+ email: process.env.APP_AUTHOR_EMAIL,
+ link: CONFIG.url,
+ });
+ expect(feed.options.copyright).toBe(
+ `\u00A9 ${CONFIG.copyright.startYear} - ${CONFIG.copyright.endYear} ${CONFIG.name} - CC BY SA`
+ );
+ expect(feed.options.description).toBe(process.env.APP_FEED_DESCRIPTION);
+ expect(feed.options.favicon).toBe(`${CONFIG.url}/favicon.ico`);
+ expect(feed.options.feedLinks).toStrictEqual({
+ json: `${CONFIG.url}${ROUTES.RSS}.json`,
+ atom: `${CONFIG.url}${ROUTES.RSS}/atom`,
+ });
+ expect(feed.options.generator).toBe('Feed & NextJS');
+ expect(feed.options.language).toBe(CONFIG.locales.defaultLocale);
+ expect(feed.options.link).toBe(CONFIG.url);
+ expect(feed.options.title).toBe(`${CONFIG.name} | ${CONFIG.baseline}`);
+ expect(feed.items.length).toBeGreaterThan(0);
+ });
+ /* eslint-enable max-statements */
+});
diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts
index 82fa1ee..9e98a38 100644
--- a/src/utils/helpers/rss.ts
+++ b/src/utils/helpers/rss.ts
@@ -11,7 +11,7 @@ import { ROUTES } from '../constants';
/**
* Retrieve the data for all the articles.
*
- * @returns {Promise<ArticlePreview[]>} - All the articles.
+ * @returns {Promise<ArticlePreview[]>} All the articles.
*/
const getAllArticles = async (): Promise<ArticlePreview[]> => {
const totalPosts = await fetchPostsCount();
@@ -25,7 +25,7 @@ const getAllArticles = async (): Promise<ArticlePreview[]> => {
/**
* Generate a new feed.
*
- * @returns {Promise<Feed>} - The feed.
+ * @returns {Promise<Feed>} The feed.
*/
export const generateFeed = async (): Promise<Feed> => {
const author = {
@@ -33,15 +33,16 @@ export const generateFeed = async (): Promise<Feed> => {
email: process.env.APP_AUTHOR_EMAIL,
link: CONFIG.url,
};
- const copyright = `${CONFIG.name} CC BY SA ${CONFIG.copyright.startYear} - ${CONFIG.copyright.endYear}`;
+ const copyright = `\u00A9 ${CONFIG.copyright.startYear} - ${CONFIG.copyright.endYear} ${CONFIG.name} - CC BY SA`;
const title = `${CONFIG.name} | ${CONFIG.baseline}`;
const feed = new Feed({
author,
copyright,
description: process.env.APP_FEED_DESCRIPTION,
+ favicon: `${CONFIG.url}/favicon.ico`,
feedLinks: {
- json: `${CONFIG.url}${ROUTES.RSS}/json`,
+ json: `${CONFIG.url}${ROUTES.RSS}.json`,
atom: `${CONFIG.url}${ROUTES.RSS}/atom`,
},
generator: 'Feed & NextJS',
@@ -53,16 +54,32 @@ export const generateFeed = async (): Promise<Feed> => {
const articles = await getAllArticles();
- articles.forEach((article) => {
+ for (const article of articles) {
feed.addItem({
+ author: [author],
+ category: article.meta.thematics?.map((thematic) => {
+ return {
+ domain: `${CONFIG.url}${thematic.url}`,
+ name: thematic.name,
+ };
+ }),
content: article.intro,
- date: new Date(article.meta.dates.publication),
+ copyright,
+ date: new Date(
+ article.meta.dates.update ?? article.meta.dates.publication
+ ),
description: article.intro,
id: `${article.id}`,
+ image: article.meta.cover
+ ? {
+ url: article.meta.cover.src,
+ }
+ : undefined,
link: `${CONFIG.url}${ROUTES.ARTICLE}/${article.slug}`,
+ published: new Date(article.meta.dates.publication),
title: article.title,
});
- });
+ }
return feed;
};