diff --git a/components/parts/loading/Loading.tsx b/components/parts/loading/Loading.tsx new file mode 100644 index 0000000..687b338 --- /dev/null +++ b/components/parts/loading/Loading.tsx @@ -0,0 +1,7 @@ +const Loading = () => { + return ( +
Loading...
+ ) +} + +export default Loading \ No newline at end of file diff --git a/pages/articles/[id].tsx b/pages/articles/[id].tsx index a7db5ae..b4a2250 100644 --- a/pages/articles/[id].tsx +++ b/pages/articles/[id].tsx @@ -7,6 +7,8 @@ import { } from 'next' import React from 'react' import { MicroCmsArticle } from '../../types/microcms/type' +import { useRouter } from 'next/router' +import Loading from '../../components/parts/loading/Loading' export const getStaticProps: GetStaticProps<{ article: MicroCmsArticle }> = async (context: GetStaticPropsContext<{id: string}>) => { const { id } = context.params @@ -29,11 +31,19 @@ export const getStaticPaths: GetStaticPaths = async () => { } } }), - fallback: false, + fallback: true, } } const ArticleDetail = ({ article }: InferGetStaticPropsType) => { + const router = useRouter() + + // If the page is not yet generated, this will be displayed + // initially until getStaticProps() finishes running + if (router.isFallback) { + return Loading + } + const createMarkUp = () => { return { __html: article.body } } diff --git a/pages/index.tsx b/pages/index.tsx index f2dc652..b19866f 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,18 +1,20 @@ import Head from 'next/head' import { apiClient } from '../modules/apiClient' import { - GetStaticPaths, + GetStaticProps, InferGetStaticPropsType } from 'next' import ArticleListItem from '../components/article/ArticleListItem' +import { MicroCmsArticle } from '../types/microcms/type' -export const getStaticProps = async () => { +export const getStaticProps: GetStaticProps<{articles: MicroCmsArticle[]}> = async () => { const articles = (await apiClient.articles.$get()).contents return { props: { articles, }, + revalidate: 1, } }