dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Setting the locale in the server context (SSR) does not work in production build

Open PatrickBauer opened this issue 6 months ago • 5 comments

We have a very simple code that works perfectly on client side, but fails on the server side on a production build. This does not happen when using the dev server though.

Code

export default defineNuxtRouteMiddleware(() => {
  const { locale } = useNuxtApp().$i18n;
  const dayjs = useDayjs();

  if (locale.value !== dayjs.locale()) {
    console.log("Setting dayjs locale to", locale.value);
    dayjs.locale(locale.value);
    console.log("Dayjs locale is now set to", dayjs.locale());
  }
});

Output

Setting dayjs locale to de
Dayjs locale is now set to en

Here I'd expect the server to put out "de" twice, but setting the locale simply does nothing.

Config

  locales: i18nConfig.locales,
  plugins: ["localeData", "localizedFormat", "utc", "customParseFormat"],
  defaultLocale: 'en',

PatrickBauer avatar Jul 31 '24 15:07 PatrickBauer