From 31fcad25674bb0eaf6b0c1b31a9bb59a7eeb79b7 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Tue, 9 Jan 2024 23:55:59 +0900 Subject: [PATCH] update --- src/app/articles/[...slug]/page.tsx | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/app/articles/[...slug]/page.tsx b/src/app/articles/[...slug]/page.tsx index 604af06..b32819e 100644 --- a/src/app/articles/[...slug]/page.tsx +++ b/src/app/articles/[...slug]/page.tsx @@ -1,5 +1,6 @@ 'use server'; +import { cache } from 'react' import { Metadata } from 'next' import { ContentResponse, Content } from '../../../models/models'; import { isIgnoreRequest } from '../../../utils/filterRequests'; @@ -9,11 +10,18 @@ import { Renderer } from './renderer'; import { runOrHandleErrorIf, throwIfError } from "../../handler"; import { defaultRobotsMeta } from '../../../../config'; +const cachedFindByPath = cache(async (path: string) => { + const response = await findByPath(path); + throwIfError(response); + return response; +}); + // TODO: use cache // https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#opting-out-of-data-caching export async function generateMetadata({ params: { slug }}: { params: { slug: Array }}): Promise { - const response: Response = await findByPath("/articles/" + slug.join("/")); - const content = await response.json() as ContentResponse; + const response = await cachedFindByPath("/articles/" + slug.join("/")); + // @ts-ignore + const content = response.json as ContentResponse; const robotsAttributes = content.robotsAttributes === undefined ? defaultRobotsMeta : content.robotsAttributes; return { @@ -42,21 +50,18 @@ export default async function Page(req: any) { async function run(req: any): Promise { const { props } = await get(req); - const c = props.content return ; } async function get(req: any) { - const path = "/articles/" + req.params.slug.join("/"); - // NOTE: avoid send request of images - if (isIgnoreRequest(path)) { + if (isIgnoreRequest(req.params.slug.join("/"))) { throw new Error('Not found', { cause: 404 }); } + const path = "/articles/" + req.params.slug.join("/"); + const response: Response = await cachedFindByPath(path); - const response: Response = await findByPath(path); - throwIfError(response); - + // @ts-ignore const contentResponse: ContentResponse = await response.json() as ContentResponse; const content: Content = { id: contentResponse.id,