Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(storefront): point hreflang alternates at 200 locale URLs (#1356)

Viktor Holik014d92d6c9e3
diff --git a/apps/storefront/src/app/[locale]/(auth)/forgot-password/page.tsx b/apps/storefront/src/app/[locale]/(auth)/forgot-password/page.tsx
index 8f587156a..d000b4ee2 100644
--- a/apps/storefront/src/app/[locale]/(auth)/forgot-password/page.tsx
+++ b/apps/storefront/src/app/[locale]/(auth)/forgot-password/page.tsx
@@ -4,6 +4,7 @@ import { Metadata } from "next"
export const metadata: Metadata = {
title: "Forgot password",
description: "Create a new password",
+ robots: { index: false, follow: false },
}
export default function ForgotPasswordPage() {
diff --git a/apps/storefront/src/app/[locale]/(auth)/login/page.tsx b/apps/storefront/src/app/[locale]/(auth)/login/page.tsx
index 7b83490f5..7c84c4999 100644
--- a/apps/storefront/src/app/[locale]/(auth)/login/page.tsx
+++ b/apps/storefront/src/app/[locale]/(auth)/login/page.tsx
@@ -1,7 +1,13 @@
import { LoginForm } from "@/components/molecules"
import { retrieveCustomer } from "@/lib/data/customer"
+import type { Metadata } from "next"
import { redirect } from "next/navigation"
+export const metadata: Metadata = {
+ title: "Login",
+ robots: { index: false, follow: false },
+}
+
export default async function LoginPage() {
const user = await retrieveCustomer()
diff --git a/apps/storefront/src/app/[locale]/(auth)/register/page.tsx b/apps/storefront/src/app/[locale]/(auth)/register/page.tsx
index 8122c3092..ebc5c03e6 100644
--- a/apps/storefront/src/app/[locale]/(auth)/register/page.tsx
+++ b/apps/storefront/src/app/[locale]/(auth)/register/page.tsx
@@ -1,7 +1,13 @@
import { RegisterForm } from "@/components/molecules"
import { retrieveCustomer } from "@/lib/data/customer"
+import type { Metadata } from "next"
import { redirect } from "next/navigation"
+export const metadata: Metadata = {
+ title: "Register",
+ robots: { index: false, follow: false },
+}
+
export default async function RegisterPage() {
const user = await retrieveCustomer()
diff --git a/apps/storefront/src/app/[locale]/(checkout)/checkout/page.tsx b/apps/storefront/src/app/[locale]/(checkout)/checkout/page.tsx
index 1089b57bc..43105818e 100644
--- a/apps/storefront/src/app/[locale]/(checkout)/checkout/page.tsx
+++ b/apps/storefront/src/app/[locale]/(checkout)/checkout/page.tsx
@@ -15,7 +15,8 @@ import { listCartPaymentMethods } from '@/lib/data/payment';
export const metadata: Metadata = {
title: 'Checkout',
- description: 'My cart page - Checkout'
+ description: 'My cart page - Checkout',
+ robots: { index: false, follow: false }
};
export default async function CheckoutPage({}) {
diff --git a/apps/storefront/src/app/[locale]/(main)/cart/page.tsx b/apps/storefront/src/app/[locale]/(main)/cart/page.tsx
index 6789b1225..0bc751dff 100644
--- a/apps/storefront/src/app/[locale]/(main)/cart/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/cart/page.tsx
@@ -5,6 +5,7 @@ import { Suspense } from 'react';
export const metadata: Metadata = {
title: 'Cart',
description: 'My cart page',
+ robots: { index: false, follow: false },
};
export default function CartPage({}) {
diff --git a/apps/storefront/src/app/[locale]/(main)/categories/[category]/page.tsx b/apps/storefront/src/app/[locale]/(main)/categories/[category]/page.tsx
index 592e2f042..cad41b012 100644
--- a/apps/storefront/src/app/[locale]/(main)/categories/[category]/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/categories/[category]/page.tsx
@@ -11,7 +11,10 @@ import { headers } from "next/headers"
import Script from "next/script"
import { listRegions } from "@/lib/data/regions"
import { listProducts } from "@/lib/data/products"
-import { toHreflang } from "@/lib/helpers/hreflang"
+import {
+ buildHreflangAlternates,
+ getStorefrontLocales,
+} from "@/lib/helpers/hreflang"
export const revalidate = 60
@@ -31,39 +34,31 @@ export async function generateMetadata({
return {}
}
- let languages: Record<string, string> = {}
+ let locales: string[] = []
try {
- const regions = await listRegions()
- const locales = Array.from(
- new Set(
- (regions || []).flatMap((r) => r.countries?.map((c) => c.iso_2) || [])
- )
- ) as string[]
- languages = locales.reduce<Record<string, string>>((acc, code) => {
- acc[toHreflang(code)] = `${baseUrl}/${code}/categories/${categoryHandle}`
- return acc
- }, {})
+ locales = getStorefrontLocales(await listRegions())
} catch {
- languages = {
- [toHreflang(locale)]: `${baseUrl}/${locale}/categories/${categoryHandle}`,
- }
+ locales = [locale]
}
+ const { canonical, languages } = buildHreflangAlternates({
+ baseUrl,
+ path: `/categories/${categoryHandle}`,
+ locale,
+ locales,
+ })
+
const title = `${cat.name} Category`
const description = `${cat.name} Category - ${
process.env.NEXT_PUBLIC_SITE_NAME || "Storefront"
}`
- const canonical = `${baseUrl}/${locale}/categories/${categoryHandle}`
return {
title,
description,
alternates: {
canonical,
- languages: {
- ...languages,
- "x-default": `${baseUrl}/categories/${categoryHandle}`,
- },
+ languages,
},
robots: { index: true, follow: true },
openGraph: {
diff --git a/apps/storefront/src/app/[locale]/(main)/categories/page.tsx b/apps/storefront/src/app/[locale]/(main)/categories/page.tsx
index 0e9fe069d..49df47564 100644
--- a/apps/storefront/src/app/[locale]/(main)/categories/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/categories/page.tsx
@@ -9,7 +9,10 @@ import type { Metadata } from "next"
import Script from "next/script"
import { listRegions } from "@/lib/data/regions"
import { listProducts } from "@/lib/data/products"
-import { toHreflang } from "@/lib/helpers/hreflang"
+import {
+ buildHreflangAlternates,
+ getStorefrontLocales,
+} from "@/lib/helpers/hreflang"
export const revalidate = 60
@@ -24,34 +27,31 @@ export async function generateMetadata({
const protocol = headersList.get("x-forwarded-proto") || "https"
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || `${protocol}://${host}`
- let languages: Record<string, string> = {}
+ let locales: string[] = []
try {
- const regions = await listRegions()
- const locales = Array.from(
- new Set(
- (regions || []).flatMap((r) => r.countries?.map((c) => c.iso_2) || [])
- )
- ) as string[]
- languages = locales.reduce<Record<string, string>>((acc, code) => {
- acc[toHreflang(code)] = `${baseUrl}/${code}/categories`
- return acc
- }, {})
+ locales = getStorefrontLocales(await listRegions())
} catch {
- languages = { [toHreflang(locale)]: `${baseUrl}/${locale}/categories` }
+ locales = [locale]
}
+ const { canonical, languages } = buildHreflangAlternates({
+ baseUrl,
+ path: "/categories",
+ locale,
+ locales,
+ })
+
const title = "All Products"
const description = `Browse all products on ${
process.env.NEXT_PUBLIC_SITE_NAME || "our store"
}`
- const canonical = `${baseUrl}/${locale}/categories`
return {
title,
description,
alternates: {
canonical,
- languages: { ...languages, "x-default": `${baseUrl}/categories` },
+ languages,
},
robots: { index: true, follow: true },
openGraph: {
diff --git a/apps/storefront/src/app/[locale]/(main)/collections/[handle]/page.tsx b/apps/storefront/src/app/[locale]/(main)/collections/[handle]/page.tsx
index 438f8efc2..413e6a4dd 100644
--- a/apps/storefront/src/app/[locale]/(main)/collections/[handle]/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/collections/[handle]/page.tsx
@@ -3,9 +3,70 @@ import { Breadcrumbs } from "@/components/atoms"
import { ProductListingSkeleton } from "@/components/organisms/ProductListingSkeleton/ProductListingSkeleton"
import { SearchProductsListing, ProductListing } from "@/components/sections"
import { getCollectionByHandle } from "@/lib/data/collections"
+import { listRegions } from "@/lib/data/regions"
+import {
+ buildHreflangAlternates,
+ getStorefrontLocales,
+} from "@/lib/helpers/hreflang"
import isBot from "@/lib/helpers/isBot"
+import type { Metadata } from "next"
+import { headers } from "next/headers"
import { Suspense } from "react"
+export async function generateMetadata({
+ params,
+}: {
+ params: Promise<{ handle: string; locale: string }>
+}): Promise<Metadata> {
+ const { handle, locale } = await params
+
+ const collection = await getCollectionByHandle(handle)
+ if (!collection) {
+ return {}
+ }
+
+ const headersList = await headers()
+ const host = headersList.get("host")
+ const protocol = headersList.get("x-forwarded-proto") || "https"
+ const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || `${protocol}://${host}`
+
+ let locales: string[] = []
+ try {
+ locales = getStorefrontLocales(await listRegions())
+ } catch {
+ locales = [locale]
+ }
+
+ const { canonical, languages } = buildHreflangAlternates({
+ baseUrl,
+ path: `/collections/${handle}`,
+ locale,
+ locales,
+ })
+
+ const title = collection.title
+ const description = `${collection.title} - ${
+ process.env.NEXT_PUBLIC_SITE_NAME || "Storefront"
+ }`
+
+ return {
+ title,
+ description,
+ robots: { index: true, follow: true },
+ alternates: {
+ canonical,
+ languages,
+ },
+ openGraph: {
+ title: `${title} | ${process.env.NEXT_PUBLIC_SITE_NAME || "Storefront"}`,
+ description,
+ url: canonical,
+ siteName: process.env.NEXT_PUBLIC_SITE_NAME || "Storefront",
+ type: "website",
+ },
+ }
+}
+
const SingleCollectionsPage = async ({
params,
}: {
diff --git a/apps/storefront/src/app/[locale]/(main)/page.tsx b/apps/storefront/src/app/[locale]/(main)/page.tsx
index 356a1f071..6aa3be41b 100644
--- a/apps/storefront/src/app/[locale]/(main)/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/page.tsx
@@ -11,7 +11,11 @@ import type { Metadata } from "next"
import { headers } from "next/headers"
import Script from "next/script"
import { listRegions } from "@/lib/data/regions"
-import { toHreflang } from "@/lib/helpers/hreflang"
+import {
+ buildHreflangAlternates,
+ getStorefrontLocales,
+ toHreflang,
+} from "@/lib/helpers/hreflang"
export async function generateMetadata({
params,
@@ -25,32 +29,24 @@ export async function generateMetadata({
const protocol = headersList.get("x-forwarded-proto") || "https"
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || `${protocol}://${host}`
- let languages: Record<string, string> = {}
+ let locales: string[] = []
try {
- const regions = await listRegions()
- const locales = Array.from(
- new Set(
- (regions || [])
- .map((r) => r.countries?.map((c) => c.iso_2) || [])
- .flat()
- .filter(Boolean)
- )
- ) as string[]
-
- languages = locales.reduce<Record<string, string>>((acc, code) => {
- const hrefLang = toHreflang(code)
- acc[hrefLang] = `${baseUrl}/${code}`
- return acc
- }, {})
+ locales = getStorefrontLocales(await listRegions())
} catch {
- languages = { [toHreflang(locale)]: `${baseUrl}/${locale}` }
+ locales = [locale]
}
+ const { canonical, languages } = buildHreflangAlternates({
+ baseUrl,
+ path: "",
+ locale,
+ locales,
+ })
+
const title = "Home"
const description =
"Welcome to Mercur B2C Demo! Create a modern marketplace that you own and customize in every aspect with high-performance, fully customizable storefront."
const ogImage = "/B2C_Storefront_Open_Graph.png"
- const canonical = `${baseUrl}/${locale}`
return {
title,
@@ -68,10 +64,7 @@ export async function generateMetadata({
},
alternates: {
canonical,
- languages: {
- ...languages,
- "x-default": baseUrl,
- },
+ languages,
},
openGraph: {
title: `${title} | ${
diff --git a/apps/storefront/src/app/[locale]/(main)/products/[handle]/page.tsx b/apps/storefront/src/app/[locale]/(main)/products/[handle]/page.tsx
index 4d7d5bc88..253a38a3b 100644
--- a/apps/storefront/src/app/[locale]/(main)/products/[handle]/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/products/[handle]/page.tsx
@@ -15,7 +15,7 @@ export async function generateMetadata({
queryParams: { handle: [handle], limit: 1 },
}).then(({ response }) => response.products[0])
- return generateProductMetadata(prod)
+ return generateProductMetadata(prod, locale)
}
export default async function ProductPage({
diff --git a/apps/storefront/src/app/[locale]/(main)/sellers/[handle]/page.tsx b/apps/storefront/src/app/[locale]/(main)/sellers/[handle]/page.tsx
index eefea9e3e..f18d583ab 100644
--- a/apps/storefront/src/app/[locale]/(main)/sellers/[handle]/page.tsx
+++ b/apps/storefront/src/app/[locale]/(main)/sellers/[handle]/page.tsx
@@ -1,8 +1,69 @@
import { SellerTabs } from "@/components/organisms"
import { SellerPageHeader } from "@/components/sections"
import { retrieveCustomer } from "@/lib/data/customer"
+import { listRegions } from "@/lib/data/regions"
import { getSellerByHandle } from "@/lib/data/seller"
+import {
+ buildHreflangAlternates,
+ getStorefrontLocales,
+} from "@/lib/helpers/hreflang"
import { SellerDTO } from "@mercurjs/types"
+import type { Metadata } from "next"
+import { headers } from "next/headers"
+
+export async function generateMetadata({
+ params,
+}: {
+ params: Promise<{ handle: string; locale: string }>
+}): Promise<Metadata> {
+ const { handle, locale } = await params
+
+ const seller = (await getSellerByHandle(handle)) as SellerDTO
+ if (!seller) {
+ return {}
+ }
+
+ const headersList = await headers()
+ const host = headersList.get("host")
+ const protocol = headersList.get("x-forwarded-proto") || "https"
+ const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || `${protocol}://${host}`
+
+ let locales: string[] = []
+ try {
+ locales = getStorefrontLocales(await listRegions())
+ } catch {
+ locales = [locale]
+ }
+
+ const { canonical, languages } = buildHreflangAlternates({
+ baseUrl,
+ path: `/sellers/${handle}`,
+ locale,
+ locales,
+ })
+
+ const title = seller.name
+ const description = `${seller.name} - ${
+ process.env.NEXT_PUBLIC_SITE_NAME || "Storefront"
+ }`
+
+ return {
+ title,
+ description,
+ robots: { index: true, follow: true },
+ alternates: {
+ canonical,
+ languages,
+ },
+ openGraph: {
+ title: `${title} | ${process.env.NEXT_PUBLIC_SITE_NAME || "Storefront"}`,
+ description,
+ url: canonical,
+ siteName: process.env.NEXT_PUBLIC_SITE_NAME || "Storefront",
+ type: "website",
+ },
+ }
+}
export default async function SellerPage({
params,
diff --git a/apps/storefront/src/app/[locale]/(reset-password)/reset-password/page.tsx b/apps/storefront/src/app/[locale]/(reset-password)/reset-password/page.tsx
index 68bc3f588..d084f27e2 100644
--- a/apps/storefront/src/app/[locale]/(reset-password)/reset-password/page.tsx
+++ b/apps/storefront/src/app/[locale]/(reset-password)/reset-password/page.tsx
@@ -1,5 +1,11 @@
import { Card } from "@/components/atoms"
import { ProfilePasswordForm } from "@/components/molecules/ProfilePasswordForm/ProfilePasswordForm"
+import type { Metadata } from "next"
+
+export const metadata: Metadata = {
+ title: "Reset password",
+ robots: { index: false, follow: false },
+}
export default async function ResetPasswordPage({
searchParams,
diff --git a/apps/storefront/src/app/layout.tsx b/apps/storefront/src/app/layout.tsx
index 0489d258b..12669774f 100644
--- a/apps/storefront/src/app/layout.tsx
+++ b/apps/storefront/src/app/layout.tsx
@@ -26,12 +26,7 @@ export const metadata: Metadata = {
},
description:
process.env.NEXT_PUBLIC_SITE_DESCRIPTION || 'Mercur B2C Demo - Marketplace Storefront',
- metadataBase: new URL(process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'),
- alternates: {
- languages: {
- 'x-default': process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000'
- }
- }
+ metadataBase: new URL(process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000')
};
export default async function RootLayout({
diff --git a/apps/storefront/src/lib/helpers/hreflang.ts b/apps/storefront/src/lib/helpers/hreflang.ts
index a7c392a80..f8732e8ef 100644
--- a/apps/storefront/src/lib/helpers/hreflang.ts
+++ b/apps/storefront/src/lib/helpers/hreflang.ts
@@ -30,3 +30,61 @@ export const toHreflang = (code: string): string => {
}
return map[code] || code
}
+
+const DEFAULT_REGION = process.env.NEXT_PUBLIC_DEFAULT_REGION || "us"
+
+// The storefront serves every page under a locale prefix (/de, /se, ...);
+// requests without one are 307-redirected by the middleware. x-default must
+// therefore resolve to a locale that actually returns 200, never the bare path.
+export const resolveXDefaultLocale = (locales: string[]): string => {
+ if (!locales.length) return DEFAULT_REGION
+ for (const preferred of [DEFAULT_REGION, "us", "gb"]) {
+ if (locales.includes(preferred)) return preferred
+ }
+ return locales[0]
+}
+
+export const getStorefrontLocales = (
+ regions: { countries?: { iso_2?: string | null }[] | null }[] | null
+): string[] =>
+ Array.from(
+ new Set(
+ (regions || []).flatMap(
+ (r) => r.countries?.map((c) => c.iso_2).filter(Boolean) || []
+ )
+ )
+ ) as string[]
+
+type HreflangAlternates = {
+ canonical: string
+ languages: Record<string, string>
+}
+
+// Builds hreflang alternates where every entry — including x-default and the
+// self-referencing link — points at a locale-prefixed URL that returns 200.
+export const buildHreflangAlternates = ({
+ baseUrl,
+ path,
+ locale,
+ locales,
+}: {
+ baseUrl: string
+ // Path after the locale segment, starting with "/" (empty string for home).
+ path: string
+ locale: string
+ locales: string[]
+}): HreflangAlternates => {
+ const list = locales.length ? locales : [locale]
+
+ const languages = list.reduce<Record<string, string>>((acc, code) => {
+ acc[toHreflang(code)] = `${baseUrl}/${code}${path}`
+ return acc
+ }, {})
+
+ languages["x-default"] = `${baseUrl}/${resolveXDefaultLocale(list)}${path}`
+
+ return {
+ canonical: `${baseUrl}/${locale}${path}`,
+ languages,
+ }
+}
diff --git a/apps/storefront/src/lib/helpers/seo.ts b/apps/storefront/src/lib/helpers/seo.ts
index 86a4ab1d2..28d5c10ca 100644
--- a/apps/storefront/src/lib/helpers/seo.ts
+++ b/apps/storefront/src/lib/helpers/seo.ts
@@ -2,23 +2,49 @@ import { HttpTypes } from "@medusajs/types"
import { Metadata } from "next"
import { headers } from "next/headers"
+import { listRegions } from "@/lib/data/regions"
+import {
+ buildHreflangAlternates,
+ getStorefrontLocales,
+} from "@/lib/helpers/hreflang"
+
export const generateProductMetadata = async (
- product: HttpTypes.StoreProduct
+ product: HttpTypes.StoreProduct,
+ locale: string
): Promise<Metadata> => {
const headersList = await headers()
const host = headersList.get("host")
const protocol = headersList.get("x-forwarded-proto") || "https"
+ const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || `${protocol}://${host}`
+
+ let locales: string[] = []
+ try {
+ locales = getStorefrontLocales(await listRegions())
+ } catch {
+ locales = [locale]
+ }
+
+ const { canonical, languages } = buildHreflangAlternates({
+ baseUrl,
+ path: `/products/${product?.handle}`,
+ locale,
+ locales,
+ })
return {
title: product?.title,
description: `${product?.title} - ${process.env.NEXT_PUBLIC_SITE_NAME}`,
robots: "index, follow",
- metadataBase: new URL(`${protocol}://${host}/products/${product?.handle}`),
+ metadataBase: new URL(baseUrl),
+ alternates: {
+ canonical,
+ languages,
+ },
openGraph: {
title: product?.title,
description: `${product?.title} - ${process.env.NEXT_PUBLIC_SITE_NAME}`,
- url: `${protocol}://${host}/products/${product?.handle}`,
+ url: canonical,
siteName: process.env.NEXT_PUBLIC_SITE_NAME,
images: [
{