summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/pages/article/[slug].tsx52
-rw-r--r--src/pages/sujet/[slug].tsx9
-rw-r--r--src/pages/thematique/[slug].tsx7
-rw-r--r--src/ts/types/app.ts4
-rw-r--r--src/utils/helpers/format.ts12
5 files changed, 58 insertions, 26 deletions
diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx
index d0ea68a..d00d939 100644
--- a/src/pages/article/[slug].tsx
+++ b/src/pages/article/[slug].tsx
@@ -4,14 +4,17 @@ import { getLayout } from '@components/Layouts/Layout';
import PostFooter from '@components/PostFooter/PostFooter';
import PostHeader from '@components/PostHeader/PostHeader';
import Sidebar from '@components/Sidebar/Sidebar';
+import Spinner from '@components/Spinner/Spinner';
import { Sharing, ToC } from '@components/Widgets';
import { getAllPostsSlug, getPostBySlug } from '@services/graphql/queries';
import styles from '@styles/pages/Page.module.scss';
import { NextPageWithLayout } from '@ts/types/app';
import { ArticleMeta, ArticleProps } from '@ts/types/articles';
import { settings } from '@utils/config';
+import { getFormattedPaths } from '@utils/helpers/format';
import { loadTranslation } from '@utils/helpers/i18n';
import { addPrismClasses, translateCopyButton } from '@utils/helpers/prism';
+import { usePrismTheme } from '@utils/providers/prism';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import Head from 'next/head';
import { useRouter } from 'next/router';
@@ -21,9 +24,32 @@ import { useEffect } from 'react';
import { useIntl } from 'react-intl';
import { Blog, BlogPosting, Graph, WebPage } from 'schema-dts';
import '@utils/plugins/prism-color-scheme';
-import { usePrismTheme } from '@utils/providers/prism';
const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
+ const intl = useIntl();
+ const router = useRouter();
+ const locale = router.locale ? router.locale : settings.locales.defaultLocale;
+
+ useEffect(() => {
+ addPrismClasses();
+ Prism.highlightAll();
+ });
+
+ useEffect(() => {
+ translateCopyButton(locale, intl);
+ }, [intl, locale]);
+
+ const { setCodeBlocks } = usePrismTheme();
+
+ useEffect(() => {
+ const allPre: NodeListOf<HTMLPreElement> = document.querySelectorAll(
+ 'pre[data-prismjs-color-scheme'
+ );
+ setCodeBlocks(allPre);
+ }, [setCodeBlocks, router.asPath]);
+
+ if (router.isFallback) return <Spinner />;
+
const {
author,
comments,
@@ -48,29 +74,8 @@ const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
wordsCount: info.wordsCount,
};
- const intl = useIntl();
- const router = useRouter();
- const locale = router.locale ? router.locale : settings.locales.defaultLocale;
const articleUrl = `${settings.url}${router.asPath}`;
- useEffect(() => {
- addPrismClasses();
- Prism.highlightAll();
- });
-
- useEffect(() => {
- translateCopyButton(locale, intl);
- }, [intl, locale]);
-
- const { setCodeBlocks } = usePrismTheme();
-
- useEffect(() => {
- const allPre: NodeListOf<HTMLPreElement> = document.querySelectorAll(
- 'pre[data-prismjs-color-scheme'
- );
- setCodeBlocks(allPre);
- }, [setCodeBlocks, router.asPath]);
-
const webpageSchema: WebPage = {
'@id': `${articleUrl}`,
'@type': 'WebPage',
@@ -192,9 +197,10 @@ export const getStaticProps: GetStaticProps = async (
export const getStaticPaths: GetStaticPaths = async () => {
const allSlugs = await getAllPostsSlug();
+ const paths = getFormattedPaths(allSlugs);
return {
- paths: allSlugs.map((post) => `/article/${post.slug}`),
+ paths,
fallback: true,
};
};
diff --git a/src/pages/sujet/[slug].tsx b/src/pages/sujet/[slug].tsx
index 3ccacbb..ca7d7cd 100644
--- a/src/pages/sujet/[slug].tsx
+++ b/src/pages/sujet/[slug].tsx
@@ -2,6 +2,7 @@ import { getLayout } from '@components/Layouts/Layout';
import PostHeader from '@components/PostHeader/PostHeader';
import PostPreview from '@components/PostPreview/PostPreview';
import Sidebar from '@components/Sidebar/Sidebar';
+import Spinner from '@components/Spinner/Spinner';
import { RelatedThematics, ToC, TopicsList } from '@components/Widgets';
import { getAllTopicsSlug, getTopicBySlug } from '@services/graphql/queries';
import styles from '@styles/pages/Page.module.scss';
@@ -9,6 +10,7 @@ import { NextPageWithLayout } from '@ts/types/app';
import { ArticleMeta } from '@ts/types/articles';
import { TopicProps, ThematicPreview } from '@ts/types/taxonomies';
import { settings } from '@utils/config';
+import { getFormattedPaths } from '@utils/helpers/format';
import { loadTranslation } from '@utils/helpers/i18n';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import Head from 'next/head';
@@ -23,6 +25,8 @@ const Topic: NextPageWithLayout<TopicProps> = ({ topic }) => {
const relatedThematics = useRef<ThematicPreview[]>([]);
const router = useRouter();
+ if (router.isFallback) return <Spinner />;
+
const updateRelatedThematics = (newThematics: ThematicPreview[]) => {
newThematics.forEach((thematic) => {
const thematicIndex = relatedThematics.current.findIndex(
@@ -182,10 +186,11 @@ export const getStaticProps: GetStaticProps = async (
};
export const getStaticPaths: GetStaticPaths = async () => {
- const allSlugs = await getAllTopicsSlug();
+ const allTopics = await getAllTopicsSlug();
+ const paths = getFormattedPaths(allTopics);
return {
- paths: allSlugs.map((post) => `/sujet/${post.slug}`),
+ paths,
fallback: true,
};
};
diff --git a/src/pages/thematique/[slug].tsx b/src/pages/thematique/[slug].tsx
index dbef25b..df7ff1a 100644
--- a/src/pages/thematique/[slug].tsx
+++ b/src/pages/thematique/[slug].tsx
@@ -2,6 +2,7 @@ import { getLayout } from '@components/Layouts/Layout';
import PostHeader from '@components/PostHeader/PostHeader';
import PostPreview from '@components/PostPreview/PostPreview';
import Sidebar from '@components/Sidebar/Sidebar';
+import Spinner from '@components/Spinner/Spinner';
import { RelatedTopics, ThematicsList, ToC } from '@components/Widgets';
import {
getAllThematicsSlug,
@@ -12,6 +13,7 @@ import { NextPageWithLayout } from '@ts/types/app';
import { ArticleMeta } from '@ts/types/articles';
import { TopicPreview, ThematicProps } from '@ts/types/taxonomies';
import { settings } from '@utils/config';
+import { getFormattedPaths } from '@utils/helpers/format';
import { loadTranslation } from '@utils/helpers/i18n';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import Head from 'next/head';
@@ -26,6 +28,8 @@ const Thematic: NextPageWithLayout<ThematicProps> = ({ thematic }) => {
const relatedTopics = useRef<TopicPreview[]>([]);
const router = useRouter();
+ if (router.isFallback) return <Spinner />;
+
const updateRelatedTopics = (newTopics: TopicPreview[]) => {
newTopics.forEach((topic) => {
const topicIndex = relatedTopics.current.findIndex(
@@ -173,9 +177,10 @@ export const getStaticProps: GetStaticProps = async (
export const getStaticPaths: GetStaticPaths = async () => {
const allSlugs = await getAllThematicsSlug();
+ const paths = getFormattedPaths(allSlugs);
return {
- paths: allSlugs.map((post) => `/thematique/${post.slug}`),
+ paths,
fallback: true,
};
};
diff --git a/src/ts/types/app.ts b/src/ts/types/app.ts
index 636aeed..2c9a291 100644
--- a/src/ts/types/app.ts
+++ b/src/ts/types/app.ts
@@ -100,6 +100,10 @@ export type PageInfo = {
total: number;
};
+export type ParamsSlug = {
+ params: { slug: string };
+};
+
export type Project = {
cover?: string;
id: string;
diff --git a/src/utils/helpers/format.ts b/src/utils/helpers/format.ts
index e45a6a0..817daaf 100644
--- a/src/utils/helpers/format.ts
+++ b/src/utils/helpers/format.ts
@@ -1,3 +1,4 @@
+import { ParamsSlug, Slug } from '@ts/types/app';
import {
Article,
ArticlePreview,
@@ -283,3 +284,14 @@ export const getFormattedDate = (date: string, locale: string) => {
return new Date(date).toLocaleDateString(locale, dateOptions);
};
+
+/**
+ * Convert an array of slugs to an array of params with slug.
+ * @param {Slug} array - An array of object with slug.
+ * @returns {ParamsSlug} An array of params with slug.
+ */
+export const getFormattedPaths = (array: Slug[]): ParamsSlug[] => {
+ return array.map((object) => {
+ return { params: { slug: object.slug } };
+ });
+};