From 65a4cb6ce9387c52c4274fddde9320baadbf017f Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 12 Jan 2022 02:02:54 +0100 Subject: chore: generate feed I could loop until hasNextPage is false, but I'm not sure if it is the best solution. So, for now, only the first 100 posts will be added to the file. --- src/utils/helpers/rss.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/utils/helpers/rss.ts (limited to 'src') diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts new file mode 100644 index 0000000..42560cb --- /dev/null +++ b/src/utils/helpers/rss.ts @@ -0,0 +1,54 @@ +import { config } from '@config/website'; +import { getPublishedPosts } from '@services/graphql/queries'; +import { ArticlePreview } from '@ts/types/articles'; +import { Feed } from 'feed'; +import { writeFileSync } from 'fs'; + +const getAllPosts = async (): Promise => { + const posts: ArticlePreview[] = []; + const postsList = await getPublishedPosts({ first: 100 }); + posts.push(...postsList.posts); + + return posts; +}; + +export const generateFeed = async () => { + const websiteUrl = process.env.FRONTEND_URL ? process.env.FRONTEND_URL : ''; + const author = { + name: config.name, + email: process.env.AUTHOR_EMAIL, + link: websiteUrl, + }; + const copyright = `${config.name} CC BY SA ${config.copyright.startYear} - ${config.copyright.endYear}`; + const title = `${config.name} | ${config.baseline}`; + + const feed = new Feed({ + author, + copyright, + description: process.env.FEED_DESCRIPTION, + feedLinks: { + json: `${websiteUrl}/feed/json`, + atom: `${websiteUrl}/feed/atom`, + }, + generator: 'Feed & NextJS', + id: websiteUrl, + language: config.defaultLocale, + link: websiteUrl, + title, + }); + + const posts = await getAllPosts(); + + posts.forEach((post) => { + feed.addItem({ + content: post.intro, + date: new Date(post.dates.publication), + description: post.intro, + id: post.id, + link: `${websiteUrl}/article/${post.slug}`, + title: post.title, + }); + }); + + writeFileSync('./public/feed.xml', feed.rss2(), 'utf8'); +}; -- cgit v1.2.3 From a32f7b146c216f4159f65d5c0b9a7189d31b2f5a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 13 Jan 2022 18:15:41 +0100 Subject: chore: add different feed formats (feed.xml, atom.xml, feed.json) --- src/pages/atom.xml.tsx | 20 ++++++++++++++++++++ src/pages/feed.json.tsx | 20 ++++++++++++++++++++ src/pages/feed.xml.tsx | 20 ++++++++++++++++++++ src/utils/helpers/rss.ts | 3 +-- 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/pages/atom.xml.tsx create mode 100644 src/pages/feed.json.tsx create mode 100644 src/pages/feed.xml.tsx (limited to 'src') diff --git a/src/pages/atom.xml.tsx b/src/pages/atom.xml.tsx new file mode 100644 index 0000000..e6908bd --- /dev/null +++ b/src/pages/atom.xml.tsx @@ -0,0 +1,20 @@ +import { generateFeed } from '@utils/helpers/rss'; +import { GetServerSideProps } from 'next'; + +const Feed = () => null; + +export const getServerSideProps: GetServerSideProps = async ({ res }) => { + const feed = await generateFeed(); + + if (res) { + res.setHeader('Content-Type', 'text/xml'); + res.write(`${feed.atom1()}`); + res.end(); + } + + return { + props: {}, + }; +}; + +export default Feed; diff --git a/src/pages/feed.json.tsx b/src/pages/feed.json.tsx new file mode 100644 index 0000000..e113b46 --- /dev/null +++ b/src/pages/feed.json.tsx @@ -0,0 +1,20 @@ +import { generateFeed } from '@utils/helpers/rss'; +import { GetServerSideProps } from 'next'; + +const Feed = () => null; + +export const getServerSideProps: GetServerSideProps = async ({ res }) => { + const feed = await generateFeed(); + + if (res) { + res.setHeader('Content-Type', 'application/json'); + res.write(`${feed.json1()}`); + res.end(); + } + + return { + props: {}, + }; +}; + +export default Feed; diff --git a/src/pages/feed.xml.tsx b/src/pages/feed.xml.tsx new file mode 100644 index 0000000..093cab8 --- /dev/null +++ b/src/pages/feed.xml.tsx @@ -0,0 +1,20 @@ +import { generateFeed } from '@utils/helpers/rss'; +import { GetServerSideProps } from 'next'; + +const Feed = () => null; + +export const getServerSideProps: GetServerSideProps = async ({ res }) => { + const feed = await generateFeed(); + + if (res) { + res.setHeader('Content-Type', 'text/xml'); + res.write(`${feed.rss2()}`); + res.end(); + } + + return { + props: {}, + }; +}; + +export default Feed; diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts index 42560cb..3e7258a 100644 --- a/src/utils/helpers/rss.ts +++ b/src/utils/helpers/rss.ts @@ -2,7 +2,6 @@ import { config } from '@config/website'; import { getPublishedPosts } from '@services/graphql/queries'; import { ArticlePreview } from '@ts/types/articles'; import { Feed } from 'feed'; -import { writeFileSync } from 'fs'; const getAllPosts = async (): Promise => { const posts: ArticlePreview[] = []; @@ -50,5 +49,5 @@ export const generateFeed = async () => { }); }); - writeFileSync('./public/feed.xml', feed.rss2(), 'utf8'); + return feed; }; -- cgit v1.2.3 From 553225bac64f762ce0a9ac5094ce6e3348f88f41 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 13 Jan 2022 18:23:01 +0100 Subject: chore: add feed formats to document head --- src/components/Layouts/Layout.tsx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/components/Layouts/Layout.tsx b/src/components/Layouts/Layout.tsx index 35e7d27..8a57cf6 100644 --- a/src/components/Layouts/Layout.tsx +++ b/src/components/Layouts/Layout.tsx @@ -4,6 +4,8 @@ import Header from '@components/Header/Header'; import Main from '@components/Main/Main'; import Breadcrumb from '@components/Breadcrumb/Breadcrumb'; import { t } from '@lingui/macro'; +import Head from 'next/head'; +import { config } from '@config/website'; const Layout = ({ children, @@ -14,6 +16,26 @@ const Layout = ({ }) => { return ( <> + + + + + {t`Skip to content`}
{children}
-- cgit v1.2.3 From 320b5782f348d42f6a2bb74a70d4d114525355e4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 13 Jan 2022 18:34:27 +0100 Subject: chore: add all posts from CMS to feed --- src/utils/helpers/rss.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts index 3e7258a..7851ff8 100644 --- a/src/utils/helpers/rss.ts +++ b/src/utils/helpers/rss.ts @@ -1,12 +1,20 @@ import { config } from '@config/website'; import { getPublishedPosts } from '@services/graphql/queries'; import { ArticlePreview } from '@ts/types/articles'; +import { PostsList } from '@ts/types/blog'; import { Feed } from 'feed'; const getAllPosts = async (): Promise => { const posts: ArticlePreview[] = []; - const postsList = await getPublishedPosts({ first: 100 }); - posts.push(...postsList.posts); + let hasNextPage = true; + let after = undefined; + + do { + const postsList: PostsList = await getPublishedPosts({ first: 10, after }); + posts.push(...postsList.posts); + hasNextPage = postsList.pageInfo.hasNextPage; + after = postsList.pageInfo.endCursor; + } while (hasNextPage); return posts; }; -- cgit v1.2.3