diff --git a/examples/cms-kontent/lib/api.js b/examples/cms-kontent/lib/api.js index de5151087a910..a56eaea502017 100644 --- a/examples/cms-kontent/lib/api.js +++ b/examples/cms-kontent/lib/api.js @@ -43,6 +43,22 @@ export async function getAllPostSlugs() { return postsResponse.items.map((post) => post.slug.value) } +export async function getMorePostsForSlug(slug, preview) { + return client + .items() + .queryConfig({ + usePreviewMode: !!preview, + }) + .type('post') + .orderByDescending('elements.date') + .withParameter('elements.slug[neq]', slug) + .limitParameter(2) + .toPromise() + .then((res) => { + return res.items.map((post) => parsePost(post)) + }) +} + export async function getPostBySlug(slug, preview) { const post = await client .items() @@ -58,7 +74,7 @@ export async function getPostBySlug(slug, preview) { } export async function getAllPosts(preview) { - const postsResponse = await client + return await client .items() .queryConfig({ usePreviewMode: !!preview, @@ -66,6 +82,5 @@ export async function getAllPosts(preview) { .type('post') .orderByDescending('elements.date') .toPromise() - - return postsResponse.items.map((post) => parsePost(post)) + .then((postsResponse) => postsResponse.items.map((post) => parsePost(post))) } diff --git a/examples/cms-kontent/pages/posts/[slug].js b/examples/cms-kontent/pages/posts/[slug].js index 19b87d5f787f3..5695c6dd00e9e 100644 --- a/examples/cms-kontent/pages/posts/[slug].js +++ b/examples/cms-kontent/pages/posts/[slug].js @@ -2,15 +2,21 @@ import { useRouter } from 'next/router' import ErrorPage from 'next/error' import Container from '../../components/container' import PostBody from '../../components/post-body' +import MoreStories from '../../components/more-stories' import Header from '../../components/header' import PostHeader from '../../components/post-header' +import SectionSeparator from '../../components/section-separator' import Layout from '../../components/layout' -import { getPostBySlug, getAllPostSlugs } from '../../lib/api' +import { + getAllPostSlugs, + getPostBySlug, + getMorePostsForSlug, +} from '../../lib/api' import PostTitle from '../../components/post-title' import Head from 'next/head' import { CMS_NAME } from '../../lib/constants' -export default function Post({ post, morePosts, preview }) { +export default function Post({ post, morePosts = [], preview }) { const router = useRouter() if (!router.isFallback && !post?.slug) { return @@ -38,6 +44,8 @@ export default function Post({ post, morePosts, preview }) { /> + + {morePosts.length > 0 && } )} @@ -46,14 +54,16 @@ export default function Post({ post, morePosts, preview }) { } export async function getStaticProps({ params, preview = null }) { - const post = await getPostBySlug(params.slug, preview) - - return { + return await Promise.all([ + getPostBySlug(params.slug, preview), + getMorePostsForSlug(params.slug, preview), + ]).then((values) => ({ props: { - post, + post: values[0], + morePosts: values[1], preview, }, - } + })) } export async function getStaticPaths() {