简体   繁体   English

将前端与同构的Ruby on Rails Web应用程序分开。

[英]Separating front end from the isomorphic Ruby on Rails web application.

We have our web application built completely on Ruby on Rails. 我们的Web应用程序完全基于Ruby on Rails构建。 The front end is tightly coupled with the backend. 前端与后端紧密相连。 We are in a stage where we want to gradually start separating our frontend from the backend. 我们正处于逐步开始将前端与后端分离的阶段。 We want to go ReactJS way with the UI part and we want to follow the waterfall model for the transition. 我们希望UI部分采用ReactJS方式,并且我们希望遵循瀑布模型进行过渡。

We have two options: 我们有两种选择:

  1. Use gem called react-rails , and build new components in React. 使用称为react-rails的 gem,并在React中构建新组件。 Slowly start converting existing components into React components. 慢慢开始将现有组件转换为React组件。 Once we have all components converted into React components, take out every React component, separate complete frontend and host it somewhere else. 一旦我们将所有组件都转换为React组件,取出每个React组件,分离完整的前端并将其托管在其他位置。

  2. Host ReactJS somewhere else. 在其他地方托管ReactJS。 Gradually start converting each component to React component, move it to new hosted place and remove them from Rails. 逐渐开始将每个组件转换为React组件,将其移动到新的托管位置,并将其从Rails中移除。 The only catch is, React components will be located at a different location than where Rails is. 唯一要注意的是,React组件将位于与Rails不同的位置。 While navigating within the app, it could give a feel of navigating to a different website. 在应用程序中浏览时,它可以给您导航到其他网站的感觉。

Please share your experience and guide us on which way should we proceed. 请分享您的经验,并指导我们应采取哪种方式。 Our end goal is to have the complete front end in ReactJS and backend on Rails. 我们的最终目标是在ReactJS中拥有完整的前端,并在Rails上拥有后端。 They communicate via APIs, completely removing coupling between frontend and backend. 它们通过API进行通信,从而完全消除了前端和后端之间的耦合。

Thanks 谢谢

Use #1 only if you plan on keeping rails in the server rendering mix (not recommended). 仅在计划将轨保持在服务器渲染混合中时才使用#1(不建议)。 If not then #2 is your best option to just start from scratch with a complete js solution. 如果不是,那么#2是最好的选择,那就是从头开始使用完整的js解决方案。 It doesn't have to feel like a different site if you create a decoupled react component library and a rendering pipeline to the existing rails app when it needs the new views. 如果您在需要新视图时创建一个解耦的React组件库和到现有Rails应用程序的渲染管道,则不必感觉不同。 Then when you have converted all the view components you can wrap up the app logic and make the switch over entirely. 然后,在转换完所有视图组件后,您可以包装应用程序逻辑并完全切换。

Another option is you don't even need to host the new react components seperately, the react components can go through a separate build cycle, then cached on the existing server and rendered as needed from rails. 另一个选择是,您甚至不需要分别托管新的React组件,React组件可以经历一个单独的构建周期,然后缓存在现有服务器上,并根据需要从Rails进行渲染。

I would actually NOT recommend going away from your Ruby centered system. 我实际上不建议您离开以Ruby为中心的系统。 You can use http://ruby-hyperloop.io to build your react.js components in Ruby. 您可以使用http://ruby-hyperloop.io在Ruby中构建react.js组件。 Hyperloop includes a Ruby DSL (HyperReact) for react, and HyperMesh which will give you complete access to your ActiveRecord models in your React components. Hyperloop包括用于React的Ruby DSL(HyperReact)和HyperMesh,可让您完全访问React组件中的ActiveRecord模型。

The huge advantages are you will deal with one language throughout, you do not need to build APIs just to access your data down at the client, and you can use the existing rails tool chain including test tools. 巨大的优势是,您将始终使用一种语言,您无需构建API即可仅在客户端访问数据,并且可以使用现有的Rails工具链(包括测试工具)。

There is a huge library of React.js components which interoperate fine with HyperReact. 有一个巨大的React.js组件库,可以与HyperReact很好地互操作。

If for whatever reason you feel that it would be better to go the JS route then I would recommend react-rails. 如果出于某种原因,您认为最好走JS路线,那么我建议您使用react-rails。 It works, is well supported, and handles prerendering (a very important concept.) You can easily integrate with NPM and webpack as well. 它可以正常工作,得到良好的支持并可以处理预渲染(一个非常重要的概念。)您也可以轻松地与NPM和Webpack集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM