رفتن به محتوای اصلی
بازگشت به نوشته‌ها
یادداشت فنی•۰۱ آذر ۱۴۰۵

Zero-CLS bilingual RTL and LTR design systems in Next.js

Keep layout shifts at zero across Persian and English routes by aligning fonts, direction attributes, and logical spacing on the server.

Next.jsPerformanceInternationalizationUX

Layout shift hurts bilingual apps twice

Cumulative Layout Shift (CLS) often creeps into bilingual (fa / en) applications when the server renders one direction or fallback font and the client hydrates into another. A navbar flips from left-to-right to right-to-left, a Persian heading swaps from a system font to Vazirmatn, or a skeleton collapses when localized labels arrive.

The fix is not a loading spinner—it is resolving locale, direction, and typography before the first byte of HTML reaches the browser.

Lock lang, dir, and fonts during SSR

In Next.js App Router, resolve the active locale from the route segment and middleware header in the root layout, then apply lang, dir, and self-hosted next/font/local CSS variables directly on <html>.

<html
  lang={locale}
  dir={locale === 'fa' ? 'rtl' : 'ltr'}
  className={`${bodyFont.variable} ${headingFont.variable} ${persianFont.variable}`}
>

Self-hosting variable font files with display: 'swap' and matching metric fallbacks eliminates external font-CDN round trips and prevents late-arriving stylesheets from reflowing headings.

Use logical CSS properties everywhere

Physical utilities (ml-4, pl-6, text-left, border-r) force you to maintain duplicate rtl: overrides—and every missed override creates an asymmetric jump when switching languages. Logical properties (ms-4, ps-6, text-start, border-e) adapt automatically to dir="rtl" and dir="ltr" with zero JavaScript.

Keep directional exceptions deliberate:

  • Arrows that indicate reading order (ArrowRight) should mirror with rtl:rotate-180.
  • Numeric codes, URLs, SKUs, and timestamps inside Persian copy should stay dir="ltr" with tabular-nums so digits do not jump or reorder.

Reserve space for media and dynamic chrome

Even with clean directionality, images and client-hydrated badges can shift content:

  • Always give hero portraits and project thumbnails explicit aspect-* containers or width/height attributes.
  • Avoid injecting conditional banners above the fold only after a client useEffect fetch completes; stream or reserve their slot on the server.

Audit both locales at 320px and 1440px

Check every release in both /en and /fa with JavaScript disabled first, then after hydration. If the first paint and the hydrated DOM share the same direction, font metrics, and container dimensions, CLS stays near zero in both languages.

پیام بگذارید — پاسخ در چت یا ایمیل

سؤالی درباره مقاله «Zero-CLS bilingual RTL and LTR design systems in Next.js» دارید؟

گفت‌وگوی زنده را با موضوع همین صفحه باز کنید.

این مسئله برای محصول شما هم پیش آمده است؟ درباره شرایط و کاری که نیاز دارید صحبت کنیم.