SSR component from package
I'm having trouble server-side-rendering a component directly from a package. I can load the component in the client like so:
react_component('@package/package-name.ComponentName')
But I can require('@package/package-name').ComponentName fine, but when trying to SSR, i get an error that it is undefined.
Do I have to explicitly make this package available for SSR somehow?
react_component seeks files slightly differently than the require tool. A simple solution would be to make a file with a name that simply requires and exports the component directly, that would make it accessible to the react_component helper.
I would also accept a PR that would allow the context to look for components using require in addition to files.
Thanks, @BookOfGreg . so in my project, i would have to create:
# PackageComponent.js
module.exports = require('@package/package-name').ComponentName
Then, i could (assuming the above file is in the glob context):
react_component('PackageComponent')
Is this correct?
Also, I would love to add this enhancement, i'll take at the contributing guidelines and get started.
Beyond that, any advice where I should get started making this change, such as a file to look at first? after stepping through the source, it looks like the component is eventually resolved at:
https://github.com/reactjs/react-rails/blob/4dbcd6756dcd09c312abf95a73cbb6026f3f9a69/react_ujs/src/getConstructor/fromGlobal.js
But from there, its not clear to me how stuff gets inserted into that global object.
Is this correct?
@quinn Yep, that should work for you.
But from there, its not clear to me how stuff gets inserted into that global object.
Things are added to the global context by requiring them onto it which I hadn't thought when I initially replied. I actually wonder if simply requiring the component and then referencing it by name would work. If so then no additional work would be needed as I think that's a reasonable thing to do before use.
Can you try
require('@package/package-name').ComponentName or require('@package/package-name') in the entrypoint for both the applicaton and serverside pack, and then see if react_component('PackageComponent') works directly from there without the extra file?
wouldn't I have to
window.PackageComponent = require('@package/package-name').ComponentName
explicitly?
Either way, I will try it out and let you know how it goes. thank you!
@quinn were you able to get this working?
@quinn, closing the issue for now. We have a solution for this, as discussed in the above conversations.