import type { Metadata } from 'next';
import { Suspense } from 'react';
import { getServerClient } from '@/core/lib/brainerce.server';
import { getRegionId } from '@/core/lib/region.server';
import { HOME_FEATURED_LIMIT } from '@/core/lib/listing-query';
import { localePath } from '@/core/lib/locale-path';
import { buildMetaDescription } from '@/core/lib/seo';
import { OrganizationJsonLd } from '@/components/seo/organization-json-ld';
import { getCanonicalSiteUrl } from '@/core/lib/site-url';
import { ListingSeedProvider, type HomeSeed } from '@/core/providers/listing-seed';
import { RichTextBlock } from '@/ui/layout/rich-text-block';
import { HomeClient } from '@/ui/home/home-client';
import { ReferralGreeting } from '@/components/referral/referral-greeting';
// `locale` exists only on a multi-language storefront (the `[locale]` segment);
// a single-language one has no params at all.
type Props = { params: Promise<{ locale?: string }> };
// Build homepage metadata server-side so Google, Facebook, WhatsApp etc.
// see a real on first request. Falls back to the
// store name when the merchant hasn't authored a meta description yet.
//
// The canonical lives HERE, not in the root layout: a layout-level canonical is
// inherited by every page that does not set its own, which told Google that the
// FAQ, the blog and every other page were duplicates of the homepage.
export async function generateMetadata({ params }: Props): Promise {
try {
const { locale } = await params;
const storeInfo = await (await getServerClient(locale)).getStoreInfo();
const description = buildMetaDescription(storeInfo.metaDescription) || undefined;
return {
title: storeInfo.name,
description,
alternates: { canonical: localePath(locale, '/') },
// `openGraph` replaces the layout's object wholesale rather than merging
// into it, so `siteName` has to be repeated or the tag disappears.
openGraph: {
title: storeInfo.name,
description,
siteName: storeInfo.name,
type: 'website',
},
twitter: {
card: 'summary_large_image',
title: storeInfo.name,
description,
},
};
} catch {
return {};
}
}
export default async function HomePage({ params }: Props) {
const { locale } = await params;
// Fetch store info server-side so the JSON-LD is in the initial HTML —
// crawlers and AI Overviews see it without needing to execute JavaScript.
// Failure here is non-fatal: the client component re-fetches via the
// StoreProvider so the page still renders.
//
// Also fetch the merchant's homepage intro (RICH_TEXT key='main'). When
// present, it renders as a sanitized HTML block above the product grid —
// typical use is brand introduction, value-prop copy, or a seasonal
// announcement that needs more formatting than the AnnouncementBar.
// When absent, RichTextBlock returns null cleanly so the layout collapses
// to just the product grid.
const client = await getServerClient(locale);
const regionId = await getRegionId();
const [storeInfo, intro, featured] = await Promise.all([
client.getStoreInfo().catch(() => null),
client.content.richText.get('main').catch(() => null),
// The featured grid, fetched here so the initial HTML carries real
// products. Without it the page was a spinner until the browser ran
// JavaScript, which is all a non-rendering crawler ever saw of the
// homepage. A failure is non-fatal: the client hook fetches instead.
client
.getProducts({
limit: HOME_FEATURED_LIMIT,
sortBy: 'createdAt',
sortOrder: 'desc',
...(regionId ? { regionId } : {}),
})
.catch(() => null),
]);
const homeSeed: HomeSeed | null = featured ? { products: featured.data, regionId } : null;
const baseUrl = await getCanonicalSiteUrl();
return (
<>
{storeInfo && baseUrl ? : null}
{/* Referral landing. A referral link is `/?ref=`, so it lands here.
Renders nothing without a valid `?ref=`, and nothing at all on a store
with referrals switched off. The Suspense boundary is required: the
component reads `useSearchParams()`, which would otherwise opt this
whole page out of static rendering. */}
>
);
}