Skip to content

Commit

Permalink
fix: return config from queryPostOrPosts (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio authored Aug 18, 2024
1 parent 3a97506 commit 9ea83c3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/forty-planets-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@headstartwp/core": patch
"@headstartwp/next": patch
---

Fix: return config from queryPostOrPosts
1 change: 1 addition & 0 deletions packages/next/src/rsc/data/queries/queryPostOrPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function queryPostOrPosts<
if (result.isSingle && result.data.post) {
return {
...result,
config,
seo: prepareSEOMetadata(result.data.post, config),
};
}
Expand Down
23 changes: 2 additions & 21 deletions projects/wp-nextjs-app/src/app/(single)/[...path]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
import { Metadata } from 'next';
import { removeSourceUrl } from '@headstartwp/core';
import dynamic from 'next/dynamic';
import { FC, Suspense } from 'react';
import { Suspense } from 'react';
import Blocks from '../../../components/Blocks';
import { ServerRelatedPosts } from '../../../components/ServerRelatedPosts';

const ClientRelatedPosts = dynamic(() =>
import('../../../components/RelatedPosts').then((mod) => mod.RelatedPosts),
Expand Down Expand Up @@ -53,26 +54,6 @@ export async function generateMetadata({ params }: HeadstartWPRoute): Promise<Me
return metatada;
}

const ServerRelatedPosts: FC<{ post_id: number; category: string }> = async ({
post_id,
category,
}) => {
const { data } = await queryPosts({
params: { postType: 'post', per_page: 3, category, exclude: [post_id] },
});

return (
<div>
<h2>Related Posts (Streamed from Server)</h2>
<ul>
{data.posts.map((post) => (
<li key={post.id}>{post.title.rendered}</li>
))}
</ul>
</div>
);
};

const Single = async ({ params }: HeadstartWPRoute) => {
const { data, seo, config } = await query({ params });

Expand Down
22 changes: 20 additions & 2 deletions projects/wp-nextjs-app/src/app/blog/[[...path]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { PostEntity, QueriedObject } from '@headstartwp/core';
import { HtmlDecoder } from '@headstartwp/core/react';
import { HeadstartWPRoute, JSONLD, queryPostOrPosts } from '@headstartwp/next/app';
import Link from 'next/link';
import { notFound } from 'next/navigation';
import { Suspense } from 'react';
import Blocks from '../../../components/Blocks';
import { ServerRelatedPosts } from '../../../components/ServerRelatedPosts';

async function query({ params }: HeadstartWPRoute) {
return queryPostOrPosts({
Expand Down Expand Up @@ -62,7 +66,7 @@ const Archive = ({ posts, queriedObject, schema = '' }: ArchiveProps) => {
};

const BlogPage = async ({ params }: HeadstartWPRoute) => {
const { isArchive, isSingle, data, seo } = await query({ params });
const { isArchive, isSingle, data, seo, config } = await query({ params });

if (isArchive && typeof data.posts !== 'undefined') {
return (
Expand All @@ -73,7 +77,21 @@ const BlogPage = async ({ params }: HeadstartWPRoute) => {
if (isSingle && typeof data.post !== 'undefined') {
return (
<article>
<h1>{data.post.title.rendered}</h1>
<h1>
<HtmlDecoder html={data.post.title.rendered ?? ''} />
</h1>

<Blocks html={data.post.content.rendered ?? ''} settings={config} />

{data.post.terms?.category && (
<Suspense fallback="Loading (streaming)">
<ServerRelatedPosts
post_id={data.post.id}
category={data.post.terms.category[0].slug}
/>
</Suspense>
)}

{seo.schema && <JSONLD schema={seo.schema} />}
</article>
);
Expand Down
22 changes: 22 additions & 0 deletions projects/wp-nextjs-app/src/components/ServerRelatedPosts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { queryPosts } from '@headstartwp/next/app';
import { FC } from 'react';

export const ServerRelatedPosts: FC<{ post_id: number; category: string }> = async ({
post_id,
category,
}) => {
const { data } = await queryPosts({
params: { postType: 'post', per_page: 3, category, exclude: [post_id] },
});

return (
<div>
<h2>Related Posts (Streamed from Server)</h2>
<ul>
{data.posts.map((post) => (
<li key={post.id}>{post.title.rendered}</li>
))}
</ul>
</div>
);
};

0 comments on commit 9ea83c3

Please sign in to comment.