Issue with script/library used both client-side and server side
Unanswered
Hovawart posted this in #help-forum
HovawartOP
We are using
This comes from here: https://github.com/matrix-org/matrix-js-sdk/blob/4947a0cb648ade4ceaeabbee6277e55615a4de84/src/index.ts#L20
That seems to suggest that this file is being run more than once. My guess would be that the server-side is intentionally running the file "from scratch" twice, once for the pre-rendered client-side files and once for the server-side files (with the logic being, I'm guessing, that the server-side and client-side should basically be sandboxed from each other so they need separate runs), but is running into issues since it's accessing a global object that ends up being shared across both of them. Is there anything I can do to avoid this problem?
We have tried to simply avoid the problem by running code that involves the
matrix-js-sdk in a NextJS application, and we're specifically trying to use the library both client-side and server-side. When we use it just client-side, we have no issues, but when we include any reference to one of its exports server-side as well (even just an enum), we get the following error:Multiple matrix-js-sdk entrypoints detected!
This comes from here: https://github.com/matrix-org/matrix-js-sdk/blob/4947a0cb648ade4ceaeabbee6277e55615a4de84/src/index.ts#L20
That seems to suggest that this file is being run more than once. My guess would be that the server-side is intentionally running the file "from scratch" twice, once for the pre-rendered client-side files and once for the server-side files (with the logic being, I'm guessing, that the server-side and client-side should basically be sandboxed from each other so they need separate runs), but is running into issues since it's accessing a global object that ends up being shared across both of them. Is there anything I can do to avoid this problem?
We have tried to simply avoid the problem by running code that involves the
matrix-js-sdk in a separate process but it's not entirely clear how best to do that in NextJS, and the approach I did try out (essentially to add a new build entry to webpack with webpack-merge and run that using node's process.spawn/fork) has had problems of its own. Any help would be appreciated.1 Reply
HovawartOP
We managed to find a way around this issue, though if there's a correct way to handle this, we're open to them.