Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jan 23, 2024
1 parent 7f5eec7 commit 6007b87
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
47 changes: 34 additions & 13 deletions src/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -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<string> }}): Promise<Metadata> {
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));
Expand All @@ -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)
}
}
}
2 changes: 0 additions & 2 deletions src/app/articles/[number]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 6007b87

Please sign in to comment.