fix: eliminate 2s LCP rendering delay from Directus timeout on Hero
All checks were successful
CI / CD / test-build (push) Successful in 10m11s
CI / CD / deploy-dev (push) Successful in 1m17s
CI / CD / deploy-production (push) Has been skipped

The Hero server component awaited getMessages(locale) which called Directus
with a 2-second timeout. On testing.dk0.dev (or when Directus is unreachable),
this blocked the entire Hero render for ~2s → LCP 3.0s / 2320ms rendering delay.

Changes:
- Hero.tsx: remove getMessages() call entirely; use t() for all strings
- messages/en.json + de.json: add hero.badge, hero.line1, hero.line2 keys
- lib/i18n-loader.ts: invert lookup order — JSON first, Directus only as
  override for keys absent from JSON. Previously Directus was tried first
  for every key, causing ~49 parallel network requests per page load in
  HomePageServer (aboutT + projectsT + contactT + footerT translations).
  Now all JSON-backed keys return instantly without any network I/O.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 22:36:03 +01:00
parent dacec18956
commit bdf02b2a3a
5 changed files with 40 additions and 37 deletions

View File

@@ -29,8 +29,12 @@ function getCached(key: string): unknown | null {
}
/**
* Get a localized message by key
* Tries: Directus (requested locale) → Directus (EN) → JSON (requested locale) → JSON (EN)
* Get a localized message by key.
* Tries: JSON (requested locale) → JSON (EN) → Directus (requested locale) → Directus (EN)
*
* JSON is checked first so that translation-heavy server components never wait on
* a Directus network round-trip for keys that already exist in the message files.
* Directus is only queried when the key is absent from JSON (i.e. CMS-only content).
*/
export async function getLocalizedMessage(
key: string,
@@ -40,31 +44,16 @@ export async function getLocalizedMessage(
const cached = getCached(cacheKey);
if (cached !== null) return cached as string;
// Try Directus with requested locale
const dbValue = await getMessage(key, locale);
if (dbValue) {
setCached(cacheKey, dbValue);
return dbValue;
}
// Fallback to EN in Directus if not EN already
if (locale !== 'en') {
const dbValueEn = await getMessage(key, 'en');
if (dbValueEn) {
setCached(cacheKey, dbValueEn);
return dbValueEn;
}
}
// Fallback to JSON file (normalize locale to 'en' or 'de')
const normalizedLocale = locale.startsWith('de') ? 'de' : 'en';
// 1) JSON requested locale
const jsonValue = getNestedValue(jsonFallback[normalizedLocale as 'en' | 'de'], key);
if (jsonValue) {
setCached(cacheKey, jsonValue);
return jsonValue;
}
// Fallback to EN JSON
// 2) JSON EN fallback
if (normalizedLocale !== 'en') {
const jsonValueEn = getNestedValue(jsonFallback['en'], key);
if (jsonValueEn) {
@@ -73,7 +62,23 @@ export async function getLocalizedMessage(
}
}
// Fallback: return the key itself
// 3) Directus only for keys missing from JSON (CMS-only content)
const dbValue = await getMessage(key, locale);
if (dbValue) {
setCached(cacheKey, dbValue);
return dbValue;
}
// 4) Directus EN fallback
if (locale !== 'en') {
const dbValueEn = await getMessage(key, 'en');
if (dbValueEn) {
setCached(cacheKey, dbValueEn);
return dbValueEn;
}
}
// 5) Return the key itself as last resort
return key;
}