-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
clientModules are still evaluated server-side instead of client-side #4268
Comments
Yes that seems like a bug to me. You can probably work around this temporarily by using: if (typeof window !== "undefined") {
require("../lbh/all").initAll();
} We'll figure out how to exclude client modules from the server build. Note for myself: the client modules seems to be imported on the SSR pipeline with |
good to know i'm not going mad! your workaround does the trick for now |
We should note that the original call stack isn't being preserved. If this bug ends up being a wontfix / intentional, it'd be nice to have that fixed. https://app.netlify.com/sites/typescript-eslint/deploys/6190117990e17f0007564e0d:
|
Fixing this issue is trivial (just do a conditional import of the client modules), the only problem being whether we want to do it or not. I feel like we don't even have a clear definition of what client modules are, what they are supposed to do, and how they should interact with the Docusaurus app itself. @JoshuaKGoldberg Given that the client modules are directly |
This is my first time peeking into the Docusaurus source code so just thinking out loud... Going off of slorber's helpful hint, the
If you're ok with using top-level async/await, you could: try {
await import('./client-lifecycles-dispatcher');
} catch (error) {
console.error("Oh no! Failed to load client modules!", error);
} ...perhaps wrapped with an An alternative would be to drop the But, again, I'm super unfamiliar with the Docusaurus execution model so those are just suggestions 😄 |
Yes, I think you've got the idea about where it's coming from. We just had some troubles with client modules' side effects (#6052) so I did look a bit into this. And when I said "conditional import" I mean: if (typeof window !== 'undefined') {
require('./client-lifecycles-dispatcher');
} Because we use CJS throughout, we don't necessarily need to However that still wouldn't make the error stack visible, because there's no caller here, just a bunch of |
This is the content of the generated export default [
require("/Users/sebastienlorber/Desktop/projects/docusaurus/node_modules/infima/dist/css/default/default.css"),
require("/Users/sebastienlorber/Desktop/projects/docusaurus/packages/docusaurus-theme-classic/lib/prism-include-languages"),
require("/Users/sebastienlorber/Desktop/projects/docusaurus/packages/docusaurus-theme-classic/lib/admonitions.css"),
require("/Users/sebastienlorber/Desktop/projects/docusaurus/website/src/css/custom.css"),
require("/Users/sebastienlorber/Desktop/projects/docusaurus/website/_dogfooding/clientModuleExample.ts"),
]; This concept of client modules exist for a while, and I'm also not 100% sure what was the initial purpose. https://docusaurus.io/docs/lifecycle-apis#getclientmodules
In practice, this is used to load global code in the client app. We run the client app and import those modules too in the SSR process. Not sure what kind of weird side effects this would lead to if we only import those modules on the browser app, skipping the SSR app? Maybe a good solution would be to just change the name to Not sure how to get a better stacktrace. This is what Webpack gives us in the stats object: I tried to play with a few options without success. @alexander-akait any idea? One possibility is that we wrap the generated file requires() in try/catch to at least tell the user which client module is failing to load, but still not perfect. |
What is webpack version? |
I don't understand what you mean. My goal is just to get a stacktrace alongside the error message, not to debug a CSS loading issue. What I want is Webpack to provide us with an easy to debug error like this: {
errors: [
{
message: "ReferenceError: document is not defined",
+ error: "ReferenceError: document is not defined
+ at /Users/sebastienlorber/Desktop/projects/docusaurus/website/_dogfooding/clientModuleExample.js:12
+ at /Users/sebastienlorber/Desktop/projects/docusaurus/website/docusaurus/client-modules.js:6"
}
]
} |
Sorry, I think you need help to identify the problem, we have bundled code, so we don't have good original stack trace, if we need good stack trace we need source maps (like we do in browser), you can try https://nodejs.org/dist/latest-v14.x/docs/api/cli.html#cli_enable_source_maps, I don't try it, but I think it should work (and you need to set |
I see thanks 👍 will try that |
I'll probably be closing this as a wontfix, because over time, I start to understand "client module" here with "client" being "webpack" rather than "client-side rendering", and side-effects a client module induces could be equally useful in SSR, even if it means making people writing I'll leave it open to remind us that there's the source map problem, but this is totally different. All SSR errors have very poor stack traces. |
Yes, it's a bit confusing because we have 3 things:
The question is For now, it's 2 + 3, which is more generic/flexible, but both can make sense eventually, it's hard to tell without a list of use-cases (and I think the initial one was the Google Analytics plugin) We might has well provide a new option for modules initialized only in 3, and yet keep the current |
Opened #7456. I'll close this as a working-as-intended, now that this behavior is documented. Our client modules API is designed with maximum flexibility in mind because we don't really want many APIs that do similar stuff. |
Hello,
i'd like to include some third party global javascript code in my docusaurus website.
the third-party code is not ssr-safe - it includes references to
window
anddocument
objects and i can't change it (eg. to wrap it in an if document then... conditional).To Reproduce
I've written a tiny js file that imports the js code and initialises it. it looks a bit like this:
and then, in my
docusaurus.config.js
file, i include that file as aclientModule
:This runs great in development, but when I do
docusaurus build
it breaks withReferenceError: document is not defined
.I assume docusaurus is still trying to include that third-party code in the server bundle somehow.
Possibly this isn't a bug and I've misunderstood what clientModules is supposed to be used for. If so, perhaps we could improve the docs to make this clear?
Expected behavior
Build succeeds.
Actual Behavior
ReferenceError: document is not defined
Your Environment
Reproducible Demo
https://codesandbox.io/s/twilight-snowflake-zocv2
also raised in the discord
The text was updated successfully, but these errors were encountered: