Below is an example of how you could create a Next.js page for an article. This example assumes you have a basic understanding of Next.js and that you have a Next.js project set up.
First, let's create a page component for the article. We'll use a dynamic route for this, so we'll create a file named [articleId].js inside the pages/articles directory.
// pages/articles/[articleId].js
import { useRouter } from 'next/router';
import Head from 'next/head';
import { getArticle, getRelatedArticles } from '../../lib/api'; // Assuming you have an API or a method to fetch articles
import ArticleContent from '../../components/ArticleContent';
import RelatedArticles from '../../components/RelatedArticles';
export async function getStaticProps({ params }) {
const article = await getArticle(params.articleId);
const relatedArticles = await getRelatedArticles(params.articleId);
if (!article) {
return {
notFound: true,
};
}
return {
props: {
article,
relatedArticles,
},
};
}
export async function getStaticPaths() {
// If you have a list of article IDs, you can pre-render pages at build time
// For this example, let's assume we're generating paths for a few articles
const articleIds = ['1', '2', '3']; // Fetch or define your article IDs here
const paths = articleIds.map((id) => ({
params: { articleId: id.toString() },
}));
return { paths, fallback: 'blocking' }; // Use 'blocking' for SSR if the page isn't pre-rendered
}
const ArticlePage = ({ article, relatedArticles }) => {
const router = useRouter();
if (router.isFallback) {
return <div>Loading...</div>;
}
return (
<div className="article-page">
<Head>
<title>{article.title}</title>
<meta name="description" content={article.excerpt} />
</Head>
<div className="main-content">
<h1>{article.title}</h1>
{article.thumbnail && (
<img src={article.thumbnail} alt={article.title} className="thumbnail" />
)}
<ArticleContent content={article.content} />
</div>
<aside className="sidebar">
<h2>Related Articles</h2>
<RelatedArticles articles={relatedArticles} />
</aside>
<style jsx>{`
.article-page {
display: flex;
max-width: 1200px;
margin: auto;
padding: 20px;
}
.main-content {
flex: 3;
padding-right: 20px;
}
.sidebar {
flex: 1;
}
.thumbnail {
width: 100%;
height: auto;
margin-bottom: 20px;
}
`}</style>
</div>
);
};
export default ArticlePage;
For the ArticleContent component, you might want to create a simple component that renders the article content. This could be as straightforward as:
// components/ArticleContent.js
const ArticleContent = ({ content }) => {
return (
<div dangerouslySetInnerHTML={{ __html: content }} />
);
};
export default ArticleContent;
And for RelatedArticles, you could have:
// components/RelatedArticles.js
import Link from 'next/link';
const RelatedArticles = ({ articles }) => {
return (
<ul>
{articles.map((article) => (
<li key={article.id}>
<Link href={`/articles/${article.id}`}>
<a>{article.title}</a>
</Link>
</li>
))}
</ul>
);
};
export default RelatedArticles;
For the getArticle and getRelatedArticles functions, you would need to implement API calls or database queries to fetch the required data. Here's a simple example using a hypothetical API:
// lib/api.js
export async function getArticle(articleId) {
const response = await fetch(`https://yourapi.com/articles/${articleId}`);
if (!response.ok) return null;
return response.json();
}
export async function getRelatedArticles(articleId) {
const response = await fetch(`https://yourapi.com/articles/${articleId}/related`);
if (!response.ok) return [];
return response.json();
}
This example provides a basic structure for an article page with a sidebar containing related articles. You can expand upon this by adding more features, styles, and interactivity as needed.