react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

SSR component from package

Open quinn opened this issue 6 years ago • 5 comments

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?

quinn avatar Aug 31 '19 03:08 quinn

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.

BookOfGreg avatar Sep 09 '19 10:09 BookOfGreg

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.

quinn avatar Sep 09 '19 15:09 quinn

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?

BookOfGreg avatar Sep 09 '19 16:09 BookOfGreg

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 avatar Sep 09 '19 16:09 quinn

@quinn were you able to get this working?

detj avatar Jun 07 '20 21:06 detj

@quinn, closing the issue for now. We have a solution for this, as discussed in the above conversations.

alkesh26 avatar Nov 07 '22 08:11 alkesh26