diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-02-13 18:15:07 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-02-13 19:12:22 +0100 |
| commit | 43f1fc3ce52e6ddd397853964c8868f2f4eee41d (patch) | |
| tree | 1c631893cd79c4f2be048e4f7c36a4e2671e5f44 /src | |
| parent | 65bf38646c303399daf0766a10dc7893b78d8301 (diff) | |
chore: improve RSS feed generation time
Diffstat (limited to 'src')
| -rw-r--r-- | src/pages/atom.xml.tsx | 4 | ||||
| -rw-r--r-- | src/pages/feed.json.tsx | 4 | ||||
| -rw-r--r-- | src/pages/feed.xml.tsx | 4 | ||||
| -rw-r--r-- | src/services/graphql/queries.ts | 17 | ||||
| -rw-r--r-- | src/ts/types/app.ts | 9 | ||||
| -rw-r--r-- | src/ts/types/articles.ts | 8 | ||||
| -rw-r--r-- | src/utils/helpers/rss.ts | 15 |
7 files changed, 46 insertions, 15 deletions
diff --git a/src/pages/atom.xml.tsx b/src/pages/atom.xml.tsx index e6908bd..79813d1 100644 --- a/src/pages/atom.xml.tsx +++ b/src/pages/atom.xml.tsx @@ -7,6 +7,10 @@ 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(); diff --git a/src/pages/feed.json.tsx b/src/pages/feed.json.tsx index e113b46..9b2e510 100644 --- a/src/pages/feed.json.tsx +++ b/src/pages/feed.json.tsx @@ -7,6 +7,10 @@ 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(); diff --git a/src/pages/feed.xml.tsx b/src/pages/feed.xml.tsx index 093cab8..24209ac 100644 --- a/src/pages/feed.xml.tsx +++ b/src/pages/feed.xml.tsx @@ -7,6 +7,10 @@ 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(); diff --git a/src/services/graphql/queries.ts b/src/services/graphql/queries.ts index 0817cf6..a9b3141 100644 --- a/src/services/graphql/queries.ts +++ b/src/services/graphql/queries.ts @@ -1,5 +1,5 @@ import { Slug } from '@ts/types/app'; -import { Article, PostBy } from '@ts/types/articles'; +import { Article, PostBy, TotalArticles } from '@ts/types/articles'; import { AllPostsSlug, PostsList, RawPostsList } from '@ts/types/blog'; import { AllTopics, @@ -26,6 +26,21 @@ import { fetchApi } from './api'; // Posts list queries //============================================================================== +export const getPostsTotal = async (): Promise<number> => { + const query = gql` + query PostsTotal { + posts { + pageInfo { + total + } + } + } + `; + + const response = await fetchApi<TotalArticles>(query, null); + return response.posts.pageInfo.total; +}; + export const getPublishedPosts = async ({ first = 10, after = '', diff --git a/src/ts/types/app.ts b/src/ts/types/app.ts index 2c9a291..3edc8a0 100644 --- a/src/ts/types/app.ts +++ b/src/ts/types/app.ts @@ -1,7 +1,7 @@ import { NextPage } from 'next'; import { AppProps } from 'next/app'; import { ReactElement, ReactNode } from 'react'; -import { PostBy } from './articles'; +import { PostBy, TotalArticles } from './articles'; import { AllPostsSlug, RawPostsList } from './blog'; import { CommentData, CreateComment } from './comments'; import { ContactData, SendEmail } from './contact'; @@ -48,10 +48,11 @@ export type RequestType = | AllThematicsSlug | CreateComment | PostBy - | TopicBy - | ThematicBy | RawPostsList - | SendEmail; + | SendEmail + | ThematicBy + | TopicBy + | TotalArticles; //============================================================================== // Globals diff --git a/src/ts/types/articles.ts b/src/ts/types/articles.ts index 5281e7e..75e5a1a 100644 --- a/src/ts/types/articles.ts +++ b/src/ts/types/articles.ts @@ -93,3 +93,11 @@ export type PostBy = { export type ArticleProps = { post: Article; }; + +export type TotalArticles = { + posts: { + pageInfo: { + total: number; + }; + }; +}; diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts index 305c6e7..8a1c801 100644 --- a/src/utils/helpers/rss.ts +++ b/src/utils/helpers/rss.ts @@ -1,20 +1,15 @@ -import { getPublishedPosts } from '@services/graphql/queries'; +import { getPostsTotal, getPublishedPosts } from '@services/graphql/queries'; import { ArticlePreview } from '@ts/types/articles'; import { PostsList } from '@ts/types/blog'; import { settings } from '@utils/config'; import { Feed } from 'feed'; const getAllPosts = async (): Promise<ArticlePreview[]> => { + const totalPosts = await getPostsTotal(); const posts: ArticlePreview[] = []; - 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); + + const postsList: PostsList = await getPublishedPosts({ first: totalPosts }); + posts.push(...postsList.posts); return posts; }; |
