feat: implement locale-aware routing with [locale] dynamic segments
Some checks failed
Build and Deploy / build (push) Has been cancelled

WARNING: This change breaks existing SEO URLs for Serbian locale.

Changes:
- Migrated from separate locale folders (src/app/en/, src/app/de/, etc.)
  to [locale] dynamic segments (src/app/[locale]/)
- Serbian is now at /sr/ instead of / (root)
- English at /en/, German at /de/, French at /fr/
- All components updated to generate locale-aware links
- Root / now redirects to /sr (307 temporary redirect)

SEO Impact:
- Previously indexed Serbian URLs (/, /products, /about, /contact)
  will now return 404 or redirect to /sr/* URLs
- This is a breaking change for SEO - Serbian pages should ideally
  remain at root (/) with only non-default locales getting prefix
- Consider implementing 301 redirects from old URLs to maintain
  search engine rankings

Technical Notes:
- next-intl v4 with [locale] structure requires ALL locales to
  have the prefix (cannot have default locale at root)
- Alternative approach would be separate folder structure per locale
This commit is contained in:
Unchained
2026-03-23 20:59:33 +02:00
parent 5bd1a0f167
commit 92b6c830e1
47 changed files with 2175 additions and 2881 deletions

View File

@@ -0,0 +1,104 @@
import { getProductBySlug, getProducts, getLocalizedProduct } from "@/lib/saleor";
import { getTranslations, setRequestLocale } from "next-intl/server";
import Header from "@/components/layout/Header";
import Footer from "@/components/layout/Footer";
import ProductDetail from "@/components/product/ProductDetail";
import type { Product } from "@/types/saleor";
import { routing } from "@/i18n/routing";
interface ProductPageProps {
params: Promise<{ locale: string; slug: string }>;
}
export async function generateStaticParams() {
const locales = routing.locales;
const params: Array<{ locale: string; slug: string }> = [];
for (const locale of locales) {
try {
const productLocale = locale === "sr" ? "SR" : "EN";
const products = await getProducts(productLocale, 100);
products.forEach((product: Product) => {
params.push({ locale, slug: product.slug });
});
} catch (e) {
}
}
return params;
}
export async function generateMetadata({ params }: ProductPageProps) {
const { locale, slug } = await params;
const productLocale = locale === "sr" ? "SR" : "EN";
const product = await getProductBySlug(slug, productLocale);
if (!product) {
return {
title: locale === "sr" ? "Proizvod nije pronađen" : "Product not found",
};
}
const localized = getLocalizedProduct(product, productLocale);
return {
title: localized.name,
description: localized.seoDescription || localized.description?.slice(0, 160),
};
}
export default async function ProductPage({ params }: ProductPageProps) {
const { locale, slug } = await params;
setRequestLocale(locale);
const t = await getTranslations("Product");
const productLocale = locale === "sr" ? "SR" : "EN";
const product = await getProductBySlug(slug, productLocale);
const basePath = locale === "sr" ? "" : `/${locale}`;
if (!product) {
return (
<>
<Header locale={locale} />
<main className="min-h-screen bg-white">
<div className="pt-[180px] lg:pt-[200px] pb-20 text-center px-4">
<h1 className="text-2xl font-medium mb-4">
{t("notFound")}
</h1>
<p className="text-[#666666] mb-8">
{t("notFoundDesc")}
</p>
<a
href={`${basePath}/products`}
className="inline-block px-8 py-3 bg-black text-white text-sm uppercase tracking-[0.1em] hover:bg-[#333333] transition-colors"
>
{t("browseProducts")}
</a>
</div>
</main>
<Footer locale={locale} />
</>
);
}
let relatedProducts: Product[] = [];
try {
const allProducts = await getProducts(productLocale, 8);
relatedProducts = allProducts
.filter((p: Product) => p.id !== product.id)
.slice(0, 4);
} catch (e) {}
return (
<>
<Header locale={locale} />
<main className="min-h-screen bg-white">
<ProductDetail
product={product}
relatedProducts={relatedProducts}
locale={productLocale}
/>
</main>
<Footer />
</>
);
}

View File

@@ -0,0 +1,103 @@
import { getProducts } from "@/lib/saleor";
import { getTranslations, setRequestLocale } from "next-intl/server";
import Header from "@/components/layout/Header";
import Footer from "@/components/layout/Footer";
import ProductCard from "@/components/product/ProductCard";
import { ChevronDown } from "lucide-react";
interface ProductsPageProps {
params: Promise<{ locale: string }>;
}
export async function generateMetadata({ params }: ProductsPageProps) {
const { locale } = await params;
return {
title: locale === "sr"
? "Proizvodi - ManoonOils"
: "Products - ManoonOils",
description: locale === "sr"
? "Pregledajte našu kolekciju premium prirodnih ulja za negu kose i kože."
: "Browse our collection of premium natural oils for hair and skin care.",
};
}
export default async function ProductsPage({ params }: ProductsPageProps) {
const { locale } = await params;
setRequestLocale(locale);
const t = await getTranslations("Products");
const productLocale = locale === "sr" ? "SR" : "EN";
const products = await getProducts(productLocale);
return (
<>
<Header locale={locale} />
<main className="min-h-screen bg-white">
<div className="pt-[72px] lg:pt-[72px]">
<div className="border-b border-[#e5e5e5]">
<div className="container py-8 md:py-12">
<div className="flex flex-col md:flex-row md:items-end md:justify-between gap-4">
<div>
<span className="text-xs uppercase tracking-[0.2em] text-[#666666] mb-2 block">
{t("collection")}
</span>
<h1 className="text-3xl md:text-4xl font-medium">
{t("allProducts")}
</h1>
</div>
<div className="flex items-center gap-3">
<span className="text-sm text-[#666666]">
{t("productsCount", { count: products.length })}
</span>
<div className="relative">
<select
className="appearance-none bg-transparent border border-[#e5e5e5] pl-4 pr-10 py-2 text-sm focus:outline-none focus:border-black cursor-pointer"
defaultValue="featured"
>
<option value="featured">{t("featured")}</option>
<option value="newest">{t("newest")}</option>
<option value="price-low">{t("priceLow")}</option>
<option value="price-high">{t("priceHigh")}</option>
</select>
<ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 pointer-events-none text-[#666666]" />
</div>
</div>
</div>
</div>
</div>
<section className="py-12 md:py-16">
<div className="container">
{products.length === 0 ? (
<div className="text-center py-20">
<p className="text-[#666666] mb-4">
{t("noProducts")}
</p>
<p className="text-sm text-[#999999]">
{t("checkBack")}
</p>
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 lg:gap-8">
{products.map((product, index) => (
<ProductCard
key={product.id}
product={product}
index={index}
locale={productLocale}
/>
))}
</div>
)}
</div>
</section>
</div>
</main>
<div className="pt-16">
<Footer locale={locale} />
</div>
</>
);
}