Next.js Discord

Discord Forum

Date format preferences client and server renders

Unanswered
Mucuchies posted this in #help-forum
Open in Discord
MucuchiesOP
Hi there, i want to create a performant way to let users select a date format preference. I get this from DB. BUT i have some requirements
*Max 1 query per render
*No async / await (i dont want to do await FormatDate(dateobj))
*No hydration issues?

I tried to put this in my utils:
// Preloaded settings cache
let cachedDateFormat: Intl.DateTimeFormatOptions | null = null;

// Preload settings (call this during app initialization)
export function preloadDateFormatSettings(dateFormat: string) {
  cachedDateFormat = mapDateFormatToOptions(dateFormat);
}

And this in my /app/layout.tsx:
  // Fetch the date format setting on the server
  const dateFormat = await getSetting("date_format", true, "dd mm");
  // Preload the date format settings
  preloadDateFormatSettings(dateFormat);


But then the datetime is rendered BEFORE this variable is set... Idk how, but anyone know how i can fix this?
Thanks :)

1 Reply

MucuchiesOP
Update, made it work with provider kinda. but still some issues.