Skip to content

Commit

Permalink
add: fallback:trueにして、記事を増やしたときも再ビルド不要にする (#6)
Browse files Browse the repository at this point in the history
* add: fallback: trueにして新しく追加した記事でも読み込まれるようにした

* fix: 一覧画面のrevalidateを1にした
  • Loading branch information
TeXmeijin committed Aug 9, 2020
1 parent 6b6b178 commit 36aa794
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions components/parts/loading/Loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Loading = () => {
return (
<div>Loading...</div>
)
}

export default Loading
12 changes: 11 additions & 1 deletion pages/articles/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,11 +31,19 @@ export const getStaticPaths: GetStaticPaths = async () => {
}
}
}),
fallback: false,
fallback: true,
}
}

const ArticleDetail = ({ article }: InferGetStaticPropsType<typeof getStaticProps>) => {
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 }
}
Expand Down
6 changes: 4 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -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,
}
}

Expand Down

1 comment on commit 36aa794

@vercel
Copy link

@vercel vercel bot commented on 36aa794 Aug 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.