简体   繁体   English

需要建议将React应用程序转换为使用服务器端渲染

[英]Need advice converting React app to use server side rendering

I'm trying to convert an app to SSR for the first time. 我正在尝试将应用程序首次转换为SSR。 All the tutorials I've found only provide instructions for converting simple apps or building from scratch (and sharing the same package.json between client & server), but my app has dedicated package.json files & node_modules folders for the client & server. 我发现的所有教程仅提供有关转换简单应用程序或从头开始构建(以及在客户端和服务器之间共享相同的package.json)的说明,但是我的应用程序具有用于客户端和服务器的专用package.json文件和node_modules文件夹。

  1. In order for the server to build the initial render of my root component, it needs access to all of the client dependencies, correct? 为了使服务器构建我的根组件的初始呈现,它需要访问所有客户端依赖项,对吗?
  2. If the answer to question #1 is true, does it make the most sense to refactor my code to share a single package.json in the root directory? 如果问题#1的答案是正确的,那么重构我的代码以在根目录中共享一个package.json是否最有意义?

Some inputs: 一些输入:

1) In order for the server to build the initial render of my root component, it does need access to all of the client dependencies and they should be SSR compatible(eg: Libraries like reflux doesn't work with SSR AFAIK). 1)为了使服务器能够构建我的根组件的初始呈现,它确实需要访问所有客户端依赖项,并且它们应该与SSR兼容(例如:像回流这样的库不适用于SSR AFAIK)。

2) It does make the most sense to refactor your code to share a single package.json in the root directory, if you have minimal npm dependencies in both server and client. 2)如果您在服务器和客户端中对npm的依赖性都最小,那么重构代码以在根目录中共享一个package.json 确实是最有意义的。 But, this can get un-manageable as you add more packagaes. 但是,当您添加更多packagaes时,这可能变得难以管理。 You can still have multiple package.json in different folders and use workspace management tools like lerna ( Note: I have not worked with lerna and SSR together, but theoretically it should work). 您仍然可以在不同的文件夹中拥有多个package.json,并使用lerna之类的工作区管理工具( 注意:我没有一起使用lerna和SSR,但理论上应该可以使用)。

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

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