Blog is language-locked: locale-aware API queries and twin redirects
Deploy Frontend / deploy (push) Successful in 1m10s

- Every blog list fetch (homepage, new/popular/search, categories, posts, tags) sends the active locale as lang to the API, so only that language's content renders
- Post, category and tag detail pages redirect to the mirrored twin slug when the URL language does not match the selected locale, so shared links always open in the viewer's language
- Fix home card links that used category.name as the URL slug instead of category.slug
- Add slugForLocale/isFaSlug helpers in lib/functions/utils
This commit is contained in:
Parsa Aghayi committed 2026-10-02 08:31:05 -04:00
1 parent 32a7481ef6
commit 2ef4d41588
10 files changed
+120 -20

No files matched your search

@@ -6,14 +6,18 @@ import { redirect } from "next/navigation";
import NoDataFounded from "../../components/no-data-founded/noDataFounded";
import Pagination from "../../components/pagination/pagination";
import { getDictionary } from "@/app/lib/functions/dictionaries";
import { getDefaultLocale } from "@/app/lib/functions/utils";
import {
getDefaultLocale,
slugForLocale,
} from "@/app/lib/functions/utils";
async function fetchCategoryData(
categorySlug: string,
searchParams: URLSearchParams,
locale: string,
): Promise<CategoryDataType> {
const res = await fetch(
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories/${categorySlug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}`,
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories/${categorySlug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}&lang=${locale}`,
{
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
},
@@ -26,11 +30,29 @@ async function fetchCategoryData(
return res.json();
}
/**
* Blog categories are language twins sharing the `-fa` slug suffix. A Persian
* visitor landing on the English URL (a shared link, for example) is sent to
* the mirrored Persian category so both versions always show the right language.
*/
async function resolveCategorySlug(raw: string): Promise<string> {
const locale = await getDefaultLocale();
const localized = slugForLocale(raw, locale);
if (localized !== raw) {
redirect(`/blog/categories/${localized}`);
}
return locale;
}
export async function generateMetadata(props: categorySlugPropsType): Promise<Metadata> {
const params = await props.params;
const locale = await resolveCategorySlug(params.categorySlug);
const categoryData: CategoryDataType = await fetchCategoryData(
params.categorySlug,
new URLSearchParams(),
locale,
);
return {
@@ -47,11 +69,13 @@ export async function generateMetadata(props: categorySlugPropsType): Promise<Me
export default async function SingleCategory(props: categorySlugPropsType) {
const searchParams = await props.searchParams;
const params = await props.params;
const dict = await getDictionary(await getDefaultLocale());
const locale = await resolveCategorySlug(params.categorySlug);
const dict = await getDictionary(locale);
const categoryData: CategoryDataType = await fetchCategoryData(
params.categorySlug,
new URLSearchParams(searchParams as Record<string, string>),
locale,
);
return (
+2 -1
View File
@@ -26,8 +26,9 @@ export const metadata: Metadata = {
export default async function Categories(props: categoryPropsType) {
const searchParams = await props.searchParams;
const locale = await getDefaultLocale();
const CategoriesData: CategoriesListType = await fetch(
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories?per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_CATEGORIES_PER_CATEGORY_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}`,
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/categories?per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_CATEGORIES_PER_CATEGORY_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}&lang=${locale}`,
{
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
},
@@ -73,7 +73,7 @@ export default async function FilteredPosts({ data }: FilteredPostsProps) {
{dict.blog.homePage.in}
</p>
<Link
href={`/blog/categories/${data[0].category.name}`}
href={`/blog/categories/${data[0].category.slug}`}
className="text-[15px] font-[400] text-white underline sm:text-[16px]"
>
{data[0].category.name}
@@ -39,7 +39,7 @@ export default async function New({ data }: NewProps) {
{dict.blog.homePage.in}
</p>
<Link
href={`/blog/categories/${data[0].category.name}`}
href={`/blog/categories/${data[0].category.slug}`}
className="text-[15px] font-[400] text-white underline sm:text-[16px]"
>
{data[0].category.name}
@@ -47,7 +47,7 @@ export default async function Random({ data }: RandomProps) {
{dict.blog.homePage.in}
</p>
<Link
href={`/blog/categories/${randomPost.category.name}`}
href={`/blog/categories/${randomPost.category.slug}`}
className="text-[16px] font-[400] text-blog-text-default hover:underline"
>
{randomPost.category.name}
@@ -83,12 +83,13 @@ export default async function FilteredPosts(props: FilteredPostsPagepropsType) {
const filteredPostApiUrl = filterKey ? FILTERS[filterKey].apiUrl : "";
const dict = await getDictionary(await getDefaultLocale());
const locale = await getDefaultLocale();
let FilteredPostsData: FilteredPostsDataType;
if (filteredPostApiUrl !== "") {
FilteredPostsData = await fetch(
`${process.env.NEXT_PUBLIC_BLOG_API_URL}${filteredPostApiUrl}&per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_POSTS_PER_FILTERED_POSTS_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}`,
`${process.env.NEXT_PUBLIC_BLOG_API_URL}${filteredPostApiUrl}&per_page=${searchParams.per_page ? searchParams.per_page : process.env.NEXT_PUBLIC_POSTS_PER_FILTERED_POSTS_PAGE}${searchParams.page ? `&page=${searchParams.page}` : ""}&lang=${locale}`,
{
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
},
+8 -3
View File
@@ -2,16 +2,21 @@ import FilteredPosts from "./components/home/filtered-posts/filteredPosts";
import New from "./components/home/new/new";
import Random from "./components/home/random/random";
import Categories from "./components/home/Categories/Categories";
import { getDefaultLocale } from "@/app/lib/functions/utils";
export default async function Blog() {
const locale = await getDefaultLocale();
const homePageData: {
latest_posts: [];
latest_categories: [];
most_viewed_posts: [];
random_posts: [];
} = await fetch(`${process.env.NEXT_PUBLIC_BLOG_API_URL}/homepage`, {
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
}).then((res) => res.json());
} = await fetch(
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/homepage?lang=${locale}`,
{
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
},
).then((res) => res.json());
return (
<div className="flex w-full max-w-[1280px] flex-col items-center gap-[50px] pt-5 md:gap-[100px]">
<New data={homePageData.latest_posts} />
@@ -1,5 +1,8 @@
import { getDictionary } from "@/app/lib/functions/dictionaries";
import { getDefaultLocale } from "@/app/lib/functions/utils";
import {
getDefaultLocale,
slugForLocale,
} from "@/app/lib/functions/utils";
import {
PostSlugPagePostType,
PostSlugPagepropsType,
@@ -14,9 +17,12 @@ import { getCoverImageSrc } from "./coverImage";
import { AUTHOR_PROFILE_URL } from "@/app/lib/social-links";
import { SITE_NAME, SITE_NAME_EN, SITE_URL } from "@/app/lib/site-config";
async function fetchPostData(postSlug: string): Promise<PostSlugPagePostType> {
async function fetchPostData(
postSlug: string,
locale: string,
): Promise<PostSlugPagePostType> {
const res = await fetch(
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/posts/${postSlug}`,
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/posts/${postSlug}?lang=${locale}`,
{
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
}
@@ -29,6 +35,23 @@ async function fetchPostData(postSlug: string): Promise<PostSlugPagePostType> {
return res.json();
}
/**
* Every post is a strict English/Persian mirror sharing the `-fa` slug suffix.
* A visitor whose locale does not match the requested URL is sent to the twin,
* so the shared English link opens the Persian article when the site is in
* Persian and vice versa.
*/
async function resolvePostSlug(raw: string): Promise<string> {
const locale = await getDefaultLocale();
const localized = slugForLocale(raw, locale);
if (localized !== raw) {
redirect(`/blog/posts/${localized}`);
}
return locale;
}
function formatPostDate(value: unknown): string {
if (typeof value !== "string" || value.trim() === "") {
return "";
@@ -47,7 +70,8 @@ function formatPostDate(value: unknown): string {
export async function generateMetadata(props: PostSlugPagepropsType): Promise<Metadata> {
const params = await props.params;
const post: PostSlugPagePostType = await fetchPostData(params.postSlug);
const locale = await resolvePostSlug(params.postSlug);
const post: PostSlugPagePostType = await fetchPostData(params.postSlug, locale);
const coverImage = getCoverImageSrc(post.post.featured_image);
const pageUrl = `${SITE_URL}/blog/posts/${params.postSlug}`;
@@ -93,8 +117,12 @@ export async function generateMetadata(props: PostSlugPagepropsType): Promise<Me
export default async function SinglePost(props: PostSlugPagepropsType) {
const params = await props.params;
const dict = await getDictionary(await getDefaultLocale());
const postData: PostSlugPagePostType = await fetchPostData(params.postSlug);
const locale = await resolvePostSlug(params.postSlug);
const dict = await getDictionary(locale);
const postData: PostSlugPagePostType = await fetchPostData(
params.postSlug,
locale,
);
const post = postData.post;
const coverImage = getCoverImageSrc(post.featured_image);
const authorName = post.author?.name ?? "";
+27 -3
View File
@@ -9,14 +9,18 @@ import { redirect } from "next/navigation";
import NoDataFounded from "../../components/no-data-founded/noDataFounded";
import Pagination from "../../components/pagination/pagination";
import { getDictionary } from "@/app/lib/functions/dictionaries";
import { getDefaultLocale } from "@/app/lib/functions/utils";
import {
getDefaultLocale,
slugForLocale,
} from "@/app/lib/functions/utils";
async function fetchTagData(
slug: string,
searchParams: URLSearchParams,
locale: string,
): Promise<TagDataType> {
const res = await fetch(
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/tags/by-slug/${slug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}`,
`${process.env.NEXT_PUBLIC_BLOG_API_URL}/tags/by-slug/${slug}/posts?per_page=${searchParams.get("per_page") || process.env.NEXT_PUBLIC_POSTS_PER_CATEGORY_PAGE}${searchParams.get("page") ? `&page=${searchParams.get("page")}` : ""}&lang=${locale}`,
{
cache: process.env.NEXT_PUBLIC_BLOG_CACHE ? "force-cache" : "no-store",
},
@@ -29,11 +33,29 @@ async function fetchTagData(
return res.json();
}
/**
* Blog tags are language twins sharing the `-fa` slug suffix. A Persian visitor
* landing on the English URL is sent to the mirrored Persian tag so both
* versions always show the right language.
*/
async function resolveTagSlug(raw: string): Promise<string> {
const locale = await getDefaultLocale();
const localized = slugForLocale(raw, locale);
if (localized !== raw) {
redirect(`/blog/tags/${localized}`);
}
return locale;
}
export async function generateMetadata(props: tagSlugPropsType): Promise<Metadata> {
const params = await props.params;
const locale = await resolveTagSlug(params.slug);
const tagData: TagDataType = await fetchTagData(
params.slug,
new URLSearchParams(),
locale,
);
return {
@@ -45,11 +67,13 @@ export async function generateMetadata(props: tagSlugPropsType): Promise<Metadat
export default async function SingleTag(props: tagSlugPropsType) {
const searchParams = await props.searchParams;
const params = await props.params;
const dict = await getDictionary(await getDefaultLocale());
const locale = await resolveTagSlug(params.slug);
const dict = await getDictionary(locale);
const tagData: TagDataType = await fetchTagData(
params.slug,
new URLSearchParams(searchParams as Record<string, string>),
locale,
);
const tagName = tagData.tag.name || params.slug.replaceAll(/%20|-|_/g, " ");
+17
View File
@@ -8,3 +8,20 @@ export const getDefaultLocale = async () => {
: "en";
return defaultLocale;
};
/**
* Blog content is stored as a strict English/Persian mirror, with Persian rows
* using the `-fa` slug suffix. These helpers locate a row's twin in the other
* language so the language switcher can jump between the two versions.
*/
export const isFaSlug = (slug: string): boolean => slug.endsWith("-fa");
export const slugForLocale = (slug: string, locale: string): string => {
const isFa = isFaSlug(slug);
if (locale === "fa") {
return isFa ? slug : `${slug}-fa`;
}
return isFa ? slug.slice(0, -3) : slug;
};