Next.js Discord

Discord Forum

next-intl + MUI with pigment.css - Strange Error

Answered
Adralonter posted this in #help-forum
Open in Discord
Hello,
i am currently setting up i18n on our Next.js App. I closely followed the instructions on https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing.
Then I tried to add a locale switcher in our header as outlined here: https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing
I followed this closely so everything is set up identically to the example implementation.
Now since we are using MUI I tried to change up the native select with the MUI Select Component. I managed to do this just fine and everything works. The LocaleSwitcherSelect Component now looks like this:
------------
"use client";

import * as React from "react";

import { useParams } from "next/navigation";
import { ReactNode, useTransition } from "react";
import { Locale, usePathname, useRouter } from "@/i18n/routing";

import { Select, SelectChangeEvent } from "@mui/material";

type Props = {
children: ReactNode;
defaultValue: string;
};

export default function LocaleSwitcherSelect({
children,
defaultValue,
}: Props) {
const router = useRouter();
const [isPending, startTransition] = useTransition();
const pathname = usePathname();
const params = useParams();

function onSelectChange(event: SelectChangeEvent) {
const nextLocale = event.target.value as Locale;
startTransition(() => {
router.replace(
{ pathname, params },
{ locale: nextLocale },
);
});
}

return (
<Select
size="small"
id="locale-switcher-select"
defaultValue={defaultValue}
disabled={isPending}
onChange={onSelectChange}
displayEmpty
>
{children}
</Select>
);
}
------------
Now, if I add any sx prop to the Select or use styled from pigment.css anywhere in the Component, the server breaks completely. I narrowed it down to the useRouter, once that is not in the component anymore, everything works again (apart from the functionality of the switcher of course)

The error is the following:

------------
⨯ unhandledRejection: Error: Cannot assign to read only property '__esModule' of object '[object Object]' inC:\dev\frontend\node_modules\next\dist\client\link.js
| C:\dev\frontend\node_modules\next\link.js
| C:\dev\frontend\node_modules\next-intl\dist\development\navigation\shared\LegacyBaseLink.js
| C:\dev\frontend\node_modules\next-intl\dist\development\navigation\react-client\ClientLink.js
| C:\dev\frontend\node_modules\next-intl\dist\development\navigation\react-client\createSharedPathnamesNavigation.js
| C:\dev\frontend\node_modules\next-intl\dist\development\navigation.react-client.js
| C:\dev\frontend\node_modules\next-intl\dist\navigation.react-client.js
| C:\dev\frontend\src\i18n\routing.ts
| C:\dev\frontend\src_components\navigation\header\locale-switcher\locale-switcher-select.tsx

at C:\dev\frontend\node_modules\ (wyw-in-js\transform\src\module.ts:330)
at <unknown> (C:\dev\frontend\src\i18n\routing.ts:8:19)
at <unknown> (C:\dev\frontend\src\i18n\routing.ts:20:3)
------------

I do not understand how these two things conflict with each other and cause this (to me) unhelpful error.
I am not sure if this issue is tied to mui, pigment.css or next-intl, thats why I am asking here.
Answered by Adralonter
I was able to get help from the maintainer of pigment.css and he solved it, you can look up the solution here:
https://github.com/mui/pigment-css/issues/365
View full answer

19 Replies

@Adralonter Hello, i am currently setting up i18n on our Next.js App. I closely followed the instructions on https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing. Then I tried to add a locale switcher in our header as outlined here: https://next-intl.dev/docs/getting-started/app-router/with-i18n-routing I followed this closely so everything is set up identically to the example implementation. Now since we are using MUI I tried to change up the native select with the MUI Select Component. I managed to do this just fine and everything works. The LocaleSwitcherSelect Component now looks like this: ------------ "use client"; import * as React from "react"; import { useParams } from "next/navigation"; import { ReactNode, useTransition } from "react"; import { Locale, usePathname, useRouter } from "@/i18n/routing"; import { Select, SelectChangeEvent } from "@mui/material"; type Props = { children: ReactNode; defaultValue: string; }; export default function LocaleSwitcherSelect({ children, defaultValue, }: Props) { const router = useRouter(); const [isPending, startTransition] = useTransition(); const pathname = usePathname(); const params = useParams(); function onSelectChange(event: SelectChangeEvent) { const nextLocale = event.target.value as Locale; startTransition(() => { router.replace( { pathname, params }, { locale: nextLocale }, ); }); } return ( <Select size="small" id="locale-switcher-select" defaultValue={defaultValue} disabled={isPending} onChange={onSelectChange} displayEmpty > {children} </Select> ); } ------------ Now, if I add any sx prop to the Select or use styled from pigment.css anywhere in the Component, the server breaks completely. I narrowed it down to the useRouter, once that is not in the component anymore, everything works again (apart from the functionality of the switcher of course) The error is the following: ------------ ⨯ unhandledRejection: Error: Cannot assign to read only property '__esModule' of object '[object Object]' inC:\dev\frontend\node_modules\next\dist\client\link.js | C:\dev\frontend\node_modules\next\link.js | C:\dev\frontend\node_modules\next-intl\dist\development\navigation\shared\LegacyBaseLink.js | C:\dev\frontend\node_modules\next-intl\dist\development\navigation\react-client\ClientLink.js | C:\dev\frontend\node_modules\next-intl\dist\development\navigation\react-client\createSharedPathnamesNavigation.js | C:\dev\frontend\node_modules\next-intl\dist\development\navigation.react-client.js | C:\dev\frontend\node_modules\next-intl\dist\navigation.react-client.js | C:\dev\frontend\src\i18n\routing.ts | C:\dev\frontend\src\_components\navigation\header\locale-switcher\locale-switcher-select.tsx at C:\dev\frontend\node_modules\ (wyw-in-js\transform\src\module.ts:330) at <unknown> (C:\dev\frontend\src\i18n\routing.ts:8:19) at <unknown> (C:\dev\frontend\src\i18n\routing.ts:20:3) ------------ I do not understand how these two things conflict with each other and cause this (to me) unhelpful error. I am not sure if this issue is tied to mui, pigment.css or next-intl, thats why I am asking here.
West African Lion
can you throw some parts of your app into a bolt or codesandbox instance so i can recreate it and debug it for u
@West African Lion https://codesandbox.io/p/devbox/next-intl-mui-pigment-fpk9m3
Here it is, everything is set up. Just need to comment it in in LocaleSwitcherSelect.tsx
West African Lion
ty bro
ill take a look now
West African Lion
I am not sure if something is oblivious to me but it is working for me on your example
Well
I have no idea why it would be inconsistent
For me its 100%. Start dev server -> visit page, -> use sx prop in the component -> crash
I did not even copy parts of the main project, I just followed the mui, pigment.css and intl docs and it led to the same exact error here.
West African Lion
try dynamically loading @Adralonter the MUI components with SSR disabled
lazy-loading
Ok I will try that.
I would just really like to understand where this issue stems from.
Lazy loading them did not help.
Anyone else willing to debug the codesandbox example I provided? Im completely out of Ideas.
I was able to get help from the maintainer of pigment.css and he solved it, you can look up the solution here:
https://github.com/mui/pigment-css/issues/365
Answer
looks nice! apologise on behalf of the community for not being able to assist here, next-intl + MUI + pigment is a pretty rare combination that i don't think is used by any of the active people here
@joulev looks nice! apologise on behalf of the community for not being able to assist here, next-intl + MUI + pigment is a pretty rare combination that i don't think is used by any of the active people here
Yeah its all good. Since pigment is still in alpha its to be expected that there is less knowledge. I hope once MUI fully switches over to it from emotion, there will be more people using it. :meow_coffee:
Britannia Petite
sazp[z;