need to fetch and display the articles on your Next.js frontend.
Install Payload CMS Client for Next.js
You need to install Axios or the Payload client to fetch data from Payload CMS.
bash npm install axios
Create a Page for the Knowledge Base
- In your Next.js project, create a new page where you want to display the knowledge base articles, such as
pages/knowledgebase.js. - Use
getServerSidePropsorgetStaticPropsto fetch data from Payload CMS.
Example:
js
Copy code
// pages/knowledgebase.js
import axios from 'axios';
const KnowledgeBasePage = ({ articles }) => {
return (
<div>
<h1>Knowledge Base</h1>
<div>
{articles.map((article) => (
<div key={article.id}>
<h2>{article.title}</h2>
<div dangerouslySetInnerHTML={{ __html: article.content }} />
<h3>Example:</h3>
<pre>{article.example}</pre>
</div>
))}
</div>
</div>
);
};
export async function getServerSideProps() {
// Fetch knowledge base articles from Payload CMS
const response = await axios.get('http://localhost:3000/api/knowledge-base');
const articles = response.data;
return {
props: {
articles,
},
};
}
export default KnowledgeBasePage;
In this example, replace 'http://localhost:3000/api/knowledge-base' with the correct API URL based on your Payload CMS setup. This example uses getServerSideProps to fetch the data on each request, but you can use getStaticProps if you prefer static generation.
API Endpoint in Payload CMS
Payload CMS automatically generates API endpoints based on the collection names. So, in this case, you can access your knowledge base articles via:
http Copy code GET /api/knowledge-base