create a next js page for an article (so something, that would be on route GET /articles/:article_id). Please include at least title, content, thumbnail, sidebar with related articles, and other stuff you would like to add.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is an example of how you could structure an article page in Next.js, incorporating the features you've mentioned (title, content, thumbnail, sidebar with related articles) along with a few additional elements that are commonly found on article pages (author info, publication date, tags, and a call-to-action for newsletter subscription). This example assumes you're fetching article data from an API or a CMS (Content Management System) like Strapi, Contentful, or even a custom backend.
First, let's create a new page in Next.js for articles. We'll name it [articleId].js inside the pages/articles directory. The [articleId].js naming convention tells Next.js to create a dynamic route where articleId is a parameter.
File Path:pages/articles/[articleId].js
jsx
import Head from 'next/head';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { GetServerSideProps } from 'next';
// Sample API function to fetch article data (replace with your actual API call)
async function fetchArticle(articleId) {
// For demonstration, we're using a placeholder API response.
// In a real app, this would be an actual fetch call to your backend or CMS.
const response = await new Promise((resolve) => {
setTimeout(() => {
resolve({
data: {
id: articleId,
title: `Sample Article ${articleId}`,
content: `<p>This is a sample article content for article ${articleId}. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>`,
thumbnail: `https://picsum.photos/seed/${articleId}/800/600`,
author: {
name: 'John Doe',
avatar: 'https://picsum.photos/100/100',
},
publishedAt: '2023-04-01T12:00:00.000Z',
tags: ['Technology', 'Programming', 'Next.js'],
relatedArticles: [
{ id: '1', title: 'Related Article 1' },
{ id: '2', title: 'Related Article 2' },
{ id: '3', title: 'Related Article 3' },
],
},
});
}, 1000);
});
return response.data;
}
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const articleId = params.articleId;
const article = await fetchArticle(articleId);
if (!article) {
return {
notFound: true,
};
}
return {
props: {
article,
},
};
};
function ArticlePage({ article }) {
const router = useRouter();
if (router.isFallback) {
return <div>Loading...</div>;
}
return (
<>
<Head>
<title>{article.title} | My Blog</title>
<meta name="description" content={`${article.title} - Read this amazing article on My Blog`} />
</Head>
<div className="container mx-auto p-4 pt-6 md:p-6 lg:p-12 xl:p-24">
<div className="flex flex-wrap -mx-4">
<div className="w-full xl:w-8/12 p-4">
<article>
<header>
<h1 className="text-3xl lg:text-5xl font-bold mb-4">{article.title}</h1>
<div className="flex items-center mb-6">
<Image
src={article.author.avatar}
width={40}
height={40}
className="rounded-full"
alt={article.author.name}
/>
<span className="ml-3 text-gray-600">
By {article.author.name} on {new Date(article.publishedAt).toLocaleDateString()}
</span>
</div>
<figure className="mb-6">
<Image
src={article.thumbnail}
width={800}
height={600}
layout="responsive"
objectFit="cover"
className="rounded-lg"
alt={article.title}
/>
</figure>
</header>
<section
className="prose lg:prose-lg mx-auto mb-8"
dangerouslySetInnerHTML={{ __html: article.content }}
/>
<footer className="mb-8">
<h4 className="font-bold mb-2">Tags:</h4>
<ul className="flex flex-wrap gap-2">
{article.tags.map((tag) => (
<li key={tag}>
<span className="bg-gray-200 hover:bg-gray-300 transition duration-300 rounded-full px-3 py-1 text-sm text-gray-600">
{tag}
</span>
</li>
))}
</ul>
</footer>
</article>
{/* Newsletter Call-to-Action */}
<section className="bg-gray-100 p-6 rounded-lg mb-8">
<h3 className="text-lg font-bold mb-2">Enjoyed this article?</h3>
<p className="text-gray-600 mb-4">Subscribe to our newsletter for more content like this!</p>
<form className="flex flex-wrap gap-2">
<input
type="email"
placeholder="Your email address"
className="flex-grow p-2 pl-4 pr-4 rounded-lg border border-gray-300"
/>
<button
type="submit"
className="bg-blue-500 hover:bg-blue-700 transition duration-300 text-white font-bold py-2 px-4 rounded-lg"
>
Subscribe
</button>
</form>
</section>
</div>
{/* Sidebar with Related Articles */}
<aside className="w-full xl:w-4/12 p-4">
<h3 className="text-lg font-bold mb-4">Related Articles</h3>
<ul>
{article.relatedArticles.map((relatedArticle) => (
<li key={relatedArticle.id} className="mb-3">
<a
href={`/articles/${relatedArticle.id}`}
className="text-gray-600 hover:text-gray-900 transition duration-300"
>
{relatedArticle.title}
</a>
</li>
))}
</ul>
</aside>
</div>
</div>
</>
);
}
export default ArticlePage;
Key Features Explained:
Dynamic Routing: The [articleId].js filename enables dynamic routing, where articleId is captured from the URL (/articles/:articleId).
getServerSideProps (GSSP): This method pre-renders the page on each request by fetching the article data server-side. It ensures SEO friendliness and fast initial page loads.
fetchArticle Function: A placeholder for your actual API call to fetch article data. Replace its content with your real data fetching logic.
Responsive Design: Tailwind CSS classes are used for styling, making the layout responsive and accessible across various devices.
Image Optimization: next/image is used for optimizing images (thumbnail and author avatar), enhancing performance by lazy loading and serving appropriately sized images.
SEO: next/head helps in modifying the <head> section of the HTML document for each article page, crucial for SEO with dynamic title and meta description.
DangerouslySetInnerHTML: Used to render the article content as HTML. Be cautious with this attribute to avoid XSS vulnerabilities if content isn't sanitized or comes from an untrusted source.
Related Articles Sidebar: Fetches and displays related articles, enhancing user engagement by encouraging further reading.
Newsletter Subscription: A simple call-to-action (CTA) to capture interest and grow your audience.