Skip to content

Commit 23add20

Browse files
committed
refactor: add fields to search condition
1 parent b4092a1 commit 23add20

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/app/api/list/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export const POST = async (request: NextRequest) => {
2525

2626
const keyword = htmlspecialchars(result.data.keyword.trim())
2727
const articleList =
28-
keyword == '' ? await getArticleList() : await getArticleListByKeyword(keyword)
28+
keyword == ''
29+
? await getArticleList('id,title,overview,svgPath,createdDate')
30+
: await getArticleListByKeyword(keyword, 'id,title,overview,svgPath,createdDate')
2931
if (!articleList) {
3032
return NextResponse.json({ articleListPerPage }, { status: 500 })
3133
}

src/app/articles/[id]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { notFound } from 'next/navigation'
1414

1515
// Dynamic Route使用時にSSGでビルドする
1616
export async function generateStaticParams() {
17-
const response = await getArticleList()
17+
const response = await getArticleList('id')
1818
const articleList = response?.contents
1919

2020
return !articleList

src/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const metadata = {
1111

1212
const Home = async () => {
1313
let hasError = false
14-
const articleList = await getArticleList().catch(() => {
14+
const articleList = await getArticleList('id,title,overview,svgPath,createdDate').catch(() => {
1515
hasError = true
1616
})
1717

src/libs/microcms/client.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ export const getArticle = async (id: string) => {
3333
return article
3434
}
3535

36-
export const getArticleList = async () => {
36+
export const getArticleList = async (filedNames?: string) => {
3737
const articleList = await client
3838
.get<ArticleList>({
3939
endpoint: 'article',
40-
queries: { limit: 100, clearCache: 'true' } as CustomMicroCMSQueries,
40+
queries: {
41+
limit: 100,
42+
clearCache: 'true',
43+
fields: filedNames ?? '',
44+
} as CustomMicroCMSQueries,
4145
})
4246
.then((res) => res)
4347
.catch((err) => {
@@ -46,11 +50,16 @@ export const getArticleList = async () => {
4650
return articleList
4751
}
4852

49-
export const getArticleListByKeyword = async (keyword: string) => {
53+
export const getArticleListByKeyword = async (keyword: string, filedNames?: string) => {
5054
const articleList = await client
5155
.getList<Article>({
5256
endpoint: 'article',
53-
queries: { q: keyword, limit: 100, clearCache: 'true' } as CustomMicroCMSQueries,
57+
queries: {
58+
q: keyword,
59+
limit: 100,
60+
clearCache: 'true',
61+
fields: filedNames ?? '',
62+
} as CustomMicroCMSQueries,
5463
})
5564
.then((res) => res)
5665
.catch((err) => {

0 commit comments

Comments
 (0)