From 6007b87159e23c06f13cf230fd895e51b23ae270 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Tue, 23 Jan 2024 23:31:07 +0900 Subject: [PATCH] update --- src/app/[...slug]/page.tsx | 47 +++++++++++++++++++++--------- src/app/articles/[number]/page.tsx | 2 -- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/app/[...slug]/page.tsx b/src/app/[...slug]/page.tsx index 6c8d1e0..0332ecf 100644 --- a/src/app/[...slug]/page.tsx +++ b/src/app/[...slug]/page.tsx @@ -1,14 +1,38 @@ 'use server'; +import { cache } from 'react' import { permanentRedirect } from "next/navigation"; - import { ContentResponse, Content } from '../../models/models'; import { findByPath } from '../../api/content'; import { asInsight } from '../../utils/converters'; import { Renderer } from './renderer'; import { runOrHandleErrorIf, throwIfError } from "../handler"; +import { sluggize } from '../../utils/slug'; +import { generateForArticleOrPage } from '../metadata'; + +// TOOD: rename & move somewhere +interface Resp { + res: Response, + body: ContentResponse +} + +// TODO: move somewhere if possible +const cachedFindByPath = cache(async (path: string) => { + const response = await findByPath(path); + throwIfError(response); + const content = await response.json() as ContentResponse; + return { + res: response, + body: content + } +}); -// TODO: add generateMetadata func +// 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 sluggized = await sluggize(slug, ''); + const content = await cachedFindByPath(sluggized); + return generateForArticleOrPage(sluggized ,content.body); +} export default async function Page(req: any) { return runOrHandleErrorIf(await run(req)); @@ -34,24 +58,21 @@ async function get(req: any) { return permanentRedirect(`/articles${path}`); } - const response: Response = await findByPath(path); - throwIfError(response); - - const contentResponse: ContentResponse = await response.json() as ContentResponse; + const response: Resp = await cachedFindByPath(path); const content: Content = { - title: contentResponse.title, - robotsAttributes: contentResponse.robotsAttributes, - externalResources: contentResponse.externalResources, - content: contentResponse.content, - length: contentResponse.length, - publishedAt: contentResponse.publishedAt + title: response.body.title, + robotsAttributes: response.body.robotsAttributes, + externalResources: response.body.externalResources, + content: response.body.content, + length: response.body.length, + publishedAt: response.body.publishedAt } as Content return { props: { // slug: sluggize(req.params.slug), content: content, - insight: asInsight(response) + insight: asInsight(response.res) } } } diff --git a/src/app/articles/[number]/page.tsx b/src/app/articles/[number]/page.tsx index 83ac71c..4fc9fa5 100644 --- a/src/app/articles/[number]/page.tsx +++ b/src/app/articles/[number]/page.tsx @@ -6,8 +6,6 @@ import { getRequestContext } from '../../../utils/requestContext'; import { Renderer } from './renderer'; import { runOrHandleErrorIf, throwIfError } from "../../handler"; -// TODO: add generateMetadata func - export default async function Page(req: any) { return runOrHandleErrorIf(await run(req)); }