Is there any way to determine whether a component is a client-side component or a server-side component? #63745
Replies: 1 comment
-
AFAIK, at the moment such tooling doesn't exist. I haven't look around community tools. That being said:
Also, the component wording here is a bit misleading, it is the JavaScript Module (file if you will) that gets turned into either a Server Module (default), or Client Module (bundled to be sent to the client, with 'use client'). With that in mind, it is possible for a module, call it So, this goes beyond the JavaScript expressions, into the bundler territory. I guess it'd be possible for bundlers to make available certain ENV vars to tell you under which situation you are, but that AFAIK is not available yet, and it would require building a module dependency tree first, and then evaluate the JavaScript AST injecting some kind of information about the module. Again, I haven't investigated too much. I also think that using this kind of meta analysis, of whether a module will be sent to the client, or not, and based on that, make some code decisions opens up for way more complexity that necessary. If you want to enforce a module's behavior, you can use these two packages:
Placing this at the top of a module, will "poison"/"taint" the module, so that at development time, you'll see when a module is used incorrectly. |
Beta Was this translation helpful? Give feedback.
-
Is there any way to determine whether a component is a client-side component or a server-side component? Can you console.log it out?
Beta Was this translation helpful? Give feedback.
All reactions