/** * Blog listing page โ€” fetches published posts from the Brainerce blog API. * * Merchants manage posts in the Brainerce dashboard under Sell โ†’ Blog. * The storefront renders them here. Each post links to /blog/[slug]. * * Posts are only visible when status=PUBLISHED and publishedAt <= NOW(). * Category and tag filtering available via query params. * * One implementation for single- and multi-language stores: `locale` exists only * under the `[locale]` segment and is `undefined` otherwise. Every string comes * from the `seo` / `blog` message namespaces in the request locale; none is a * `locale === 'he'` ternary, which gave every third language English. */ import * as React from 'react'; import type { Metadata } from 'next'; import { Link } from '@/core/lib/navigation'; import { CdnImage as Image } from '@/ui/shared/cdn-image'; import { defaultLocale } from '@/i18n'; import { getServerClient } from '@/core/lib/brainerce.server'; import { localePath } from '@/core/lib/locale-path'; import { getServerNamespace } from '@/core/lib/server-messages'; type PageProps = { params: Promise<{ locale?: string }>; searchParams: Promise<{ category?: string; tag?: string; page?: string }>; }; /** `/blog` plus whichever of category / tag / page apply, in a fixed order. */ function blogHref( locale: string | undefined, query: { category?: string; tag?: string; page?: number } ): string { const params = new URLSearchParams(); if (query.category) params.set('category', query.category); if (query.tag) params.set('tag', query.tag); if (query.page && query.page > 1) params.set('page', String(query.page)); const qs = params.toString(); return `${localePath(locale, '/blog')}${qs ? `?${qs}` : ''}`; } // The canonical is set here because the root layout no longer sets one: a // layout-level canonical is inherited by every page without its own, and this // page used to tell Google it was a duplicate of the homepage. Each page of the // list is its own canonical (`?page=2` is not a copy of page 1). export async function generateMetadata({ params, searchParams }: PageProps): Promise { const { locale } = await params; const { category, tag, page: pageParam } = await searchParams; const seo = await getServerNamespace('seo', locale); return { title: seo.blog ?? '', description: seo.blogDescription ?? '', alternates: { canonical: blogHref(locale, { category, tag, page: Number(pageParam ?? '1') || 1 }), }, }; } export default async function BlogListPage({ params, searchParams }: PageProps) { const { locale } = await params; const { category, tag, page: pageParam } = await searchParams; const page = Number(pageParam ?? '1') || 1; const client = await getServerClient(locale); const [{ data: posts, meta }, seo, strings] = await Promise.all([ client.blog .getPosts({ category, tag, page, limit: 12 }) .catch(() => ({ data: [], meta: { page: 1, limit: 12, total: 0, totalPages: 1 } })), getServerNamespace('seo', locale), getServerNamespace('blog', locale), ]); const dateLocale = locale ?? defaultLocale; return (

{seo.blog}

{posts.length === 0 ? (

{strings.empty}

) : ( <>
{posts.map((post) => { const postHref = localePath(locale, `/blog/${encodeURIComponent(post.slug)}`); return (
{post.coverImageUrl && (
{post.coverImageAlt
)}
{/* Category badge */} {post.category && ( {post.category} )} {/* Title */}

{post.title}

{/* Excerpt */} {post.excerpt && (

{post.excerpt}

)} {/* Meta: author + date */}
{post.author && {post.author}} {post.author && post.publishedAt && ยท} {post.publishedAt && ( )}
{strings.readMore}
); })}
{/* Pagination */} {meta.totalPages > 1 && ( )} )}
); }