-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting the locale in the server context (SSR) does not work in production build #44
Comments
does the console say anything about hydration miss match issues? also i wouldn't full trust the dev server without giving it a build first 😅 |
Like I said it's not happening in the dev server but only in the production build. I can see the backend content being rendered in the page source in the English locale, but being overridden on the client once hydration happens. But client side and hydration are not important here, I can't get the server to render any non-en locale dates when using the built prod server version. I can't even get it so switch to a different locale, as reading after setting the locale immediately returns the default locale again. In the dev server and client side, this works without any issues 🙏 |
Same issue |
Same issue, different locales between SSR and Client, hydration issue. |
Same issue |
Any news on this ? Thank you. |
Seems to be an issue i'm having also on a side project i'm building, https://huishistorie.nl/amersfoort/3822cg/bombardonstraat/132 As soon as i change stuff like this:
To:
From the template my hydration error on the frontend disappears. Haven't seen it in dev builds as mentioned above by @PatrickBauer |
Same issue, but in dev mode too. In ssr dayjs defaults to "en" locale even when explicitly set to another. |
I can confirm this issue... very annoying and resulting in "Hydration completed but contains mismatches." errors in console in production builds! |
Same issue with defaultTimezone. SSR does not use it (maybe and client ). In ssr dayjs using server timezone and after that I see hydration error because I have another timezone |
(Updated) I created a plugin. export default defineNuxtPlugin({
name: "locale",
hooks: {
"app:created": () => {
const nuxtApp = useNuxtApp();
const dayjs = nuxtApp.$dayjs;
const i18n = nuxtApp.$i18n;
dayjs.locale(i18n.locale.value);
},
"i18n:beforeLocaleSwitch": ({ newLocale }) => {
const nuxtApp = useNuxtApp();
const dayjs = nuxtApp.$dayjs;
dayjs.locale(newLocale);
},
},
}); Both the server and the client are configured correctly. However, it didn't work with the "i18n:localeSwitched" hook, which might be an issue with the "@nuxtjs/i18n": "^9.1.0",
"dayjs": "^1.11.13",
"dayjs-nuxt": "^2.1.11" |
Got here because I have the same issue. "dayjs-nuxt": "^2.1.11", |
The core issue is that all locales except "en" do not exist when called during SSR, despite being set in the configuration files. My temporary solution involves extracting locale configurations directly from the Day.js source, storing them in separate files within my application, and passing them as arguments to dayjs.locale(). Once the page finishes loading, I swap these temporary locales with the actual ones. This approach does have potential discrepancies if locales are updated or dayjs configurations change significantly. However, as long as you stick to the same Day.js version, this method works reliably. To avoid conflicts with standard locales, name the temporary locales distinctly, such as frSSR instead of just fr. |
To make locale config available dayjs requires import with side-effects (like It seems that imports without binding to a variable are lost on nuxt build. Current workaround for my case: import dayjs from 'dayjs'
import locale from 'dayjs/locale/ru'
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
dayjs.locale('ru', locale)
export function useDayjs()
{
return dayjs
} To use:
|
Thank you @Trash0101 and @dpetrouk for sharing your solutions. For anyone having an internationalized site, I'd suggest creating an i18n plugin and adding the workaround like this:
"dayjs-nuxt": "^2.1.11", |
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
Output
Here I'd expect the server to put out "de" twice, but setting the locale simply does nothing.
Config
The text was updated successfully, but these errors were encountered: